FORTRAN 77 Lesson 7: 1 Numerical Integration

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

FORTRAN 77 Lesson 7

Reese Haywood
Ayan Paul

Numerical Integration

A general all purpose Numerical Integration technique is the Trapezoidal


Method. For most functions, this method is fast and accurate. Other functions, may require more sofisticated methods, which can be found in Numerical Recipes, but we will stick to simple funcitons in this class.

1.1

Trapezoidal Rule

If we are given the function that represents the curve, we can evaluate the
function at points spaced along the interval of interest. If we join the points
on the curve with straight line segments, we form a group of trapezoids whose
combined areas approximate the are under the curve. The closer the points
are together on the curve the more trapezoids there are in the interval, and
thus the more accurate will be our approximation to the integral.
Recall that the area of a trapezoid is equal to one-half the base times the
sum of the two heights (or sides): area = 12 base(height1 + height2). Thus,
the area of the trapezoid is computed using the pair of points (x1 , y1 ) and
(x2 , y2): A = 21 (x2 x1 )(y1 + y2 ). If all of our points are equally spaced, then
(x2 x1 ) is a constant, and if there are N trapezoids that represent the curve
then the integral can be approximated by:
N
X
base
(y1 + 2
yk + yN +1 )
2
k=2

(1)

When computing an integral with this technique, we need to remember


that the data points on the curve could come from different sources. If we
have the equation of the curve, we can choose the data points to be close
1

together or far apart. Another possibility is that the points are experimentally collected; in this case, we have discrete x and y values, and we cannot
change the size of the base, it is fixed by x, but this does not keep us from
using the trapezoid rule.

1.2

Simpsons Rule

The Simpsons Rule (or Simpsons 13 Rule) makes the assumption that the
function can be approximated by a series of overlapping second order curves.
Thus we always need even number of intervals to do an integral by this
method. The detailed derivation of this method can be found in any book
on Numerical Integration or Statistics. The formula for the integration is
Z xN+1
h
y(x)dx = [y1 + 4 (y2 + y4 + . . .) + 2 (y3 + y5 + . . .) + yN +1 ] (2)
3
x1
with N odd. The Simpsons Rule is more accurate as it uses second order approximation as compared to the first order approximation used by
the Trapezoidal Rule and thus uses more computing time and resources.
For most problems the Trapezoidal Rule will be good enough, however, you
might sometime have to appeal to the Simpsons Rule for more complicated
integrals.

Solving Ordinary Differential Equations

Before we can write a Fortran program to solve an Ordinary Differential


Equation (ODE) we must learn a little about solving ODEs by hand. For
this section I will quote from Introduction to Ordinary Differential Equations
with Mathematica by Gray, Mezzino and Pinsky. Also, I will quote from
Numerical Recipes, which we used last time.
Second order ODEs can always be reduced to the study of sets of firstorder differential equations. For exmple
d2 y
dy
+ q(x)
= r(x)
2
dx
dx
can be rewritten as two first-order equations
dy
= z(x)
dx
dz
= r(x) q(x)z(x)
dx
2

(3)

(4)
(5)

where z is a new variable.


The generic problem in ordinary differential equations is thus reduced
to the study of a set of N coupled first-order differential equations for the
functions yi , i = 1, 2, 3, ..., N, Having the general form
dyi (x)
= fi (x, y1 , ..., yN ), i = 1, ..., N
dx

(6)

where the functions fi on the right-hand side are known.


Most differential equations do not have exact solutions. Even if a differential equation has an exact solution, the solution may be so complicated
that it is useless for practical purposes.
On the other hand, we can try to find an approximation to the solution
of a differential equation with an initial condition. For example, suppose we
are given a first-order initial value problem
y = f (t, y)
y(a) = Y0

(7)
(8)

on an interval a t b. Although we may not be able to obtain a formula for


the solution of eqn.s 5 and 6, we can subdivide the interval as a = t0 < t1 <
... < tN = b and try to assign approximate values y(tn ) to tn for n = 1, ..., N.
Instead of a formula we will have an approximation to the solution expressed
as a table of the y(tn )s in terms of the tn s. By graphing the table we can
visualize the solution.

2.1

The Euler Method

The Euler method was the first method used to find numerical solutions to
differential equaitons. In spite of the fact that it is rarely used in practice, we
need to study it because it serves as a model for more complicated methods
such as the Rung-Kutta method, an all purpose method we will write our
program with.
Suppose we want to construct a solution to equation (5) above. If we
know the solution exactly, and that it is twice differentiable, we can write
the Taylor expansion as
y(t1 ) = y(t0 ) + (t1 t0 )y (t0 ) +
3

(t1 t0 )2
y (t0 )
2

(9)

Letting h = t1 t0 we can rewrite (7) as


y(t1 ) = y(t0 ) + hf (t0 , Y0 ) +

h2
y (t0 )
2

(10)

where we have just substituted equation (5) evaluated at (t0 , y0 ) for y (t0 )
and also substituted h for (t1 t0 ). With h small this becomes simply
y(t1 ) y(t0 ) + hf (t0 , Y0 )

(11)

and, since y(t0) = Y0 we can define


Y1 = Y0 + hf (t0 , Y0 )

(12)

Then Y1 is an approximation to y(t1 ). More generally, we define


Yn+1 = Yn + hf (tn , Yn )

(13)

for 0 n N 1. The Euler method, or tangent method, consists in


approximating the solution to (5) by means of (11), which we call the Euler
method formula.

2.2

The Runge-Kutta Method

The Runge-Kutta method is an improvement of the Euler method, and is


derived in much the same way. However, the Taylor series is truncated in
the 5th power of (t1 t0 ), so the order of the method is fourth-order, and
errors are introduced on the order of h5 . A derivation of the Runge-Kutta
method is in most Numerical Analysis book. I will only give the results of
the derivation here.
The general form of the Runge-Kutta method is
h
Yn+1 = Yn + (a1,n + 2a2,n + 2a3,n + a4,n ),
6

(14)

where
a1,n = f (tn , Yn )
h
h
a2,n = f (tn + , Yn + a1,n )
2
2
h
h
a3,n = f (tn + , Yn + a2,n )
2
2
a4,n = f (tn + h, Yn + h a3,n )
4

(15)

2.3

Coding the Equations

Whether we are going to code the Euler or Runge-Kutta method we need to


stop and think about what needs to happen throughout the code. We should
ask ourselves the following kinds of questions, do we only want to write the
answers y(tn ) at each tn to a file to view them graphically later? Do we need
an array that holds the values of the function at each tn so that we can use it
in another program, or as a comparision to what we think the answer should
be? Once you have decided on all of the factors, you can successfully write
your differential equation solver. Be warned though, it will not be easy to
make a very general program, most will be specific to the problem at hand.

Your next task


1. Write a program Rto use the trapezoid method and integrate the fol2
lowing function: 0 (1 exp(0 ex ))dx. You should first write you
trapezoid function or subroutine and check it with an easy function, i.e.
x2 from 0,1. Then modify the function to be the function given above
and check to see if you get the following answers, for 0 = 1, 2, 10, 10000,
your answer for the integral should be:
INT = 0.6425, 0.986344, 1.65688, 3.12214.
2. Write a program that will solve the following differential equations: you
should start by writing a mini-program that contains all of the elements
of the solver, then you can copy and paste the main components into
each program.
Consider the problem
dy
= t + y, y(0) = 1.
dt

(16)

use the Runge-Kutta method, with h = 0.1 to find a solution from


t = 0, 1. Compare the results you get with the actual solution, given
in tabular form below.

t y = 2et 1 t
0.0
1.00000 00
0.1
1.11034 18
0.2
1.24280 55
0.3
1.39971 76
0.4
1.58364 94
0.5
1.79744 25
0.6
2.04423 76
3. Writing the the acceleration of a mass under the influence of Hookes
Law we get the following second order differential equation.
x + 2 x = 0

(17)

k
where 2 = m
. We already know the answer, upto normalization constants and phase angles. The general solution is

x = C1 cos t + C2 sin t

(18)

The initial conditions determine which function cos or sin gives the
final answer. We will let = 1. Then we have the equation
d
dt

x! + x = 0
dx
+x = 0
dt

(19)

Here I have rewitten the equation to show the dependence on t. To use


the Euler or Runge-Kutta method, we need to make this second order
equation into a first order equation. So, lets define y = dx/dt. Then
we get the new set of equations,
dy
= x
dt
dx
= y
(20)
dt
Now we have a set of two coupled first order differential equations,
which we can solve using the Runge-Kutta method. You should be
able to modify the program you wrote to solve number (1) to solve this
problem. Use h = 0.1, 0.01 with y(0) = 1, x(0) = 0 over the range
t = 0, 2, and compare the answer you get to (sin t) evaluated at each
t.
6

I have included my programs in my website, named rungtest.f and rung.f,


that solve problems (1) and (2) respectively, along with the data files that
were output by the programs, rungtest.dat and rung.dat, respectively. Feel
free to look at them and get ideas on how to write your programs.

You might also like