home *** CD-ROM | disk | FTP | other *** search
Modula Implementation | 1989-08-29 | 3.3 KB | 116 lines |
- IMPLEMENTATION MODULE NM;
-
- PROCEDURE NM( M : REAL; (* The evaluation point. *)
- INITIALEST : REAL; (* The initial estimate. *)
- epsilon : REAL; (* Tolerance for the solution. *)
- FUNCT : SINGARG; (* The function procedure variable. *)
- DERIV : SINGARG (* The derivative procedure variable. *)
- ): REAL; (* The returned approximation. *)
-
- CONST MAXCOUNT = 100; (* Maximum acceptable number of iterations. You can set
- this to whatever your patience will bear. *)
-
- VAR YN,YN1,DELTAOLD,DELTANEW,RESULT : REAL;
- COUNTER,ERROR : CARDINAL;
-
- BEGIN
- ERROR := 0;
- YN := INITIALEST;
-
- (* Make an initial estimate and set the test variable. *)
- YN1 := YN - (FUNCT(YN) - M)/DERIV(YN);
- DELTAOLD := ABS(ABS(YN1) - ABS(YN));
- YN := YN1; (* Get ready for the next iteration. *)
- COUNTER := 1;
- RESULT := 0.0;
-
- LOOP
- YN1 := YN - (FUNCT(YN) - M)/DERIV(YN);
- DELTANEW := ABS(ABS(YN1) - ABS(YN));
-
- (* Now perform tests to signal the end. *)
- IF DELTANEW >= DELTAOLD THEN
- ERROR := 1;
- EXIT;
- ELSIF DELTANEW <= epsilon THEN (* Successful finish. *)
- EXIT;
- ELSIF COUNTER >= MAXCOUNT THEN (* Slugish convergence. *)
- ERROR := 2;
- EXIT;
- END;
-
- (* Now reset the counter and the variables for the next iteration. *)
- INC(COUNTER);
- DELTAOLD := DELTANEW;
- YN := YN1;
- END; (* LOOP *)
- IF ERROR = 0 THEN RESULT := YN1; END;
- RETURN RESULT;
-
- END NM;
-
-
- END NM.
-
-
-
- MODULE CALLNM;
- (* Module to demonstrate calling the Newton's method module for finding the
- inverse of the exp(X) function or the ln(X). *)
-
- FROM MathLib0 IMPORT exp; (* Import the standard procedure exp *)
-
-
- FROM NM IMPORT NM,ERROR,SINGARG;
-
- VAR FUNCT, DERIV : SINGARG;
- M, epsilon,INITIALEST,RESULT : REAL;
-
- BEGIN
- FUNCT := exp; (* The function whose reciprocal we want. *)
- DERIV := exp; (* The derivative of that function is the same. *)
- M := 5.5; (* We want to solve for ln(5.5) *);
- (* Make an initial estimate of 1.0 *);
- INITIALEST := 1.0;
- epsilon := 1.0E-14; (* Set the error tolerance. *)
-
- RESULT := NM(M,INITIALEST,epsilon,FUNCT,DERIV);
-
- (* Here we should test the error variable. *)
-
- END CALLNM.
-
-
-
-
- DEFINITION MODULE NM;
-
-
- EXPORT QUALIFIED SINGARG,NM,ERROR;
-
- (* Declare a procedure type for single argument functions. *)
- TYPE SINGARG = PROCEDURE(REAL):REAL;
-
- VAR ERROR : CARDINAL;
- (* A global error variable that signals either successful completion or
- the type of error encountered.
-
- VALUE ERROR TYPE.
- 0 NO ERRORS
- 1 Diverging or the same instead of converging.
- 2 Too many iterations.
-
- *)
-
- PROCEDURE NM( M : REAL; (* The evaluation point. *)
- INITIALEST : REAL; (* The initial estimate. *)
- epsilon : REAL; (* Tolerance for the solution. *)
- FUNCT : SINGARG; (* The function procedure variable. *)
- DERIV : SINGARG (* The derivative procedure variable. *)
- ): REAL; (* The returned approximation. *)
-
-
- END NM.
-
-