[R] NEWTON ALGORITHM
Uwe Ligges
ligges at statistik.tu-dortmund.de
Thu Dec 22 09:31:39 CET 2011
On 21.12.2011 22:43, Curtis Moyo wrote:
> Hi,
>
> My name is Curtis and I'm a 1st year student in Biochemistry at the
> University of Geneva. I need some help completing the code for my NEWTON
> ALGORITHM. It is a bonus exercice to our autumn semester maths exam and we
> can hand it in or not. Usually people copy and paste but I decided to sit
> down and review theory and ask for help left right and center.
>
> My problem is that I cannot get my function to give me the solution. Here
> is my code:
>
> ## PRIMARY FUNCTION
> f=function(x){
> out=(2/(3))*exp(x^3)-(10)*log(x)
> return(out)
> }
> ## 1ST DERIVATIVE
> fp=function(x){
> out=(2)*(x^2)*exp(x^3)-(10/x)
> return(out)
> }
> ## 2ND DERIVATIVE
> fpp=function(x){
> out=(4)*(x)*exp(x^3)+(6)*(x^4)*exp(x^3)+(10/x^2)
> return(out)
> }
>
> I am trying to find the "zero" of "fp",my 1st derivative, with my newton
> algorithm:
>
> newbon=function(x0,epsi,f,fp){
>
> ## where "a" corresponds to x^n et
>
> ## "b" corresponds to x^n+1
>
> ## We are looking for x^n+1 such that f(x^n+1)=0
>
> ## NB: fp(a)*(a-b)=f(a) when f(b)=0
>
> a=x0
> b=(fp(a)*a-f(a))/fp(a)
> while(abs(-fp(a)*(b-a))>epsi){
> a=b
> b=(fp(a)*a-f(a))/fp(a)
> }
> out=NULL
> out=a
> return(out)
> }
>
> I must admit that my algorithm was painfully congested and I've been having
> headaches to get it to work. However, I always get the same error message:
>
> Erreur dans while (abs(-fp(a) * (b - a))> epsi) { :
> valeur manquante là où TRUE / FALSE est requis
> De plus : Message d'avis :
> In log(x) : production de NaN
This question (like all homework questions) is inappropriate on this
mailing list and we usually do not give any answers in such cases! You
should ask your instructor. She or he will certainly answer like this:
"If you read that: log() produced NaN, hence was undefined. Now, you
have only one function call for log, so think about when log(x) is
undefined ..."
Uwe Ligges
> Please forgive the French; it is my study language. Quite frankly I don't
> mind if I don't get the bonus marks, what is really important is that I
> understand why it doesn't work and what condition is missing.
>
> You're help would be kindly appreciated.
>
> Curtis MOYO
>
> [[alternative HTML version deleted]]
>
>
>
>
> ______________________________________________
> 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.
More information about the R-help
mailing list