home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l042 / 1.ddi / CHAP2.ARC / RAPHSON2.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1987-12-30  |  2.6 KB  |  72 lines

  1. program Newton_Raphson_Prog;
  2.  
  3. {----------------------------------------------------------------------------}
  4. {-                                                                          -}
  5. {-     Turbo Pascal Numerical Methods Toolbox                               -}
  6. {-     Copyright (c) 1986, 87 by Borland International, Inc.                -}
  7. {-                                                                          -}
  8. {-            Purpose: This sample program demonstrates the                 -}
  9. {-                     Newton-Raphson algorithm.  This program is very      -}
  10. {-                     bare-boned; it contains no I/O checking.             -}
  11. {-                                                                          -}
  12. {-            Unit   : RootsEqu    procedure Newton_Raphson                 -}
  13. {-                                                                          -}
  14. {----------------------------------------------------------------------------}
  15.  
  16. {$R+}              { Enable range checking }
  17.  
  18. uses
  19.   RootsEqu, Crt;
  20.  
  21. var
  22.   InitGuess : Float;           { Initial approximation }
  23.   Tolerance : Float;           { Tolerance in answer }
  24.   Root, Value, Deriv : Float;  { Resulting roots and other info }
  25.   Iter : integer;              { Number of iterations to find root }
  26.   MaxIter : integer;           { Maximum number of iterations }
  27.   Error : byte;                { Error flag }
  28.   OutFile : text;              { Output file }
  29.  
  30. {$F+}
  31. {------- HERE IS THE FUNCTION ----------}
  32. function TNTargetF(X : Float) : Float;
  33. begin
  34.   TNTargetF := Cos(X) - X;
  35. end; { function TNTargetF }
  36. {---------------------------------------}
  37.  
  38. {------- HERE IS THE DERIVATIVE --------}
  39. function TNDerivF(X : Float) : Float;
  40. begin
  41.   TNDerivF := -Sin(X) - 1;
  42. end; { function TNDerivF }
  43. {---------------------------------------}
  44. {$F-}
  45.  
  46. begin { program Newton_Raphson }
  47.   ClrScr;
  48.   InitGuess := 0;
  49.   Tolerance := 1E-6;
  50.   MaxIter := 100;
  51.   Write('Initial Approximation to the root: ');
  52.   Readln(InitGuess);
  53.   Writeln;
  54.   Write('Tolerance (> 0): ');
  55.   Readln(Tolerance);
  56.   Write('Maximum number of iterations (>= 0): ');
  57.   Readln(MaxIter);
  58.  
  59.   Newton_Raphson(InitGuess, Tolerance, MaxIter,
  60.                  Root, Value, Deriv, Iter, Error, @TNTargetF, @TNDerivF);
  61.  
  62.   Writeln;
  63.   Writeln('Error = ', Error);
  64.   Writeln('Number of iterations: ': 30, Iter : 3);
  65.   Writeln('Calculated root: ': 30, Root);
  66.   Writeln('Value of the function' : 28);
  67.   Writeln('at the root: ': 30, Value);
  68.   Writeln('Value of the derivative' :28);
  69.   Writeln('of the function at the' : 28);
  70.   Writeln('calculated root: ' : 30, Deriv);
  71. end. { program Newton_Raphson }
  72.