Solving nonlinear equations with R language

Source: Internet
Author: User

In essence, Newtons is the iterative way, so that the approximate solution (Taylor Formula) constantly approximation to the real solution, when the accuracy requirements, you can think of the approximate solution as the real solution

The following Newtons method is implemented in R language

Newtons<-function (fun,x,ep=1e-5,it_max=100) # #fun为需要求解的方程 (group), X is the initial solution, EP is the precision requirement, It_max is the maximum number of iterations
{
index<-0 # #指示是否完成迭代成功 to meet precision requirements
K<-1 # #迭代次数
while (K<=it_max)
{
X1<-x;obj<-fun (x);
X<-x-solve (obj$j,obj$f) # #obj $J is the jacobj matrix of equations (groups)
Norm<-sqrt ((x-x1)%*% (x-x1)) # #x (k+1) y and X (k) Accuracy difference
if (NORM<EP)
{
Index<-1
Break
}
K<-k+1
}
Obj<-fun (x);
List (ROOT=X,IT=K,INDEX=INDEX,FUNVAL=OBJ$F,JACOBI=OBJ$J)
}

Now to construct the equation (group) to be solved and its jacobj matrix

Funs<-function (x)
{
F<-c (x[1]^2+x[2]^2-5, (x[1]+1) *x[2]-(3*x[1]+1)) # #方程组
J<-matrix (c (2*x[1],2*x[2],x[2]-3,x[1]+1), nrow=2,byrow=t) # #jacobj矩阵, respectively, to X1 and X2 to seek first-order guidance
List (F=F,J=J)
}

Calculation Result:

> Newtons (funs,c (0,1))
$root
[1] 1 2

$it
[1] 6

$index
[1] 1

$FunVal
[1] 1.598721e-14 6.217249e-15

$Jacobi
[, 1] [, 2]
[1,] 2 4
[2,]-1 2

Interested students, you can use Debug (newtons) to debug the calculation process

Solving nonlinear equations with R language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.