CH 9, Math 5 - Lecture - Note Summer 23 24
CH 9, Math 5 - Lecture - Note Summer 23 24
CH 9, Math 5 - Lecture - Note Summer 23 24
9.1 Introduction
Any higher order differential equation can be expressed as a system of first order equations
and the method of solution for first order equation can extended for the system.
To describe various numerical methods for the solution of ordinary differential equations,
we consider the general first order differential equation.
dy
=f (x , y )
dx (1)
with the initial condition
y ( x 0 )= y 0 .
Consider the Taylor series solution of the of the initial value problem
dy
=f (x , y )
dx with y ( x 0 )= y 0
The solution of the equation is a function of x. The Taylor series expansion of y ( x ) about
x 0 is
' h2 ' ' h 3 '' ' h 4
y ( x 0 +h)= y 0 +h y 0 + y 0 + y 0 + y (iv )+⋅⋅⋅¿ ¿
2! 3! 3! 0
dn y
(n )
where y 0
n
is the value of dx at
x=x 0 .
The values of the derivatives can be found by differentiating repeatedly and substituting
x=x 0 and y= y 0 .
The method can also be used for higher order differential equations.
8-1
Summer 2023-2024
powers of h we have
'
y ( x 0 + h)≈ y ( x 0 )+ h y ( x 0 )
or equivalently
y 1 = y 0 +hf ( x 0 , y 0 )
2
This is Euler’s formula, and the order of the error is Ο(h ) .
x ≤x≤x 1 , we have
Assuming that f ( x , y )=f ( x 0 , y 0 ) in 0
y 1 = y 0 +hf ( x 0 , y 0 ) whereh=x 1−x 0
The process can be continued for the next interval by taking x 1 and y 1 as the starting
values. In general , we have the iteration formula
y n+1 = y n +hf ( x n , y n ) n=0,1,2,⋅¿⋅¿ ¿
8-2
Summer 2023-2024
If we use y 1 = y 0 +hf ( x 0 , y 0 ) on the right side of the modified Euler’s formula, we have
h
y 1= y 0+
2
[ f ( x 0 , y 0 )+ f ( x 1 , y 0+hf ( x 0 , y 0 )]
Setting
k 1=hf (x 0 , y 0 )
k 2=hf ( x0 +h , y 0 + k 1 )
1
y 1 = y 0 + [ k 1 +k 2 ]
we have 2
This is known as the second order Runge-Kutta formula.
It can be shown that RK-2 method is equivalent to Taylor series of order two and the order
3
of the error is Ο(h ).
The fourth order Runge-Kutta formula, the most commonly used one in practice, is stated
without proof.
1
y 1= y 0+ [ k + 2 k 2 +2 k 3 +k 4 ]
6 1
where,
k 1=hf (x 0 , y 0 )
h k1
k 2=hf ( x0 + 2 , y 0 + )
2
h k2
k 3= hf ( x 0 + 2 , y 0 + )
2
k 4 =hf ( x 0 + h , y 0 +k 3 )
Note that the process is not unique, and many other variations are possible. In fact, the
fourth order process is very accurate and most frequently used. This formula is equivalent to
5
Taylor series of order four and the order of the error is Ο(h ) .
So far, we have considered the method for a single differential equation but those methods
can be extended for system of first order equation.
Consider a system of pair of equations
'
y =f 1 ( t , y , z )
'
z =f 2 ( t , y , z )
subject to initial conditions y (t 0 )= y 0 and z (t 0 )=z 0 .
8-3
Summer 2023-2024
The second order Runge-Kutta (RK-2) method to the above system can be written in the
form
k 1=hf 1 (t 0 , y 0 , z 0 ) m1 =hf 2 (t 0 , y 0 , z 0 )
k 2=hf 1 (t 0 +h , y 0 +k 1 , z 0 +m1 ) m2 =hf 2 (t 0 +h , y 0 +k 1 , z 0 +m1 )
and
y 1 = y 0 + 12 [ k 1 + k 2 ]
z 1=z 0 + 12 [ m1 + m2 ]
In a similar way the RK-4 method can be extended for system of equations.
The Taylor series solution for higher order differential equations is straightforward and like
first order equation.
An ordinary differential equation of order higher than 1 can be solved numerically by
changing it into a system of first order equations. Consider, for example, a second order
initial value problem
with y ( x 0 )=c 0 and y ( x 0 )=c 1
'
y ' ' =f ( x , y , y ' )
Defining the new variable
'
y =z
the above initial value problem can be written as
y ' =z
z ' =f ( x , y , z )
with y ( x 0 )=c 0 and z ( x 0 )=c 1 .
Taking n=2,
Solution =
0 1.0000
8-5
Summer 2023-2024
0.2000 0.8484
0.4000 0.7644
0.6000 0.7257
0.8000 0.7275
1.0000 0.7799
1.4
(ii) 1.1
0.9
>> [x2, y2]=ode23(f, xin,1);
0.8
Exercise 9.2
Given the initiaal value problem
dy dz
=x+ y 2−z =x 2−3 y + z 2
dx , dx with y (1)=2 and z(1)=2. 5 .
(a) Estimate y (1 .2 ) and z(1. 2) using the RK-2 method with step sizeh=0 .1 .
(b) Use MATLAB function “[x1, y1]=ode45(F, [x0, xn], y0)”
(i) to estimate the values of y in 1 ≤ x ≤1.4 using h=0.1.
(ii) to plot the solution curve in [1, 1.4 ].
Solution
(a) Taking f ( x, y , z )=x+ y 2 −z , g( x , y , z)=x 2 −3 y+z 2 and x 0 =1 , y 0 =2 ,
z 0 =2. 5 with h=0 .1 , we have
Now starting with x 1=1 . 1 , y 1 =2. 3019 , z 1=2. 6301 and h=0 .1 , we have
k 1=0 . 1 f (1 .1 , 2. 3019 , 2 .6301 )=0 . 3769 m1 =0 .1 g(1. 1 , 2. 3019 , 2. 6301 )=0 .1222
k 2=0 . 1 f (1 .2 , 2 .6788 , 2 .7523 )=0. 5624 m2 =0 .1 g(1 .2 , 2 .6788 , 2 .7523 )=0 . 0979
1 1
y 2 =2 .3019+ 2 ( 0. 3769+0 . 5624 ) z 2 =2. 6301+ 2 ( 0 . 1222+ 0 .0979 )
=2.7716 = 2.7402
Thus
y (1 .2 )≈ y 2 =2 .7716 and z (1. 2)≈z 2 =2. 7402 .
[ ][ ] [ ][ ]
2
y '1 = x+ y 1− y 2 with y1 ( 0 ) = 2 .
y2 ' x 2−3 y 1 + y 22 y 2 (0) 2.5
Sxyz =
4.5
(ii) 4
>> plot(x2,y2) 3
2.5
2
1 1.05 1.1 1.15 1.2 1.25 1.3 1.35 1.4
Example 9.3
dI 1 dV −1 1
= V, = I− V.
dt L dt C RC
where I is the current through the inductance and V is the voltage drop across the capacitor.
1
Suppose that R= 1 ohm, C= farad∧L=1henr y.
2
Use MATLAB command “[x1, y1] =ode45(F, [x0, xn], y0)” to find the numerical solution
for t ∈ [ 0 ,5 ] if I ( 0 )=2 amperes and V( 0 )=3 volts .
Plot your results I (t) and V (t ) separately for distributions.
Solution:
[ ][ ] [ ][]
'
y1 = y2 with y 1 ( 0 ) = 2 .
y 2 ' − y 1− y 2 y 2(0) 3
8-7 2.5
2
I(t)
1.5
0.5
Summer 2023-2024
>> clear
>> F=@(t,y) [y(2); -y(1)-y(2)]
F = @(t,y)[y(2);-y(1)-y(2)]
>> [t,y]=ode45(F,[0,5],[2,3]);
>> plot(t,y(:,1))
>> xlabel('t'); ylabel('I(t)');
>> title('Distribution of I(t)');
>> plot(t,y(:,2))
>> xlabel('t'); ylabel('V(t)');
>> title('Distribution of V(t)');
Solution
(a) Using central difference formulas, we have
1 1
2 ( n +1
y −2 y n+ y n−1 )− ( y n +1− y n−1 )=1
h 2h
1 3
Taking h= , we get 9 ( y n +1−2 y n+ y n−1 )− ( y n +1− y n−1 )=1
3 2
On simplification, 21 y n−1−36 y n +15 y n+1=2
8-8
Summer 2023-2024
1 2
(b) With h=1/3, the nodal points are x 0=0 , x 1= , x 2= , x3 =1.
3 3
1
For n=1 , x 1= , y 0=1 ,
3
−36 y 1 +15 y 2=2−21=−19 ,(1)
2
For n=2 , x 2= , y 3=2(e−1) ,
3
21 y 1−36 y 2=2−30(e−1)=−49.5485 ,(2)
Solving the equations (1) and (2), we have
y 1=1.4548∧ y 2=2.2250
(c) Using y 1= y∧ y 2= y ' , we can write the system as follows:
[ ][ ] [ ][ ]
'
y 1 = y 2 with y 1 ( 0 ) = 1 .
y2 ' 1+ y 2 y 1 (1) 2(e−1)
(d)
>> clear
>> F=@(x,y) [y(2); 1+y(2)]; % dy/dx = F(x, y)
>> bc=@(ya,yb) [ya(1)-1; yb(1)-2*(exp(1)-1)]; % format boundary values
>> yinit=@(x) [1; 4]; % initial guess value supplied
>> solinit=bvpinit(linspace(0,1,3), [1,2]); % generates starting values
>> sol=bvp4c(F, bc, solinit); % solution
0 1.0000 3
0.3333 1.4579
0.6667 2.2283 2.5
1.0000 3.4366 2
>> xval=linspace(0,1); 1
>> yval=deval(sol,xval); 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
8-9
Summer 2023-2024
Exercise 9
8 - 10
Summer 2023-2024
(d) Estimate a value of q (1. 2) using the Runge-Kutta mehod of order two.
(e) Use MATLAB function “[x1, y1]=ode45(F, [x0, xn], y0)”
(i) to estimate the values of y in 1 ≤t ≤2 using h=0.2.
(ii) to plot the solution curve in [1, 2].
7. Solve the following boundary value problems using the finite difference method and
central difference approximations.
'' x 1
(a) y − y=4 x e y ( 0 )=0 , y ( 1 )=1 with h= .
4
''
(b) y =xy y ( 0 )=0 , y ( 1 ) =1 withh=0.25 .
8 - 11