home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / CLM89OCT.ZIP / NEWTON.MOD next >
Encoding:
Modula Implementation  |  1989-08-29  |  3.3 KB  |  116 lines

  1. IMPLEMENTATION MODULE NM;
  2.  
  3. PROCEDURE NM(     M : REAL;  (* The evaluation point. *)
  4.                   INITIALEST : REAL;  (* The initial estimate. *)
  5.                   epsilon  : REAL;  (* Tolerance for the solution. *)
  6.                   FUNCT : SINGARG;  (* The function procedure variable. *)
  7.                   DERIV : SINGARG  (* The derivative procedure variable. *)
  8.                   ): REAL;  (* The returned approximation. *)
  9.                   
  10. CONST MAXCOUNT = 100; (* Maximum acceptable number of iterations. You can set
  11.                          this to whatever your patience will bear. *)
  12.  
  13. VAR  YN,YN1,DELTAOLD,DELTANEW,RESULT : REAL;
  14.      COUNTER,ERROR : CARDINAL;
  15.      
  16. BEGIN
  17.   ERROR := 0;
  18.   YN := INITIALEST;
  19.  
  20. (* Make an initial estimate and set the test variable. *)
  21.   YN1 := YN - (FUNCT(YN) - M)/DERIV(YN);
  22.   DELTAOLD := ABS(ABS(YN1) - ABS(YN));
  23.   YN := YN1;  (* Get ready for the next iteration. *)
  24.   COUNTER := 1;
  25.   RESULT := 0.0;
  26.   
  27.   LOOP
  28.     YN1 := YN - (FUNCT(YN) - M)/DERIV(YN);
  29.     DELTANEW := ABS(ABS(YN1) - ABS(YN));
  30.  
  31. (* Now perform tests to signal the end. *)
  32.     IF DELTANEW >= DELTAOLD THEN
  33.       ERROR := 1;
  34.       EXIT;
  35.     ELSIF DELTANEW <= epsilon THEN (* Successful finish. *)
  36.       EXIT;
  37.     ELSIF COUNTER >= MAXCOUNT THEN (* Slugish convergence. *)
  38.       ERROR := 2;
  39.       EXIT;
  40.     END;
  41.  
  42. (* Now reset the counter and the variables for the next iteration. *)
  43.     INC(COUNTER);
  44.     DELTAOLD := DELTANEW;
  45.     YN := YN1;  
  46.   END;  (* LOOP *)  
  47.   IF ERROR = 0 THEN RESULT := YN1; END;
  48.   RETURN RESULT;
  49.   
  50. END NM;
  51.  
  52.                   
  53. END NM.
  54.  
  55.  
  56.  
  57. MODULE CALLNM;
  58. (* Module to demonstrate calling the Newton's method module for finding the
  59.    inverse of the exp(X) function or the ln(X). *)
  60.  
  61. FROM MathLib0 IMPORT exp; (* Import the standard procedure exp *)
  62.  
  63.  
  64. FROM NM IMPORT NM,ERROR,SINGARG;
  65.  
  66. VAR FUNCT, DERIV : SINGARG;
  67.     M, epsilon,INITIALEST,RESULT : REAL;
  68.  
  69. BEGIN
  70.   FUNCT := exp;  (* The function whose reciprocal we want. *)
  71.   DERIV := exp;  (* The derivative of that function is the same. *)
  72.   M := 5.5;  (* We want to solve for ln(5.5) *);
  73. (* Make an initial estimate of 1.0  *);
  74.   INITIALEST := 1.0;
  75.   epsilon := 1.0E-14;  (* Set the error tolerance. *)
  76.    
  77.   RESULT := NM(M,INITIALEST,epsilon,FUNCT,DERIV);
  78.   
  79. (* Here we should test the error variable. *)
  80.   
  81. END CALLNM.
  82.  
  83.  
  84.  
  85.  
  86. DEFINITION MODULE NM;
  87.  
  88.  
  89. EXPORT QUALIFIED SINGARG,NM,ERROR;
  90.  
  91. (* Declare a procedure type for single argument functions. *)
  92. TYPE SINGARG = PROCEDURE(REAL):REAL;
  93.  
  94. VAR ERROR : CARDINAL;
  95. (* A global error variable that signals either successful completion or
  96.    the type of error encountered.
  97.    
  98.    VALUE           ERROR TYPE.
  99.      0               NO ERRORS
  100.      1               Diverging or the same instead of converging.
  101.      2               Too many iterations.
  102.      
  103.      *)
  104.  
  105. PROCEDURE NM(     M : REAL;  (* The evaluation point. *)
  106.                   INITIALEST : REAL;  (* The initial estimate. *)
  107.                   epsilon  : REAL;  (* Tolerance for the solution. *)
  108.                   FUNCT : SINGARG;  (* The function procedure variable. *)
  109.                   DERIV : SINGARG  (* The derivative procedure variable. *)
  110.                   ): REAL;  (* The returned approximation. *)
  111.                   
  112.                   
  113. END NM.
  114.  
  115.  
  116.