next up previous
Next: Convergence Up: Fortran Implementation Previous: Fortran Implementation

Summation Using DO Loops

In worksheet 1 you learned how to evalute a sum of the form $%
\sum_{k=0}^{N}E(k)$, where E(k) is some expression depending on k, using Fortran:

    sum=E(0)
    DO k=1,N
      sum=sum+E(k)
    END DO

The only difference between this worksheet and the previous one is that the expression to sum is more complicated. Complication comes because of the presence of a factorial, which is not a built in function in Fortran. However, even if there were a factorial function in Fortran, the best way to evaluate E(k) is not to directly compute $E(k)=\frac{\left( -1\right) ^{k}%
}{\left( 2k+1\right) k!}x^{2k}$. Rather, you can use a recursive relation (which is very easy to derive from eq.1) which just depends on the ratio R(k) of two consecutive terms in the power series, R(k) = E(k)/E(k-1). In terms of the ratio R(k), the sum in then found using the simple loop,

    expression=E(0)
    DO k=1,N
      expression=expression*R(k)
    END DO

Notice that we already needed the same loop to do the summation. So in fact we can merge the two DO loops into one loop from k=1 to k=N. Do not forget to initialize the sum to the zeroth-order term sum=E(0)=1. Look through the formulas given above to see what equations you need to put in the loop for calculating E(0) and R(k).


next up previous
Next: Convergence Up: Fortran Implementation Previous: Fortran Implementation
Phil Duxbury
2000-09-11