Sunteți pe pagina 1din 15

Methods in Computational Physics

Methods in Computational Physics


PH 4002

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.

n Differential equations of higher order can be written in this first-


order form by introducing various auxiliary functions.

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

which are in the correct format discussed earlier. Thus, we need


to consider only methods for solving the first-order equations.

28

Initial value problems


n We can classify two categories for all problems involving ordinary
differential equations. These are the initial value problems and
boundary value problems.

n We are interested in finding the value of y at a particular value of


x, say x=1 when the initial condition, say y=y0 at x=0 is given.

n This can be achieved by dividing the interval into a large number,


n, of equally spaced subintervals of length h=1/n and then to
develop a recursion formula relating yn to [yn-1, yn-2, .], where yn
is our approximation to y at xn=nh.

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.

n To derive such a formula (Adams-Bashforth formulas), we start with


Taylor series expansion of yn+1 about yn;
h 2 '' h 2 ' h 3 ''
y n +1 = y n + hyn' + y n + O(h 3 ) = yn + hf n + fn + f n + ...
2 2 3!
n By replacing the derivative by backward difference, we obtain;
h 2 f n - f n -1 h 3 ''
y n +1 = yn + hf n + [ ]+ f n + ...
2 h 3!
3 1
y n +1 = y n + h[ f n + f n -1 ] = O( h 3 ) - 2nd order formula.
2 2 36

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

n Substituting this expression to the earlier equation we get;


23 16 5
yn +1 = y n + h[ fn - f n -1 + f n - 2 ] + O (h 4 )
12 12 12

which is of third order.

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 )

n Now, if we define k to be an intermediate approximation to twice


the difference between yn+1/2 and yn, the following two step
procedure can be used to give yn+1 in terms of yn.
k = hf ( x n , y n );
y n +1 = y n + hf ( x n + h / 2, y n + k / 2 ) + O ( h 3 )
n This is a second order Runge-Kutta algorithm which requires to
evaluate f twice for each step. 38

Example - 8
Applying Runga-Kutta 2nd order method to the example - 6.

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; k1=h*(-x*y); y=y+h*(-(x+(h/2))*(y+(k1/2)));
%x=x+h; xx(i+1)=x; y1(i+1)=exp(-0.5*x*x); y2(i+1)=y;
end;
for j=n:-1:1;
x=j*h; k1=-h*(-x*y); y=y-h*(-(x-(h/2))*(y+(k1/2)));
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,'.r');
end; hold off; 39

Numerical Methods 7
Methods in Computational Physics

Example 8
1
y = exp( - x 2 )
2
y y
Forward Backward

x x

40

RK: 3rd order


n Runge-Kutta schemes of higher order can be derived by using
better formulas to approximate the integral than Eulers method.
For example, Simpsons rule yields;
h
y n +1 = y n +[ f ( xn , y n ) + 4 f ( xn +1 / 2 , y n +1 / 2 ) + f ( x n+1 , y n +1 )] + O (h 5 )
6
n Thus, a third-order algorithm can be written as;
k1 = hf ( x n , y n );
k 2 = hf ( x n + h / 2, y n + k1 / 2);
k 3 = hf ( x n + h, y n - k1 + 2k 2 );
y n +1 = y n + (k1 + 4 k 2 + k 3 ) + O( h 4 )
1
6
which requires evaluation of f three times for each step.
41

Numerical Methods 8
Methods in Computational Physics

RK: 4th order


n A fourth order algorithm which requires f to be evaluated four times
for each step can be written as;

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 )

n Now, to start the recursion relation, we need to calculate y1 as well as


y0=1. This can be done by using the Taylor series expansion of exp(-h).
h2
y1 = 1 - h + + O(h 3 )
2 43

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;

n The output of the program shows that for small values of x,


numerical solution is slightly higher than the exact value. However,
when x increases (Example: x=3.5), an oscillation begins to
develop. This is a result of an instability in the algorithm.

44

Example 9

Exact - y = exp(- x )
predicted

x
45

Numerical Methods 10
Methods in Computational Physics

Boundary value problems


n Two point boundary value problems are defined as a set of one or
more differential equations with associated boundary conditions
specified at two different values of the independent variable.

n Consider the following example to illustrate the two state boundary


value problems.
d2y
+ Ay = B
dx 2
y (0) = 0, y ( L) = 0

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

n Using the same central difference approach, we obtain,


y i +1 - 2 y i + y i -1
+ Dyi2 = E
h 2

n If this equation is written for the sub-intervals of interest, a set of


nonlinear equations will result which must be solved by iterative
techniques.

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

n This problem can be recast as the


following initial value problem.
y (0) = 0,
d2y
+ Ay = B dy
dx 2 (0) = U
dx
where U is unknown and must be
chosen such that y(L) = 0. 49

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 )]

n The process is continued until a suitable solution is found.


50

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

u2=ux; y2=0; z2=u2; %trial value = ux


for i=0:n-1;
f1=8-0.25*y2; f2=z2; z2=z2+h*f1;
y2=y2+h*f2;
x(i+1)=(i+1)*h; y(i+1)=y2; d2y y
end; + =8
dx 2 4
plot(x,y,'.r'); hold off;

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

Class Work (1)


n Solve the initial value problem in the range x = 0 to 4;

d2y dy
+ 9y = 0 y ( 0 ) = 1, (0 ) = 0
dx 2 dt

n By using Eulers method with step size of 0.1.

n If the exact solution is given by y = cos 3t , study how reducing


step size can reduce error growth.

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

Class Work (1)


4

-1

-2

-3

-4

-5
0 0.5 1 1.5 2 2.5 3 3.5 4

54

Class Work (2)


n Solve the boundary value problem given below.
1
d2y dy
+ 2x =0 0.8
dx 2
dx 0.6

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

S-ar putea să vă placă și