You are on page 1of 10

Numerical Solution of ODEs - I

Roychowdhury & Banerjee

The general form of first order differential equation is


y 0 = f (x, y) (1)
where y 0 = dy/dx and f (x, y) is a given function.The solution of this equation contains
an arbitrary constant (the constant of integration).To find this constant, we must know a
point on the solution curve; that is, y must be specified at some value of x, say at x = a.
We write this auxiliary condition as y(a) = α.
An ordinary differential equation of order n
y (n) = f (x,y,y 0 , . . . ,y (n−1) ) (2)
can always transformed into n-first-order equations. Using the notation
y0 = y , y1 = y 0 , y2 = y 00 , . . . , yn−1 = y n−1 (3)
the equivalent first-order equations are
y00 = y1 y10 = y2 y20 = y3 . . . yn0 = f (x,y0 ,y1 , . . . ,yn−1 ) (4)
The solution now requires the knowledge n auxiliary conditions. If these conditions are
specified at the same value of x, the problem is said to be an initial value problem.
Then the auxiliary conditions, called initial conditions, have the form
y0 (a) = α0 , y1 (a) = α1 , . . . yn−1 (a) = αn−1 (5)
If yi are specified at different values of x, the problem is called a boundary value
problem. For example,
00 0
y = −y y(0) = 1 y (0) = 0 (6)
is an initial value problem since both auxiliary conditions imposed on the solution are
given at x = 0. On the other hand,
00
y = −y y(0) = 1 y(π) = 0 (7)
1
2

is a boundary value problem because the two conditions are specified at different values
of x.
In this chapter we consider only initial value problems. The more difficult boundary
value problems are discussed later. We also make extensive use of vector notation, which
allows us manipulate sets of first-order equations in a concise form. For example, Eqs.(5)
and (6) are written as
0
y = F(x, y) y(a) = α (8)
where  
y1
 y 
2
F(x , y) =  ..  (9)
 
 . 
f (x , y)
A numerical solution of differential equations is essentially a table of x- and y-values
listed at discrete intervals of x.

Euler method

The Euler algorithm is the most simplified version of all the higher order numerical
methods that are used for solving ordinary differential equations. The Euler algorithm
provides a simple method which can be applied to the solution of a large class of dynamical
problems. It is based on the Taylor series method, where the first-order Taylor series
integration formula is used. Before we proceed to the Euler scheme, let us take a slight
detour and discuss in brief a bit about the Taylor series method in general.
The Taylor series method is conceptually simple and capable of high accuracy. Its
basis is the truncated Taylor series for y about x:
1 00 1 1 (m)
y(x + h) ≈ y(x) + y0 (x)h + y (x)h2 + y000 (x)h3 + . . . + y (x)hm (10)
2! 3! m!
Since eq.(??) predicts y at x+h from the information available at x, it is also a formula for
numerical integration. The last term kept in the series determines the order of integration.
The truncation error, due to the terms omitted from the series, is
1
E= y (m+1) (ξ)hm+1 , x < ξ < x + h (11)
(m + 1)!
Using the finite difference approximation
y (m) (x + h) − y (m) (x)
y (m+1) (ξ) ≈ (12)
h
3

we obtain the more usable form


hm h (m) (m)
i
E≈ y (x + h) − y (x) (13)
(m + 1)!
which could be incorporated in the algorithm to monitor the error in each integration
step.
The main drawback of the Taylor series method is that it requires repeated differen-
tiation of the dependent variables. These expressions may become very long and are,
therefore, error-prone and tedious to compute. Moreover, there is the extra work of cod-
ing each of the derivatives. However, in the Euler algorithm, there is no such requirement.
The formula for euler method is given by
y(x + h) = y(x) + y 0 (x)h = y(x) + F(x, y)(x, y)h (14)
This is also the basis of Runge-Kutta methods (will be discussed later) and could be
considered to be its first order method.
Let us now take a look at the graphical interpretation of Euler’s formula. For the sake
of simplicity, we assume that there is a single dependent variable y, so that the differential
0
equation is y = f (x, y). The change in the solution y between x and x + h is
Z x+h Z x+h
0
y(x + h) − y(h) = y dx = f (x ,y)dx (15)
x x
0
which is the area of the panel under the y (x) plot, shown in Fig.(1). Euler’s formula
approximates this area by the area of the cross-hatched rectangle. The area between the
rectangle and the plot represents the truncation error. Clearly, the truncation error is
00
proportional to the slope of the plot; that is, proportional to y (x). The error can be
reduced with a judicious choice of h.
4

In what follows we would first describe this algorithm applied to a ball thrown vertically
upwards in a uniform gravitational field, which is a second order differential equation.
A scheme to solve second order differential equations numerically is to write them as a
set of two coupled first order differential equations. For example,
dy1
= f (y1 , y2 , x)
dx
dy2
= g(y1 , y2 , x)
dx
Let us apply this to a simple problem in physics now which is goverened by a second order
differential equation. As we know, Newton’s Law is a second order ordinary differential
equation, which means that if we know the position x0 and speed v0 of a particle at any
time t0 , we can work out the trajectory for all future times. Let us see how this works
out for a time point t1 = t0 + ∆t. If ∆t is small the speed can be taken to be effectively
constant within the time interval (t0 , t1 ). The ‘new’ position of the particle at time t1
can then be computed using the steps:
x(t0 + ∆t) = x(t0 ) + v0 ∆t
The ‘new’ speed will be determined by the accelaration of the particle at the initial time
a0 = a(t0 ) = F (t0 )/m
v(t0 + ∆t) = v(t0 ) + a0 ∆t
The value of F0 is supplied by the RHS of the Newton’s law. Since the law is second order
in the time derivative, no higher derivatives need to be computed for determining the
dynamical state of the particle for any future time, and the process can be continued for
5

all subsequent time points tn = t0 + n∆t. The following code (attached) would integrate
the equation of motion of the ball:
#script: ball1.py
#Demonstrates the Euler algorithm
#for integrating the equation of
#motion of a ball thrown upwards
#from random import randint
f1=file(’ball1py1.txt’,’w’)
f2=file(’ball1py2.txt’,’w’)
g=-9.8 #m/s^2
t0=0;y0=0;v0=3 #m/s
Dt=.01 #sec

t=t0
y=y0
v=v0
set xlabel ’time (t) (sec)’
while t <= 5: set ylabel ’height (h) (m)’
y1=y0+v0*t + 0.5*g*t**2 set title ’Tracking a Ball thrown \
v1=v0+g*t upwards with speed 3m/s’
print >>f1, t,y1 plot ’ball1py1.txt’ using 1:2 ps 3 \
print >>f2, t,v1 title ’Simulation’, ’ball1py1.txt’ \
y=y+v*Dt using 1:3 w l lw 2 \
v=v+g*Dt title ’Theory’
t=t+Dt
f1.close()
f2.close()
Note that we have not included any libraries to preserve the essential simplicity of the pro-
gram. The code generates the data and writes it out to a data file, which is subsequently
plotted with gnuplot. The match with the theory appears to be quite robust.
The Euler method may be very easy to implement but it can’t give accurate solutions.
A very small step size is required for any meaningful result. In this scheme, since, the
starting point of each sub-interval is used to find the slope of the solution curve, the
solution would be correct only if the function is linear. So an improvement over this is to
take the arithmetic average of the slopes at xi and xi+1 (that is, at the end points of each
sub-interval). The scheme so obtained is called modified Euler’s method. It works
first by approximating a value to yi+1 and then improving it by making use of average
6

slope.

yi+1 = yi + h/2 (yi 0 + yi+1 0 )


= yi + h/2(f (xi , yi ) + f (xi+1 , yi+1 )) (16)
If Euler’s method is used to find the first approximation of yi+1 then
yi+1 = yi + 0.5 h (fi + f (xi+1 , yi + hfi )) (17)
The error here is proportional to y 000 and thus this is a second order method.
7

Exercise 1

dx
1. Consider, the equation = x , x = 1 at t = 0.
dt
(a) Solve this using the Euler method for t = 0 . . . 1 and store the solution in the
vectors T and X. Plot the numerical solution vs. the exact solution for dt = 0.1.
(b) Modify the above program to calculate the value of e ≡ exp(1) with the following
step sizes dt = 0.1 ,0.05 ,0.01 ,0.005 ,0.001 ,0.00001. Calculate the errors for every
step size. Plot the numerical absolute error computed with respect to exp(1) for
each step size. Verify, by examining the graph, that the errors are proportional
to the step sizes.
** Next work out the above mentioned differential equation using the modifed euler
method. Check for the convergence of the solution at t = 1 for different values of
dt. Compare with the euler method and comment on their respective accuracy.

2. Solve the differential equation for the number of radio-active nuclei remaining in a
sample with decay constant λ = 0.2 for t = [0, 15] using Euler’s method. Do this
for dt = [1, 0.1, 0.01, 0.001] and plot all the resulting data in a single image with
proper labels and legends. Comparing with the analytical solution of the differential
equation, comment on the accuracy of the Euler method with respect to dt. Repeat
the entire exercise with the modified-Euler method.
3. Plot the dataset obtained in the program given in the factsheet again, this time for
t = 0 . . . 1.2 sec. You would get something like the following figure.
8

Where has all the “perfect match” disappeared? Fix the code by changing ∆t from
0.1 sec to 0.01 sec and running the code again. What did you learn from it?
4. Assume that the ball is subjected to random impulses due to air currents in the
vertical direction. Supposing that one can model the effect of the air current using
the following modified update equation for its speed, track the motion of the ball for
5 seconds.
v = v0 + gt + randint(−1, 1) × 1.2
The randint() function returns one of the values -1,0,+1 randomly. Compare results
using graphs for position and speed. You can get the randint module from the
random module.
5. Track down a particle falling through a viscous medium for 10 seconds. This is easier
than pre-calculating the time steps required to reach a specified end-time. In this
case the acceleration is computed using:

a = g − (k/m) ∗ v

Take the mass of the particle to be 1.0 kg, and unity for the drag parameter. Plot
the speed versus time. Since there will be a lot of data, you can also choose to sample
every 10th data for plotting using the every clause:
plot drag1.dat using 1:3 every 10 w l in Gnuplot . Compare the plot with
the exact solution v(t) = g ∗ (1 − e−t ). (You can sample one point every 100 points
now). Can you read the terminal speed from the graph? From the dataset ?
6. The problem is same as above. However, this time we would like to observe how the
terminal speed is actually reached by plotting the difference of the terminal speed
and the instanteneous speed vT − v (vT ≡ mg/k ≡ g) vs. time.
(a) fortunately, we don’t have to run our program again since Gnuplot can trans-
form the dataset for us. Check it out by modifying the plot . . . using clause to
plot. . . using 1:(9.8-$3).
(b) Plot now the dataset log(vT − v) vs. time. What is the nature of the graph?
What does it imply for the functional relationship between v and t?
(c) Fit the curve to a straight line ax + b and estimate the parameters a and b. How
do they compare with exact values (get them from the exact solution in the above
problem. Note that you must use the using clause with the fit command to
pick up the required dataset.
9

7. To investigate oscillations using the Euler Method, just replace the prescription for
calculating the acceleration. This time the accelaration depends on x rather than on
v. Write out a program for a particle moving in 1 dimension under a force F = −kx.
Fix up the values of parameters and the starting conditions yourself. Examine the
x − t, v − t and the phase plot v − x. Now, add a constant force to the oscillatory
component and observe its effect on the motion of the particle. How does it change?

Note: You can stack multiple plots on the same page by enclosing the plot com-
mands within set multiplot and unset multiplot commands in Gnuplot . Use
the following code snippet to create your own:

set multiplot layout 2,2 title ’4 plots’


set title ’sin’
plot sin(x)

set title ’cos’


plot cos(x)

set title ’tan’


plot tan(x)

set title ’exp’


plot exp(x)
unset multiplot

8. Modify question (7) to include the effect of damping (add a −γv/m term to the
acceleration term) and observe the motion for several cycles. Generate all three
plots. How does the phase diagram differ in this case from the previous case? Repeat
the exercise with the modified-Euler method.

References

1. Numerical Methods in Engineering with Python, Jaan Kiusalaas, Cambridge Univer-


sity Press.

2. Scientific Computing in python, Abhijit Kar Gupta, Techno World.


10

Acknowledgements

The authors would like to thank Ms. Roopkatha Banerjee for helping with designing and
typesetting the solutions.

You might also like