Documente Academic
Documente Profesional
Documente Cultură
Numerical Methods
Part II
D.U.J. Sonnadara
Department of Physics, University of Colombo
26
Ordinary differential
equations
n Many of the laws of physics are expressed in terms of differential
equations. Hence, modeling physical systems require knowledge in
solving differential equations numerically. The most general from of
an ordinary differential equation is a set of M coupled first-order
equations given by;
dy
= f ( x, y )
dx
where x is the independent variable and y is a set of M dependent
variables.
27
Numerical Methods 1
Methods in Computational Physics
Differential equations
n For example, one dimensional motion of a particle of mass m
under the force field F(z) is described by the second-order
equation;
d2z
m = F ( z)
dt 2
dz
n Now, if we define the momentum p (t ) = m then we can
write, dt
dz p ; dp
= = F (z )
dt m dt
28
29
Numerical Methods 2
Methods in Computational Physics
Eulers method
n One of the simplest algorithms is based on Eulers method where
the derivative is represented by its forward difference
approximation;
y n +1 - y n
+ O (h ) = f ( x n , y n )
h
n Now, the recursion relation expressing yn+1 in terms of yn is,
y n+1 = y n + hf ( x n , y n ) + O (h 2 )
n This formula has a local error (i.e. taking a single step from yn to
yn+1) of O(h2) and a global error of O(h) since NO(h2)O(h). Thus,
in order to improve the accuracy in the final answer, step size
should be reduced which in turn increases the number of steps.
30
Example - 6
dy
n Find the value of y at x=3 for the differential equation = - xy
dx
with the boundary condition y ( 0 ) = 1 .
clear;
hh=[0.2 0.02];
1
for k=1:2; The exact solution is: y = exp( - x 2 )
2
h=hh(k); n=3/h; y=1;
for i=0:n-1;
x=i*h; y=y+h*(-x*y);
x=x+h; xx(i+1)=x; y1(i+1)=exp(-0.5*x*x); y2(i+1)=y;
end;
plot(xx,y1); hold on; plot(xx,y2,'.r');
end; hold off;
31
Numerical Methods 3
Methods in Computational Physics
Example 6
y 1
y = exp( - x 2 )
2
h=0.20
h=0.02
x
32
Example 7
n One way to calculate the accuracy of the numerical integration is
to use the final value as the initial condition and to integrate
backward to the starting point.
clear; hh=[0.2 0.02];
for k=1:2;
h=hh(k); n=3/h; y=1;
for i=0:n-1; x=i*h; y=y+h*(-x*y); end;
for j=n:-1:1;
x=j*h; y=y-h*(-x*y);
x=x-h; xx(j)=x; y1(j)=exp(-0.5*x*x); y2(j)=y;
end;
plot(xx,y1); hold on; plot(xx,y2,'.');
end; hold off; 33
Numerical Methods 4
Methods in Computational Physics
Example 7
y 1
y = exp( - x 2 )
2
h=0.02
h=0.20
x
34
Higher accuracy
n Integration methods with a higher accuracy can be derived from the
Taylor series expansion of yn+1 about yn.
h 2 ''
yn +1 = y n + hy n' + yn + O ( h 3 )
2
n From earlier discussions;
df f f dy f f
y n' = f ( x n , y n ) and y n'' = = + = + f
dx x y dx x y
n Thus, by substitution we get;
1 f f
y n +1 = y n + hf n + h 2 n + n f n + O ( h 3 )
2 x y
This recursion relationship has a local error O(h3) and a global error
O(h2), one order better than the Eulers method. This is useful when f
is known analytically and is simple enough to differentiate.
35
Numerical Methods 5
Methods in Computational Physics
Multistep methods
n Another way of achieving higher accuracy is to use recursion
relationship that relate yn+1 not just to yn, but also to points further
in the past, say, yn, yn-1, yn-2etc.
Multistep
n The next higher order formula can be found by substituting the
backward difference expression for the fn.
f n - 2 f n-1 + f n - 2
f n'' =
h2
37
Numerical Methods 6
Methods in Computational Physics
Runge-Kutta method
n One widely used class of methods in solving differential
equations is known as Runge-Kutta methods. To derive a second-
order Runge-Kutta algorithm, approximate f in the integral by its
Taylor series expansion about the midpoint of the interval. Thus,
y n +1 = y n + hf ( x n +1 / 2 , y n +1 / 2 ) + O ( h 3 )
Example - 8
Applying Runga-Kutta 2nd order method to the example - 6.
Numerical Methods 7
Methods in Computational Physics
Example 8
1
y = exp( - x 2 )
2
y y
Forward Backward
x x
40
Numerical Methods 8
Methods in Computational Physics
k1 = hf ( x n , y n );
k 2 = hf ( xn + h / 2, y n + k1 / 2);
k 3 = hf ( xn + h / 2, y n + k 2 / 2);
k 4 = hf ( xn + h, y n + k 3 );
y n +1 = y n +
1
(k1 + 2k 2 + 2k 3 + k 4 ) + O( h 5 )
6
42
Stability
n A major consideration in integrating differential equations is the
numerical stability of the algorithm, i.e. the extent to which round off
or other errors can be amplified.
n For example, consider improving the accuracy in Eulers method by
approximating the derivative directly by the symmetric difference
approximation.
y n+1 = y n-1 + 2hf ( x n , y n ) + O (h 3 )
n Apply this to the problem, dy = - y ; y (0) = 1
dx
whose solution is; y = exp( - x )
Numerical Methods 9
Methods in Computational Physics
Example - 9
n clear;
h=0.1; ym=1; yz=1-h+0.5*h*h; n=6/h;
for j=2:n;
x=j*h; yp=ym-2*h*yz; ym=yz; yz=yp;
x=j*h; xx(j-1)=x; y1(j-1)=exp(-x); y2(j-1)=yz;
end;
plot(xx,y1-y2);
end;
44
Example 9
Exact - y = exp(- x )
predicted
x
45
Numerical Methods 10
Methods in Computational Physics
n Two principal classes of methods used for solving such problems are
discussed in this note.
46
Matrix method
n First divide the region 0 x L into n+1 equally spaced intervals of
length h. The differential equation can now be presented at the point i
by;
y i +1 - 2 yi + y i -1
+ Ayi = B
h2
n Since there are n simultaneous equations, we can multiply by h2 and
written as,
y 2 - 2 y1 + (0) + Ah 2 y1 = Bh 2 a 1 y1 Bh 2
1 a y 2
y 3 - 2 y 2 + y1 + Ah 2 y 2 = Bh 2 1 2 Bh
. . . . = .
(0) - 2 yn + yn-1 + Ah2 yn = Bh2 2
1 a y n Bh
which can be written in the matrix form as shown with a=-2+Ah2. The
coefficient matrix of this set of equations is tridiagonal, and thus can be
solved quite easily and rapidly even if n is large. The error of the
method discussed here is essentially second order. 47
Numerical Methods 11
Methods in Computational Physics
Matrix method ..
n The main disadvantage of this technique is the difficulty in dealing
with nonlinear differential equations. Consider for example,
d2y
+ Dy 2 = E
dx 2
48
Shooting method
n This approach convert a boundary
value problem into an equivalent
initial value problem. Consider the
example used earlier.
y (0) = 0,
d2y
+ Ay = B y ( L) = 0
dx 2
Numerical Methods 12
Methods in Computational Physics
Shooting method
n Seeking the appropriate value of U in order to satisfy the boundary
condition at x=L can be stated as searching for U such that,
y L (U ) = y ( L) = 0
n This is a root finding problem which can be solved by using secant
method.
n We have to provide two estimates of the root; call them U0 and U1.
Now, two solutions of the initial value problem are carried out
yielding yL(U0) and yL(U1). A new estimate of U can be obtained by,
[U1 - U 0 ]
U 2 = U1 - yL (U1 )
[ yL (U1 ) - yL (U 0 )]
Example - 10
clear;
y (0 ) = 0 y (10) = 0
h=0.1; n=10/h;
u1=1; y1=0; z1=u1; %1st guess u1=1
for i=0:n-1;
f1=8-0.25*y1; f2=z1; z1=z1+h*f1;
y1=y1+h*f2;
x(i+1)=(i+1)*h; y(i+1)=y1;
d2y y
end; 2
+ =8
plot(x,y,'.b'); hold on; dx 4
u2=5; y2=0; z2=u2; %2nd guess u2=5
for i=0:n-1; dy
f1=8-0.25*y2; f2=z2; z2=z2+h*f1; = z, y(0) = 0
y2=y2+h*f2; dx
x(i+1)=(i+1)*h; y(i+1)=y2; dz y
end; = 8 - , z (0 ) = U
dx 4
plot(x,y,'.g'); hold on;
51
Numerical Methods 13
Methods in Computational Physics
Example - 10
ux=u2-(y2*(u2-u1))/(y2-y1) y (0 ) = 0 y (10) = 0
dy
= z, y(0) = 0
y L (U1 ) (U1 - U 0 ) dx
U 2 = U1 - dz y
( yL (U1 ) - y L (U 0 )) = 8 - , z (0 ) = U
dx 4
52
d2y dy
+ 9y = 0 y ( 0 ) = 1, (0 ) = 0
dx 2 dt
n Use 4th order RK method with step size of 0.1 and compare with
the exact answer.
53
Numerical Methods 14
Methods in Computational Physics
-1
-2
-3
-4
-5
0 0.5 1 1.5 2 2.5 3 3.5 4
54
y ( 0 ) = 1, y ( ) = 0 0.4
0.2
-0.2
-0.4
-0.6
-0.8
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
55
Numerical Methods 15