Maclaurin Series
A machine can only do arithmetic operations:
addition, subtraction, multiplication and division using electronic logic
circuits, how can your calculator or Excel or Fortran on a PC calculate the
values of special functions, such as sin(0.55) or exp(0.456)?
Those special functions can be represented by Maclaurin series, such as
Only arithmetic operations are involved in the Maclaurin series. To obtain the exact value of a function, you need infinite number of terms. The sum of a finite number of terms can be used as a good approximation.
How do we calculate the Maclaurin series? The series is regular, the next term (termK+1) can be calculated by the present term (termk) and we also know the very first term (term0), for example:
therefore it is easy to use a DO loop for this calculation.
sum =
0 !
initialize the sum
term =
1 !
very first term
Do k =0, N ! summation of term0 to termN
sum
= sum + term
term
= term * x / (k+1) ! get termk+1
end
do
exp_x =
sum
Exercises
1) Write a program (myexp.f95) to calculate exp(x) using the Maclaurin series with N terms. x and N are entered via keyboard.
2) Write a program (mysine.f95) to calculate sin(x) using the Maclaurin series with N terms. x and N are entered via keyboard.