数値計算

f<-function(x,y){(x-3)^2+y^2-3}
g<-function(x,y){sin(x)+exp(y-1)-1}

# fx<-function(x){2*(x-3)}
# fy<-function(y){2*y}
# gx<-function(x){cos(x)}
# gy<-function(y){exp(y-1)}

x<-0
y<-0
T<-100
d<-0.01

for(t in 1:T){

A<-matrix(c(
(f(x+d,y)-f(x-d,y))/(2*d),(f(x,y+d)-f(x,y-d))/(2*d),
(g(x+d,y)-g(x-d,y))/(2*d),(g(x,y+d)-g(x,y-d))/(2*d)
),byrow=T,2,2)


dX<- -solve(A)%*%c(f(x,y),g(x,y))
x<-x+dX[1]
y<-y+dX[2]
}
print(x);print(y)
  • 実行
# 実行
> print(x);print(y)
[1] 1.997356
[1] -1.41234