Thanks Chales for pointing out the errors. I fixed an errorr and R accepted my "rootFinding" code. But the problem right now is that my code will work only if the intialX value is close enough to the solution. Otherwise, R says there is missing true/false value in the codition test of while loop. I can't figure out what happens. Some advice , please??
#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,maxIter){ while((target(initialX,setAlpha)!=0) && maxIter>0){ initialX<-initialX-(target(initialX,setAlpha)/firstDerivative(initialX)) maxIter<-maxIter-1 } initialX } Charles C. Berry wrote: > > 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@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:[EMAIL PROTECTED] UC San Diego > http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 > > ______________________________________________ > R-help@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. > > -- View this message in context: http://www.nabble.com/Newton-method-iteration-problem-tf4701085.html#a13442728 Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@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.