[R] Newton method iteration problem
kevinchang
shukai at seas.upenn.edu
Sat Oct 27 05:25:59 CEST 2007
Hi all,
I am coding for finding the root of f(x)= phi(x) -alpha where phi(x) is the
cumulative density function and alpha is constant . The problem right now is
I can't get the "initialX" representing the root out of the while loop when
ending , it seems to me it disappear when the loop ends accroding to the
error message. I need help . Please suggest the cause or solution to this
problem. Thanks.
# code
#generate target function (phi(x)-alpha) (allow input x and alpha)
target<-function(x,alpha){
pnorm(x)-alpha
}
#generate the first derivative of the of the target function
firstDerivative<-function(x){
exp(-(x^2)/2)/sqrt(2*pi)
}
# Finding the root by Newton method
rootFinding<-function(initialX,setAlpha){
while(target(initialX,setAlpha)!=0){
initialX<-initialX-(target(initialX,setAlpha)/firstfirstDerivative(initialX)
}
initialX
}
--
View this message in context: http://www.nabble.com/Newton-method-iteration-problem-tf4701085.html#a13439031
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list