next up previous
Next: Program Design Up: Fortran Implementation Previous: Summation Using DO Loops

Convergence

One issue was saliently forgotten in the discussion above. What should the value of N be? You might say 100 terms should be enough to calculate the sum with sufficient accuracy. Try this for different values of x.

But the efficient and preferred approach is to calculate at each step of the DO loop an estimation of the truncation error as the value of the first discarded value, given in equation 3. To do this, use an infinite DO loop (section 2.5.2) of the manual and at each iteration calculate the absolute value of the next term to be added, $\left\vert
E(k)\right\vert $. EXIT the loop if this value is smaller than a certain precision $\varepsilon $.

Most programs like this also should contain a guard against runaway endless loops. If the number of iterations becomes larger than a certain maximal number of iterations, you should exit the loop and print an error message. For example:

    INTEGER, PARAMETER :: max_iterations=100 ! Max # of iterations
    REAL(KIND=wp) :: epsilon ! Error allowed
    ...
    k=0
    Summation: DO
      k=k+1
      ... ! Calculate E(k)
      IF( ABS(E(k)) < epsilon ) THEN
        WRITE(UNIT=*,FMT=*) ''Convergence was achieved!''
        EXIT Summation
      ELSE IF( k >= max_iterations ) THEN
        WRITE(UNIT=*,FMT=*) ''Convergence not achieved!''
        STOP ! Or EXIT if you can figure out how?
      ELSE
        ... ! Calculate the sum here
      END IF
    END DO



Phil Duxbury
2000-09-11