[R] Newton method iteration problem

Charles C. Berry cberry at tajo.ucsd.edu
Sat Oct 27 06:12:29 CEST 2007


On Fri, 26 Oct 2007, kevinchang wrote:

>
> 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.

Learn to type without making errors? Learn to format (space and indent) 
your code so errors will be more obvious to you??

Your code worked for me once I corrected the typos and syntax errors.

It even agrees with qnorm( setAlpha ).

If all you want is root finding capability, I suggest you see

    ?uniroot

and friends.

HTH,

Chuck

>
> # 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.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list