Curtis Heart Moyo
Curtis Heart Moyo

Reputation: 23

I need help overcoming an error in my Newton Algorithm

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. Trust me, I've been at this assignment for the last 3 weeks (beginner) building up my skills slowly, but I've reached my limit.

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:

NEWTON ALGORITHM

newbon=function(x0,epsi,f,fp){

## where "a" corresponds to x^n and

## "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(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:

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

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

Upvotes: 2

Views: 258

Answers (1)

Jean-Bernard Pellerin
Jean-Bernard Pellerin

Reputation: 12670

log is returning NaN, so I'm guessing you're feeding it a negative value somewhere somehow.

So what I can glean from this is that fp is a function containing logs, so likely function(x){ out=(2/(3))*exp(x^3)-(10)*log(x) return(out) }
And it's being fed a negative argument, which is returning NaN. This can't be compared to epsi, therefore not returning a boolean to your loop condiditon.

It's also possible that this NaN is produced a bit earlier, in b=(fp(a)*a-f(a))/fp(a) where f is called, since that's the one that should contain log and that it's causing the above problem from there instead.

With a bit more info and what your x0,epsi,f,fp parameters are, I can try it in Matlab (or potentially whatever you are using)


Keep in mind that the standard way of implementing Newton's method is simply

while (abs(f(x)) > TOL)
  x = x - f(x)/fp(x);

for finding roots of f


Here's something I used for an assignment myself:

%x0 is a guess, a,b are bounds, f is the function, g is the derivative, TOL is epsi
function [x, flag] = SafeNewton1D(f, g, x0, a, b, TOL)
x = x0;
  while (abs(f(x)) > TOL)
    x = x - f(x)/g(x);
    if (x < a || b < x)
       %value does not fall between a and b, shorten bounds and then guess
       %again from the midpoint.
       midpoint = a + (b - a)/2;
       if (sign(f(a)) == sign(f(midpoint)))
           a = midpoint;
       else
           b = midpoint;
       end
       x = a + (b - a)/2;
    else
        %value falls between a and b, so may as well shorten the bounds.
       if (sign(f(x)) ==  sign(f(a)))
           a = x; 
       else
           b = x;
       end

    end

  end
end

Upvotes: 1

Related Questions