R學習之統計算法與R優化包(Newton法) --(R語言編程)-----數模

Newton法說明:

這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述

Newton法R程序:


{x12+x225=0(x1+1)x2(3x1+1)=0

R程序
Newtons<-function(fun,x,ep=1e-5,it_max=100){#fun是函數,x表示初值,ep表示精度,itmax表示最大迭代次數
  index<-0;k<-1
  while(k<=it_max){
    x1<-x;obj<-fun(x);
    x<-x-solve(obj$j,obj$f)
    norm<-sqrt((x-x1)%*%(x-x1))
    if(norm<ep){
      index<-1;break
    }
    k=k+1
  }
  obj<-fun(x)
  list(root=x,it=k,index=index,Fval=obj$f)
}

#例x[1]^2+x[2]^2-5=0;(x[1]+1)*x[2]-(3*x[1]+1)=0
fun<-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)
  list(f=f,j=j)#f是函數值,j是雅可比行列式
}

Newtons(fun,c(0,1))

其中部分說明

  • list對應$使用;function(函數)返回值就是函數的輸出項(沒有對應要賦值)
> a<-list(f=1,j=2)
> a$f
[1] 1
> a<-function(x){list(f=1,j=2)}
> a(1)$j
[1] 2
  • %*%表示轉置相乘更多關於運算符詳見

  • solve()函數
    例求

    {2x1+0x2+2x3=12x1+1x2+2x3=22x1+1x2+0x3=3
> A
     [,1] [,2] [,3]
[1,]    2    0    2
[2,]    2    1    2
[3,]    2    1    0

> b=1:3
> b
[1] 1 2 3

> solve(A,b)
[1]  1.0  1.0 -0.5

A1b
也就是Newton法中要求的
J1f(xk)

具體詳見
  • matrix()函數
    把向量變成矩陣的函數
    nrow
    the desired number of rows.(行數)
    byrow
    logical. If FALSE (the default) the matrix is filled by columns, otherwise the matrix is filled by rows.(默認爲F按照列排,T表示按照行排)
    如:
> matrix(c(1,2,3,4),nrow = 2)
     [,1] [,2]
[1,]    1    3
[2,]    2    4
> matrix(c(1,2,3,4),nrow = 2,byrow = T)
     [,1] [,2]
[1,]    1    2
[2,]    3    4

練習題1

編寫一個用newton法求解非線性方程根的函數,並求函數

x3x1=0

終止條件爲
|xkxk1|<105

代碼

#例x^3-x-1=0
fun<-function(x){
  f<-x^3-x-1
  j<-3*x
  list(f=f,j=j)
}
x=1;#隨便定個初值
it_max=100;index=0;k=1
while(k<=it_max){
  x1=x;
  x<-x-solve(fun(x)$j,fun(x)$f)
  if(sqrt((x1-x)%*%(x1-x))<1e-5) {index=1;break}
  k=k+1
}
list(root=x,it=k,index=index,fval=fun(x)$f)
#或者直接調用之前的Newtons函數
Newtons(fun,1)

結果

> #例x^3-x-1=0
> fun<-function(x){
+   f<-x^3-x-1
+   j<-3*x
+   list(f=f,j=j)
+ }
> x=1;#隨便定個初值
> it_max=100;index=0;k=1
> while(k<=it_max){
+   x1=x;
+   x<-x-solve(fun(x)$j,fun(x)$f)
+   if(sqrt((x1-x)%*%(x1-x))<1e-5) {index=1;break}
+   k=k+1
+ }
> list(root=x,it=k,index=index,fval=fun(x)$f)
$`root`
[1] 1.324718

$it
[1] 5

$index
[1] 1

$fval
[1] 1.070586e-06

> Newtons(fun,1)
$`root`
[1] 1.324718

$it
[1] 5

$index
[1] 1

$Fval
[1] 1.070586e-06

練習題2

用Newton法求非線性方程組

{x12+x221=0x13x2=0

取初始值
x(0)=(0.8,0.6)T
,精度要求
ξ=103

R代碼

#例 x_1^2+x_2^2-1=0\\x_1^3-x_2=0 精度10^(-3)
fun<-function(x){
  f<- c(x[1]^2+x[2]^2-1,x[1]^3-x[2])
  j<-matrix(c(2*x[1],2*x[2],3*x[1]^2,-1),nrow = 2,byrow = T)
  list(f=f,j=j)
}
#直接調用之前的Newtons函數(因爲精度變了,稍微改下)
Newtons<-function(fun,x,ep,it_max=100){#itmax表示最大迭代次數
  index<-0;k<-1
  while(k<=it_max){
    x1<-x;obj<-fun(x);
    x<-x-solve(obj$j,obj$f)
    norm<-sqrt((x-x1)%*%(x-x1))
    if(norm<ep){
      index<-1;break
    }
    k=k+1
  }
  obj<-fun(x)
  list(root=x,it=k,index=index,Fval=obj$f)
}
Newtons(fun,c(-0.8,0.6),1e-3)

結果

> #例 x_1^2+x_2^2-1=0\\x_1^3-x_2=0 精度10^(-3)
> fun<-function(x){
+   f<- c(x[1]^2+x[2]^2-1,x[1]^3-x[2])
+   j<-matrix(c(2*x[1],2*x[2],3*x[1]^2,-1),nrow = 2,byrow = T)
+   list(f=f,j=j)
+ }
> #直接調用之前的Newtons函數(因爲精度變了,稍微改下)
> Newtons<-function(fun,x,ep,it_max=100){#itmax表示最大迭代次數
+   index<-0;k<-1
+   while(k<=it_max){
+     x1<-x;obj<-fun(x);
+     x<-x-solve(obj$j,obj$f)
+     norm<-sqrt((x-x1)%*%(x-x1))
+     if(norm<ep){
+       index<-1;break
+     }
+     k=k+1
+   }
+   obj<-fun(x)
+   list(root=x,it=k,index=index,Fval=obj$f)
+ }
> Newtons(fun,c(-0.8,0.6),1e-3)
$`root`
[1] 0.8260317 0.5636240

$it
[1] 6

$index
[1] 1

$Fval
[1] 4.106884e-07 9.268858e-07

DONE!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章