CH 9 OrdinaryDifferentialEquations Fall 24 25
CH 9 OrdinaryDifferentialEquations Fall 24 25
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 ) (1)
dx
with the initial condition
y ( x0 ) = y 0 .
Consider the Taylor series solution of the of the initial value problem
dy
= f ( x, y ) with y ( x0 ) = y 0
dx
The solution of the equation is a function of x. The Taylor series expansion of y (x) about x 0
is
h2 h3 h 4 (iv)
y( x0 + h) = y0 + h y0 + y0 + y0 + y0 +
2! 3! 3!
dny
where y 0( n) is the value of at x = x0 .
dx n
The values of the derivatives can be found by differentiating repeatedly and substituting
x = x0 and y = y 0 .
The method can also be used for higher order differential equations.
1
Fall 2024-2025
2
Fall 2024-2025
y1 = y 0 + f ( x0 , y 0 ) + f ( x1 , y0 + hf ( x0 , y 0 )
h
2
Setting
k1 = hf ( x0 , y 0 )
k 2 = hf ( x0 + h, y 0 + k1 )
1
we have y1 = y0 + [k1 + k 2 ]
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
of the error is ( h 3 ).
The fourth order Runge-Kutta formula, the most commonly used one in practice, is stated
without proof.
y1 = y 0 + k1 + 2k 2 + 2k 3 + k 4
1
6
where,
k1 = hf ( x0 , y 0 )
k1
k 2 = hf ( x0 + h2 , y 0 + 2
)
k2
k 3 = hf ( x0 + h2 , y 0 + 2
)
k 4 = hf ( x0 + 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
Taylor series of order four and the order of the error is (h 5 ) .
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 = f1 (t , y, z )
z = f 2 (t , y, z )
subject to initial conditions y (t0 ) = y0 and z (t 0 ) = z0 .
The second order Runge-Kutta (RK-2) method to the above system can be written in the
form
k1 = hf1 (t 0 , y0 , z0 ) m1 = hf 2 (t0 , y0 , z0 )
k 2 = hf1 (t 0 + h, y0 + k1 , z0 + m1 ) m2 = hf 2 (t 0 + h, y0 + k1 , z0 + m1 )
and y1 = y 0 + 1 k1 + k 2
2
z1 = z 0 + m1 + m2
1
2
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
y = f ( x, y, y ) with y ( x0 ) = c0 and y ( x0 ) = c1
Defining the new variable
y = z
the above initial value problem can be written as
y = z
z = f ( x, y , z )
with y ( x0 ) = c0 and z ( x0 ) = c1 .
4
Fall 2024-2025
Solution =
0 1.0000
0.2000 0.8484
0.4000 0.7644
0.6000 0.7257
5
Fall 2024-2025
0.8000 0.7275
1.0000 0.7799
1.2000 0.9253
1.4000 1.3401
(ii)
>> xin=0:0.02:1.4; % generate points with sm
>> [x2, y2]=ode23(f, xin,1);
>> plot(x2,y2)
Exercise 9.2
Given the initiaal value problem
dy dz
= x + y2 − z , = x 2 − 3 y + z 2 with y (1) = 2 and z (1) = 2.5 .
dx dx
(a) Estimate y (1.2) and z (1.2) using the RK-2 method with step size h = 0.1 .
(b) Use MATLAB function “[x1, y1]=ode45(F, [x0, xn], y0)”
(i) to estimate the values of y in 1 ≤ 𝑥 ≤ 1.4 using ℎ = 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, 𝑥0 = 1, 𝑦𝑜 = 2, 𝑧0 =
2.5, with h = 0.1 , we have
k1 = 0.1 f (1.1, 2.3019 , 2.6301) = 0.3769 m1 = 0.1g (1.1, 2.3019 , 2.6301) = 0.1222
k 2 = 0.1 f (1.2, 2.6788 , 2.7523 ) = 0.5624 m2 = 0.1g (1.2, 2.6788 , 2.7523 ) = 0.0979
y2 = 2.3019 + 12 (0.3769 + 0.5624 ) z 2 = 2.6301 + 12 (0.1222 + 0.0979 )
=2.7716 = 2.7402
Thus
y (1.2) y 2 = 2.7716 and z (1.2) z 2 = 2.7402 .
6
Fall 2024-2025
Sxyz =
(ii)
>> [x2,y2]=ode45(F, [1:0.01:1.4], [2, 2.5]);
>> plot(x2,y2)
Example 9.3
𝑑𝐼 1 𝑑𝑉 1 1
= 𝑉, =− 𝐼− 𝑉.
𝑑𝑡 𝐿 𝑑𝑡 𝐶 𝑅𝐶
where I is the current through the inductance and V is the voltage drop across the capacitor.
1
Suppose that R= 1 ohm, 𝐶 = 2 farad and 𝐿 = 1 henry.
Use MATLAB command “[x1, y1] =ode45(F, [x0, xn], y0)” to find the numerical solution
for 𝑡 ∈ [0, 5] if 𝐼(0) = 2 amperes and V(0) = 3 volts.
Plot your results 𝐼(𝑡) and 𝑉(𝑡) separately for distributions.
Solution:
𝑦1′ 𝑦2 𝑦1 (0) 2
[ ] = [−𝑦 − 𝑦 ] with [ ] = [ ].
𝑦2 ′ 1 2 𝑦2 (0) 3
7
Fall 2024-2025
>> 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
(𝑦𝑛+1 − 2𝑦𝑛 + 𝑦𝑛−1 ) − (𝑦 − 𝑦𝑛−1 ) = 1
ℎ2 2ℎ 𝑛+1
1 3
Taking ℎ = 3 , we get 9(𝑦𝑛+1 − 2𝑦𝑛 + 𝑦𝑛−1 ) − 2 (𝑦𝑛+1 − 𝑦𝑛−1 ) = 1
1 2
(b) With ℎ = 1⁄3, the nodal points are 𝑥0 = 0, 𝑥1 = 3 , 𝑥2 = 3 , 𝑥3 = 1.
1
For 𝑛 = 1, 𝑥1 = 3 , 𝑦0 = 1,
𝑦1′ 𝑦2 𝑦1 (0) 1
[ ] = [1 + 𝑦 ] with [ ]=[ ].
𝑦2 ′ 2 𝑦1 (1) 2(𝑒 − 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
0.3333 1.4579
0.6667 2.2283
1.0000 3.4366
9
Fall 2024-2025
Exercise 9
(a) Use three-point central difference formula for derivatives to derive a recurrence
relation for the above IVP.
(b) Express the above initial value problem as a system of first order differential
equations.
(c) Use (a) and (b) to estimate the values of y at t= 1.4 and 1.6.
(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 ≤ 𝑡 ≤ 2 using ℎ = 0.2.
10
Fall 2024-2025
7. Solve the following boundary value problems using the finite difference method and
central difference approximations.
1
(a) 𝑦 ′′ − 𝑦 = 4𝑥𝑒 𝑥 𝑦(0) = 0, 𝑦(1) = 1 with ℎ = 4 .
(b) 𝑦 ′′ = 𝑥𝑦 𝑦(0) = 0, 𝑦(1) = 1 with ℎ = 0.25 .
between the true solution and the numerical solution at the points where the
numerical solution is determined.
[Ref. Numerical Methods for engineers and Scientists – Amos Gilat, Vish Subramaniam,
Problem # 10.1, Page # 457]
11
Fall 2024-2025
calculate the error between the true solution and the numerical solution at the points
where the numerical solution is determined.
[Ref. Numerical Methods for engineers and Scientists – Amos Gilat, Vish Subramaniam,
Problem # 10.5, Page # 458]
(a) Using the central difference formulas for approximating the derivatives, discretize
the ODE (rewrite the equation in a form suitable for solution with the finite
difference method).
(b) Solve the BVP using step size ℎ = 1/4.
[Ref. Numerical Methods for engineers and Scientists – Amos Gilat, Vish Subramaniam,
Problem # 11.7, Page # 500]
13