1

I'm trying to implement the Newton-Raphson method in Scilab where the input must be a root point of the equation already established inside the function. However after doing the derivative of the function and inputting the root, I get division by zero. Any idea why the derivative is equal to zero when inputting 2 as the root point?

function y = fun(x)
   y = -0.01 + (1/1+ x^2);
endfunction
function y= dfun(x)
    y = (-2.00*x) / (1+x^2)^2
endfunction
No = 0;
x1 = 0;
x0 = input('Diga el valor inicial: ');
error = 1e^-10;
while (abs(fun(x0)) > error)
    x1 = x0 - fun (x0) / dfun(x0);
    x0 = x1;
    No = No + 1;
end;
disp(x1, "Valor: ");
disp(No, "Numero de iteraciones: ")


ERROR HERE
Diga el valor inicial: 2
    x1 = x0 - fun (x0) / dfun(x0);
                                  !--error 27 
Division by zero...
at line      12 of exec file called by :    
exec('C:\Users\Silvestrini\Documents\Raphson.sci', -1)
Silvestrini
  • 445
  • 4
  • 19

1 Answers1

2

The problem does not occur at x=2. To see what's going on, insert disp(x0); at the beginning of the loop:

 2.  
 33.1875  
 20184679.  
 1.675D+36  
 6.59D+180  

and this is when the value of the derivative underflows to zero.

The division by zero is a symptom of a different problem: the method catastrophically diverges. The reason is simple: your formula in fun is missing parentheses, which results in a function that has no zeros. Use

y = -0.01 + (1/(1+ x^2))

There is also another typo: 1e^-10; should be 1e-10

And a word of caution: error is a name of a built-in function in Scilab, so using it as a name of a variable is not recommended.