home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / e / e032 / 3.ddi / FILES / PROGRAMM.PAK / NEWTON.M < prev    next >
Encoding:
Text File  |  1992-07-29  |  1.3 KB  |  44 lines

  1.  
  2. (*********************************************************************
  3.  
  4.         Adapted from
  5.         Roman E. Maeder: Programming in Mathematica,
  6.         Second Edition, Addison-Wesley, 1991.
  7.  
  8.  *********************************************************************)
  9.  
  10.  
  11. BeginPackage["NumericalMath`Newton`"]
  12.  
  13. NewtonZero::usage = "NewtonZero[expr, x, x0] finds a zero of expr as a function of x
  14.     using the initial guess x0 to start the iteration. NewtonZero[f, x0] finds
  15.     a zero of the function f. The recursion limit determines the
  16.     maximum number of iteration steps that are performed."
  17.  
  18. NewtonFixedPoint::usage = "NewtonFixedPoint[f, x0] finds a fixed point
  19.     of the function f using the initial guess x0 to start the iteration."
  20.  
  21. Newton::noconv = "Iteration did not converge in `1` steps."
  22.  
  23. Begin["`Private`"]
  24.  
  25. NewtonZero[expr_, x_, x0_] := NewtonZero[ Function[x, expr], x0 ]
  26.  
  27. NewtonZero[f_, x0_] :=
  28.     Block[{res, x0a, prec = Precision[x0], fp = f'},
  29.         x0a = SetPrecision[x0, prec + 10];
  30.         res = FixedPoint[(# - f[#]/fp[#])&, x0a, $RecursionLimit];
  31.         x0a = f[res];
  32.         If [ !NumberQ[res] || Accuracy[x0a] - Precision[x0a] < prec,
  33.             Message[Newton::noconv, $RecursionLimit] ];
  34.         N[res, prec]
  35.     ]
  36.  
  37. NewtonFixedPoint[f_, x0_] := NewtonZero[(f[#] - #)&, x0]
  38.  
  39. End[ ]
  40.  
  41. Protect[ NewtonZero, NewtonFixedPoint]
  42.  
  43. EndPackage[ ]
  44.