开发者

newton.method issues

开发者 https://www.devze.com 2023-01-09 21:45 出处:网络
Is this method broken in R? I am using it to find roots of the following function: f(x) = 2.5*exp(-0.5*(2*0.045 - x)) + 2.5*exp(-0.045) + 2.5*exp(-1.5*x) - 100

Is this method broken in R? I am using it to find roots of the following function: f(x) = 2.5*exp(-0.5*(2*0.045 - x)) + 2.5*exp(-0.045) + 2.5*exp(-1.5*x) - 100

It is giving an answer of -38.4762403 which is not even close (f(x) = 2.903809e+25 for x=-38.4762403). The answer should be around 0.01-0.1. This function should converge..

Even for a simple function like f(x) = exp(-x) * x, it gives answer as 8.89210984 for which f(x) = 0.001222392 and I set tolerance to 10^-12..

Also, is there a non graphical version of newton method? I looked at n开发者_运维技巧leqslv but have no idea how to use it..

Thanks for your help.


R has a number of root finders, such as uniroot and polyroot. For more complicated problems you can use optimisation functions such as optim, optimize or nlminb. Here is an example of solving this problem with uniroot.

## define the function
f <- function(x){                                                                                                                                            
  2.5*exp(-0.5*(2*0.045 - x)) + 2.5*exp(-0.045) + 2.5*exp(-1.5*x) - 100                                                                                      
}                                                                                                                                                            

## plot the function 
y <- seq(-20,20,0.1) 
plot(y,f(y),ylim = c(-100,100),xlim=c(-20,20))

## find the roots
uniroot(f,c(-5,0))
uniroot(f,c(0,10))
0

精彩评论

暂无评论...
验证码 换一张
取 消