Search code examples
wolfram-mathematica

Problem with Modified Newton Raphson method on Mathematica


I am trying to get the roots of the function with the modified newton raphson method, but on the second iteration the value for xi+1 blows up to -393, isnt't it supposed to get closer to the expected value of the root? (which is 0.34997). Also I am trying to get the root with an "error" below the "eS" criteria. Pls help

n = 50;
eS = 10^-4;
f[x_] := (x^2 - 10 x + 25) (x - Exp[-3 x])
Plot[f[x], {x, -1, 2}]
xi = 0.5;
Do[
 f[xi]; f'[xi]; f''[xi];
 xi1 = xi - (f[xi]*f'[xi]/(f'[xi])^2 - f[xi]*f''[xi]);
 If[Abs[f[xi]] < 10^-7,
  Print["The root is approx= ", xi1 // N, " iterations needed: ", i];
  Break[]];
 eA = Abs[(xi1 - xi)/xi1];
 If[eA < eS,
  Print["The root is approx= ", xi1 // N, " iterations needed: ", i];
  Break[]];
 xi = xi1;
 If[i == n, Print["Did not converge in ", n, " iteration(s)"]];
 , {i, 1, n}
 ]

Solution

  • There are many different "Modified Newton Raphson" methods. The one I am familiar with is

    u[x_] := f[x]/f'[x]
    
    Do[
     xi1 = xi - u[xi]/u'[xi];
     If[Abs[f[xi]] < 10^-7, 
      Print["The root is approx= ", xi1 // N, " iterations needed: ", i];
      Break[]];
     eA = Abs[(xi1 - xi)/xi1];
     If[eA < eS, 
      Print["The root is approx= ", xi1 // N, " iterations needed: ", i];
      Break[]];
     xi = xi1;
     If[i == n, Print["Did not converge in ", n, " iteration(s)"]];, {i, 1, n}]
    

    Here is a more functional way (no Do loop)

    FixedPointList[# - u[#]/u'[#] &, .5]
    (* {0.5, 0.372215, 0.350544, 0.34997, 0.34997, 0.34997, 0.34997} *)