next up previous contents
Next: homework Up: Taylor Polynomials Previous: Polynomial Evaluation   Contents

Sample Program

The following program calculates the order of the Taylor polynomial which approximates within TOL, for a given X. Note that P is the sum of the series up to .
PROGRAM TAYLOR
!
! APPROXIMATES EXP(X) BY A TAYLOR
! POLYNOMIAL OF DEGREE AT MOST 100.
! THE DEGREE IS COMPUTED S.T. 
! EXP(X)-P(X) <= TOL
!
IMPLICIT NONE
REAL :: X, P, R, TERM, TOL, E, ERR
INTEGER :: I
INTEGER, PARAMETER :: N_MAX = 100
!
PRINT*,"X="
READ*,  X
PRINT*,"TOLERANCE="
READ*,  TOL
!
E    = EXP(X)    ! TRUE FUNCTION VALUE
ERR  = TOL/E     ! NORMALIZED ERROR
!
TERM = 1.0
P    = 1.0
DO I=1,N_MAX
  TERM = TERM*X/REAL(I) 
  P = P + TERM          
  IF (TERM .LT. ERR) EXIT     
END DO
!
PRINT*,"ORDER=",N-1,". TRUE ERROR=",(E-P)
!
END PROGRAM TAYLOR



Adrian Sandu 2001-08-26