Spring 2023-2024
Numerical Methods for Science and Engineering
Lecture Note 9
Ordinary Differential Equations
9.1 Introduction
We shall consider the solution of ordinary differential equations satisfying certain
conditions. Problems in which all the initial conditions are specified at one point only are
called initial value problems (I.V.P.). On the other hand, problems involving second and
higher order differential equations, we may prescribe the conditions at two or more points.
Such problems are called boundary value problems (B.V.P.).
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 .
9.2 Taylor Series Solution
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
Spring 2023-2024
9.3 Euler Method
Consider the solution of the initial value problem
dy
=f (x , y )
dx with y ( x 0 )= y 0 (1)
From Taylor series expansion of y ( x 0 +h) and neglecting terms containing h and higher
2
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 ) .
Alternatively, integrating (1) from x0 tox1 with respect to x, we have
x1
y( x 1 )= y( x 0 )+∫ x f (x , y)dx
0
x1
y 1 = y 0 +∫ x f ( x , y )dx
or 0
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,⋅¿⋅¿ ¿
9.4 The Modified Euler’s Formula and Predictor-Corrector Method (Omitted)
Integrating the DE (1) from x0 tox1, we have
x1
y 1 = y 0 +∫ x f ( x , y )dx
0
Using trapezoidal rule to the integration we obtain the modified Euler’s formula
h
y 1= y 0+
2
[ f ( x 0 , y 0 )+ f ( x 1 , y1 )]
An iterative formula to the above equation can be taken as
h
y
1
( n+1)= y n+
2 [
f ( x0 , y 0 )+ f ( x 1 , y (n ) )
1 ]
y ( 0)
The iteration can be started by choosing 1 from the Euler’s formula
y ( 0)≈ y 1 = y 0 + hf ( x 0 , y 0 )
1
This procedure is known as one-step predictor-corrector method.
8-2
Spring 2023-2024
9.5 The Runge-Kutta Method
9.5.1 Second Order Runge-Kutta Method (RK-2 Method)
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 ).
9.5.2 Fourth Order Runge-Kutta Method (RK-4 Method)
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 ) .
9.6 System of Equations
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 .
(a) Euler’s Method for a System
8-3
Spring 2023-2024
Euler’s method to the above system can be written as
y 1 = y 0 +hf 1 (t 0 , y 0 , z 0 )
z 1=z 0 +hf 2 (t 0 , y 0 , z 0 )
(b)Rk-2 Method for a System
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 RK-4 method can be extended for system of equations.
An ordinary differential equation of order higher than 1 can be solved numerically by
changing it into a system of first order equations.
9.7 Higher Order Differential Equations
The Taylor series solution for higher order differential equations is straightforward and
similar to 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 .
Example9.1 :Given that y ' =2 x y 2− y , where y=1 at x=0.
(a) Estimate the values of y (0.2) using Euler’s method with step size h=0.1.
(b) Estimate the values of y (0.2) using the Runge-Kutta method of order two.
(c) Estimate a value of y (0.2) using theRunge-Kutta method of order four.
(d) (i) Use three point central difference formula for derivative to derive a recurrence
relation for the above IVP.
(ii) Estimate the values of y at x=0.4∧0.6 using .this
recurrence relation.
(e) Use MATLAB function “[x1, y1]=ode23(f, [x0, xn], y0)”
(i) to estimate the values of y in 0 ≤ x ≤ 1.4 using h=0.2.
(ii) to plot the solution curve in [0 ,1.4 ].
Solution (a) Here f ( x , y )=2 x y 2− y and x 0=0 , y 0=1 .
8-4
Spring 2023-2024
Taking h=0.1, we have
y ( 0.1 , 0.1 )=1+0.1 f ( 0 , 1 )=1+0.1 (−1 )=0.9 .
2
y ( 0.2 , 0.1 )=0.9+0.1 f ( 0.1 , 0.9 ) =0.9+0.1[2(0.1)(0. 9 )−0.9]=0.8262 .
(b) Taking f ( x, y)=2xy 2 − y and x 0 =0 , y 0 =1 , h=0 .2 , we have
k 1=0 . 2 f (0 , 1)=−0 .2
k 2=0 . 2 f (0 . 1 , 0. 8 )=−0 . 1088
y (0 . 2 , 0 . 2)=1+ 12 (−0 . 2−0 .1088 )=0. 8456
(c) Taking f ( x, y)=2xy 2 − y and x 0 =0 , y 0 =1 , h=0 .2 , we have
k 1=0 . 2 f (0 , 1)=−0 .2
k 2=0 . 2f (0+ 02. 2 , 1+ −0.2 2 )=0.2 f (0.1, 0 . 9)=−0 .1476
k 3=0. 2 f (0+ 02. 2 , 1+ −0 .1476
2
)=0 . 2 f (0 . 1, 0 . 9262)=−0 .1509
k 4 =0 . 2 f (0+0 . 2 , 1−0 .1121)=0. 2 f ( 0. 1 , 0 .8491 )=−0 . 1121
y(0.2, 0.2)= y 1 =1+ 16 (−0.2−0.1376−0.1579−1121)=0.8485
(d) (i) Using three point central difference formula for derivatives with the differential
equation at (x n , y n) we have
' y n +1− y n−1 2
yn ≈ =2 x n y n− y n
2h
y n +1= y n−1+ 2 h ( 2 x n y 2n− y n )
(ii) For h=0.2 with the starting values x 0=0 , y 0=1∧x 1=0.2 , y1 =0.8485, we get
Taking n=1,
y (0.4 )≈ y 2= y 0+ 2 ( 0.2 ) ( 2 x 1 y 21− y 1)
¿ 1+0.4 [ 2 ( 0.2 ) ( 0.8485 ) −0.8485 ]=0.7758
2
Taking n=2,
y (0.6) ≈ y 3= y 1 +2 ( 0.2 ) ( 2 x2 y 22− y 2 )
¿ 0.8485+ 0.4 [ 2 ( 0.4 ) ( 0.7758 ) −0.7758 ]=0.6099
2
(e) (i) >> clear all
>> f=@(x,y) 2*x*y^2-y;
>> [x1, y1]= ode23(f, [0:0.2:1.4], 1);
>> Solution = [x1,y1]
Solution =
8-5
Spring 2023-2024
0 1.0000
0.2000 0.8484
0.4000 0.7644
0.6000 0.7257
0.8000 0.7275
1.0000 0.7799
1.4
1.2000 0.9253 1.3
1.4000 1.3401 1.2
(ii) 1.1
>> xin=0:0.02:1.4; % generate points with sm 1
0.9
>> [x2, y2]=ode23(f, xin,1);
0.8
>> plot(x2,y2) 0.7
0 0.2 0.4 0.6 0.8 1 1.2 1.4
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
k 1=0 . 1 f (1 , 2 , 2 . 5)=0. 25 m1 =0 .1 g(1 , 2, 2 .5 )=0. 125
k 2=0 . 1 f (1 .1 , 2 .25 , 2 .625 )=0. 3538 m2 =0 .1 g(1 .1 , 2. 25 , 2 .625 )=0 . 1351
1 1
y 1 =2+ 2 ( 0 . 25+0 .3538 )=2 . 3019 z 1=2. 5+ 2 ( 0 . 125+0 .1351 )=2 .6301
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 .
8-6
Spring 2023-2024
(b) Using y 1= y∧ y 2=z, we can write the system as follows:
[ ][ ] [ ][ ]
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
Entry in command window;
(i) >> clear
>> F=@(x,y) [x+y(1)^2-y(2); x^2-3*y(1)+y(2)^2];
>> [x1,y1]=ode45(F, [1:0.1:1.4], [2, 2.5]);
>> Sxyz = [x1,y1]
Sxyz =
1.0000 2.0000 2.5000
1.1000 2.3091 2.6241
1.2000 2.8009 2.7112 6
1.3000 3.6825 2.6368 5.5 y
1.4000 5.6417 2.0508
z
5
4.5
(ii) 4
>> [x2,y2]=ode45(F, [1:0.01:1.4], [2, 2.5]); 3.5
>> 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
An LCR circuit can be described by the system of differential equations
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:
Using y 1=I ∧ y 2=V , we can write the system as follows:
[ ][ ] [ ][]
'
y1 = y2 with y 1 ( 0 ) = 2 .
y2 ' − y 1 − y 2 y 2(0) 3
8-7
Spring 2023-2024
Entry in command window; 3.5
Distribution of I(t)
>> clear 2.5
>> F=@(t,y) [y(2); -y(1)-y(2)] 2
I(t)
1.5
1
F = @(t,y)[y(2);-y(1)-y(2)] 0.5
>> [t,y]=ode45(F,[0,5],[2,3]); -0.5
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
t
>> plot(t,y(:,1)) 3
Distribution of V(t)
>> xlabel('t'); ylabel('I(t)');
2.5
>> title('Distribution of I(t)'); 1.5
V(t)
>> plot(t,y(:,2))
0.5
>> xlabel('t'); ylabel('V(t)'); -0.5
>> title('Distribution of V(t)'); -1
-1.5
-2
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
t
Example 9.4: Consider the boundary value problem
'' '
y − y =1 , y ( 0 )=1 , y (1)=2 ( e−1 )
(a) Derive a recurrence relation using three points central difference approximations
with h=1/3.
(b) Using the above finite difference formula solve the above BVP.
(c) Express the above initial value problem as a system of first order equations.
(d) Use MATLAB function “sol=bvp4c(odefun, bcfun, solinit)” to the BVP.
(i) Estimate the values of y at x=¿ . 1/3, 2/3, 0.4 and 0.9.
(ii) Plot the solution curve.y.
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
8-8
Spring 2023-2024
On simplification, 21 y n−1−36 y n +15 y n+1=2
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
(i) % Extracting values from solution “sol”
>> xint=[0, 1/3, 2/3, 1];
>> sxint=deval(sol,xint)
Val_xy =
3.5
0 1.0000 3
0.3333 1.4579
0.6667 2.2283 2.5
1.0000 3.4366 2
(ii) % Plotting of y (x ). 1.5
>> 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
>> plot(xval, yval(1,:))
8-9
Spring 2023-2024
Exercise 9
1. Given the initial value problem y ' =2 x 2− y +3 y 2 with y ( 2 )=0.5 .
(a) Estimate the values of y (2.2) using Euler’s method with step size h=0.2.
(b) Estimate the values of y (2.6) using the Runge-Kutta method of order two.
(c) Estimate a value of y (2.8) using theRunge-Kutta method of order four.
(d) (i) Use three point central difference formula for derivative to derive a recurrence
relation for the above IVP.
(ii) Estimate the values of y at x=3.0∧3.2 using .this recurrence relation.
(e) Use MATLAB function “[x1, y1]=ode23(f, [x0, xn], y0)”
(i) to estimate the values of y in 1≤ x ≤ 4 using h=0.2.
(ii) to plot the solution curve in [1, 4].
2. Given the initial value problem
' 2
y =2 x+ x y with y ( 0 ) =−1.
(a) Estimate the values of y (0.2) using the Runge-Kutta method of order two.
(b) Estimate a value of y (0.2) using theRunge-Kutta method of order four.
(c) (i) Use three point central difference formula for derivative to derive a recurrence
relation for the above IVP.
(ii) Estimate the values of y at x=0.4∧0.6 using .this recurrence relation.
(d) Use MATLAB function “[x1, y1]=ode45(f, [x0, xn], y0)”
(i) to estimate the values of y in 0 ≤ x ≤ 1.5 using h=0.1.
(ii) to plot the solution curve in [0 ,1.5 ].
3. Given the system
dy dz
= y 2 + xz + x 2 = y +2 x + yz
dx and dx
with the initial conditions y ( 1 )=−1 and . z (1 )=1.
(a) Estimate y(1 .1 ) and z(1.1) using Runge-Kutta method of order two.
(b) Use central difference formula to derive a recuurence formula and estimate the
values of y ( 1.2 )∧z (1.2).
(c) Use MATLAB function “[x1, y1]=ode45(F, [x0, xn], y0)”
(i) to estimate the values of y in 1 ≤ x ≤1.5 using h=0.1.
(ii) to plot the solution curve in [1, 1.5].
4. The equation for a LCR circuit with applied voltage E is
2
d q dq 1
L 2 + R + q=E
dt dt C
dq
where L=1 H , R=10 Ω , C=5 F and E=20 V given that q=1 and =2 att=1.
dt
(a) Use three point central difference formula for derivatives to derive a recurrence
relation for the above IVP.
(b) Use (a) and (b) to estimate the values of y at t¿ 1.4∧1.6.
8 - 10
Spring 2023-2024
(c) Express the above initial value problem as a system of first order differential
equations.
(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].
5. Consider the boundary value problem
'' '
y −2 y +2 y=x , y ( 0 )=1 , y (1)=2.
(a) Derive a recurrence relation for the above differential equation using three point central
difference formula for derivative with h=1/3.
(b) Using the above finite difference formula solve the above boundary value problem.
(c) Express the above initial value problem as a system of first order differential
equations.
(d) Use MATLAB function “sol = bvp4c(odefun, bcfun,solinit)” to solve the BVP.
(i) Estimate the values of y at x=1 /3∧2/3.
(ii) Plot the solution curve y (x )’
6. Consider the following boundary value problems
(i) y ' ' +5 y ' +3 y=1 , y ( 0 )=1 , y ( 1 )=0 .
'' '
(ii) y −3 x y =1 , y ( 0 )=1 , y ( 1 )=1 .
(iii) ( 2+2 x 2 ) y ' ' +6 x y ' +4 y=4 , y ( 0 )=0 , y (1 )= 1 .
2
Find the solution of the problem using followind steps.
(a) Derive a recurrence relation for the above differential equation using three point central
difference formula for derivative with h=1/3.
(b) Using the above finite difference formula solve the above boundary value problem.
(c) Express the above initial value problem as a system of first order differential
equations.
(d) Use MATLAB function “sol = bvp4c(odefun, bcfun, solinit)” to solve the BVP.
(i) Estimate the values of y at x=1 /3∧2/3.
(ii) Plot the solution curve y (x )’
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