School of Engineering and Architecture
Civil Engineering Department
General Civil Engineering
Solution of Ordinary
Differential Equations
Module 3 Chapter 2 – Numerical Solutions to CE Problems
Objective After this chapter, the student should be able to:
Solve and analyze ordinary differential equations using different
methods
Content
This chapter focuses on:
Initial Value Problems
✓ Euler’s Explicit Method
✓ Modified Euler’s Method
✓ Runge-Kutta methods (2nd order methods)
Related Below is a supplemental content for this course:
Melchers, R. E. (2018). Structural reliability analysis and prediction
Readings (3rd ed.). Hoboken, NJ: John Wiley
Engr. Aline Benneth V. Jacobo Page 1 of 16
Module 3 – Solution of Ordinary Differential Equations
ORDINARY DIFFERENTIAL EQUATIONS
“If it costs you your peace of mind, the price is too high.”
Anonymous
A differential equation is an equation that contains derivatives of an unknown function. The
solution of the equation is the function that satisfies the differential equation. A differential equation
that has one independent variable is called an ordinary differential equation (ODE). A first-order ODE
involves the first derivative of the dependent variable with respect to the independent variable. For
example, if x is the independent variable and y is the dependent variable, the equation has
dy dy
combinations of the variables x, y, and dx. A first-order ODE is linear if it is a linear func. of y and dx.
dy
Linear ODE: + ax 2 + by = 0
dx
dy
Nonlinear ODE: + a𝑦𝑥 + b√𝑦 = 0
dx
Where a and b are constants.
Differential equations appear in all branches of science
and engineering. They are also encountered in economics,
business applications, and social sciences where ideas have to
be quantified and predictive models are needed. Differential
equations provide detailed information regarding distributions
or variations of the dependent variable as a function of the
independent variable. As an example, consider the cylindrical
water tank shown in Fig. 10-1. The tank is being filled at the top,
and water flows out of the tank through a pipe connected at the
bottom. The rate of water flow into the tank varies with time and
is given by the equation:
This equation is a first-order ODE, with t as the independent variable and h the dependent
variable. The solution of the equation above is a function h(t) that satisfies the equation. In general, an
infinite number of functions satisfy the equation. To obtain a specific solution, a first-order ODE must
Engr. Aline Benneth V. Jacobo CHAPTER 2 Page 2 of 16
Module 3 – Solution of Ordinary Differential Equations
have an initial condition or constraint that specifies the value of the dependent variable at a particular
value of the independent variable. The problem is then called an initial value problem (IVP). Such a
constraint or initial condition associated with the equation stated may be written as:
Numerical Solution of a First-Order ODE
A numerical solution of a first order ODE is a set of discrete points that approximate the function
y(x). When a differential equation is solved numerically, the problem statement also includes the
domain of the solution. For example, a solution is required for values of the independent variable from
x = a to x = b (the domain is [a, b] ). Depending on the numerical method used to solve the equation,
the number of points between a and b at which the solution is obtained can be set in advance, or it
can be decided by the method.
For example, the domain can be divided into N subintervals of equal width defined by N + 1
values of the independent variable from x1 = a to xN+1 = b. The solution consists of values of the
dependent variable that are determined at each value of the independent variable. The solution then
is a set of points (x1 , y1 ), (x2, y2), ... , (xN + yN + 1 ) that define the function y(x) .
Overview of Numerical Methods for Solving a First-Order ODE
Numerical solution is a procedure for calculating an estimate of the exact solution at a set of
discrete points. The solution process is incremental, which means that it is determined in steps. It starts
at the point where the initial value is given. Then, using the known solution at the first point, a solution
is determined at a second nearby point. This is followed by a solution at a third point, and so on. There
are procedures with a single-step and multistep approach.
In a single-step approach, the solution at the next point, xi + 1, is calculated from the already
known solution at the present point, xi. In a multi-step approach, the solution at xi+ 1 is calculated from
the known solutions at several previous points. The idea is that the value of the function at several
previous points can give a better estimate for the trend of the solution.
Two types of methods, explicit, and implicit, can be used for calculating the solution at each
step. The difference between the methods is in the way that the solution is calculated at each step.
Engr. Aline Benneth V. Jacobo CHAPTER 2 Page 3 of 16
Module 3 – Solution of Ordinary Differential Equations
Explicit methods are those methods that use an explicit formula for calculating the value of the
dependent variable at the next value of the independent variable. In an explicit formula, the right-
hand side of the equation only has known quantities. In other words, the next unknown value of the
dependent variable, yi+1, is calculated by evaluating an expression of the form:
Where xi, yi, and xi+1 are all known quantities.
In implicit methods, the equation used for calculating yi+1 from the known xi, yi and xi+1 has the
form:
If the function on the right-hand side of the equation depends linearly on yi+1 then it is actually
an explicit formula because it can be rewritten and solved for y i+1 to obtain an explicit expression.
Implicit methods provide improved accuracy over explicit methods, but require more effort at each
step.
Single-step Explicit Methods
In a single-step explicit method, illustrated in Fig.
10-4, the approximate numerical solution (xi+1, yi+1) is
calculated from the known solution at point (xi, yi) by:
Where h is the step size, and the Slope is a
𝑑𝑦
constant that estimates the value of 𝑑𝑥 in the interval form
𝑥𝑖 to 𝑥𝑖+1 .
Engr. Aline Benneth V. Jacobo CHAPTER 2 Page 4 of 16
Module 3 – Solution of Ordinary Differential Equations
Euler’s Explicit Method
Euler's method is the simplest technique for solving a first-order ODE of the form of:
𝑑𝑦
= 𝑓(𝑥, 𝑦) with the initial condition: 𝑦(𝑥1 ) = 𝑦1
𝑑𝑥
Euler's explicit method (also called the forward Euler method) is a single-step, numerical
technique for solving a first-order ODE. The method uses the equations:
Where the value of the constant Slope is the slope of y(x) at point (xi, yi). This slope is actually
calculated from the differential equation:
Euler's method assumes that for a short distance h near
(xi, yi), the function y(x) has a constant slope equal to the slope
at (xi, yi). With this assumption, the next point of the numerical
solution (xi+1, yi+1) is calculated by:
Engr. Aline Benneth V. Jacobo CHAPTER 2 Page 5 of 16
Module 3 – Solution of Ordinary Differential Equations
Sample Problem 1
dy
Use Euler's explicit method to solve the ODE = −1.2y + 7e−0.3x from x = 0 to x = 2.5 with
dx
the initial condition y = 3 at x = 0. Use h = 0.5.
Solution:
Engr. Aline Benneth V. Jacobo CHAPTER 2 Page 6 of 16
Module 3 – Solution of Ordinary Differential Equations
For the exact solutions using analytical solution of ODE, the General solution of the ODE
dy
= −1.2y + 7e−0.3x
dx
Is
70 𝐶
𝑦= 0.3𝑥
+ 1.2𝑥
9𝑒 𝑒
when initial value is (0,3) , C = -43/9
Giving us the explicit solution of
70 43
𝑦= −
9𝑒 0.3𝑥 9𝑒 1.2𝑥
Substituting the values of x at each steps with step interval equal to 0.5, the resulting exact
coordinates will be:
The error is the calculated by subtracting the exact value of y and the numerical value of y which in
turn will give us a graph:
Engr. Aline Benneth V. Jacobo CHAPTER 2 Page 7 of 16
Module 3 – Solution of Ordinary Differential Equations
Modified Euler’s Method
The modified Euler method is a single-step, explicit, numerical technique for solving a first-order
ODE. The method is a modification of Euler's explicit method. The slope used in the modified. Euler
method is the average of the slope at the beginning of the interval and an estimate of the slope at
the end of the interval. Once the two slopes are calculated, a better value of y i+1 is calculated using
the average of the two slopes:
Where:
Steps in Solving using Modified Euler Method
1. Given a solution at point (xi , yi), calculate the next value of the independent variable:
2. Calculate f(xi , yi)
3. Estimate yi+1 using Euler’s method
4. Calculate
5. Calculate yi+1 using:
Engr. Aline Benneth V. Jacobo CHAPTER 2 Page 8 of 16
Module 3 – Solution of Ordinary Differential Equations
Try Sample Problem 1 using Modifies Euler’s Method:
We should be able to solve the
following:
Steps in Solving using Modified Euler Method
1. Given a solution at point (xi , yi), calculate the next value of the independent variable:
2. Calculate f(xi , yi)
3. Estimate yi+1 using Euler’s method
4. Calculate
5. Calculate yi+1 using:
Engr. Aline Benneth V. Jacobo CHAPTER 2 Page 9 of 16
Module 3 – Solution of Ordinary Differential Equations
Second-Order Runge-Kutta Method
The general form of second-order Runge-Kutta methods is:
With
Where c1, c2, a2, and b21 are constants.
Modified Euler Method in the Form of Second-Order Runge-Kutta Method
Sample Problem 2
dy
Use the second-order Runge-Kutta method (modified Euler version) to solve the ODE =
dx
−1.2y + 7e−0.3x from x = 0 to x = 2.0 with the initial condition y = 3 at x = 0. Use h = 0.5.
The first point of the solution is (0,3) at i = 1.
The values of x and y are x1 = 0 and y1 = 3.
In the first step, the equations above have the form:
𝑥𝑖+1 = 𝑥𝑖 + 0.5
The value of the dependent variable yi+1 is calculated by first calculating K1 and K2 using
Engr. Aline Benneth V. Jacobo CHAPTER 2 Page 10 of 16
Module 3 – Solution of Ordinary Differential Equations
and then substituting them in
The equations above are applied five times with i = 1, 2, 3, and 4 since the step size is h = 0.5
and we need to solve from x = 0 to x = 2.0, the number of steps is then determined by
𝑥𝑓 − 𝑥𝑖 2.0 − 0
#𝑜𝑓 𝑠𝑡𝑒𝑝𝑠 = = =4
ℎ 0.5
Engr. Aline Benneth V. Jacobo CHAPTER 2 Page 11 of 16
Module 3 – Solution of Ordinary Differential Equations
SOLUTIONS TO SAMPLE PROBLEMS:
Sample Problem 1
dy
Use Euler's explicit method to solve the ODE = −1.2y + 7e−0.3x from x = 0 to x = 2.5 with
dx
the initial condition y = 3 at x = 0. Use h = 0.5.
Solution:
The first point of the solution is (0,3) at i = 1.
The values of x and y are x1 = 0 and y1 = 3.
In the first step, the equations above have the form:
𝑥𝑖+1 = 𝑥𝑖 + 0.5
𝑦𝑖+1 = 𝑦𝑖 + (−1.2yi + 7e−0.3xi )(0.5)
The equations above are applied five times with i = 1, 2, 3, 4, and 5 since the step size is h = 0.5
and we need to solve from x = 0 to x = 2.5, the number of steps is then determined by
𝑥𝑓 − 𝑥𝑖 2.5 − 0
#𝑜𝑓 𝑠𝑡𝑒𝑝𝑠 = = =5
ℎ 0.5
First step at i = 1
xi+1 = xi + 0.5
x2 = x1 + 0.5 = 0 + 0.5
x2 = 0.5
yi+1 = yi + (−1.2yi + 7e−0.3xi )(0.5)
y2 = y1 + (−1.2y1 + 7e−0.3x! )(0.5) = 3 + [(−1.2)(3) + 7𝑒 −0.3(0) ](0.5)
y2 = 4.7
Engr. Aline Benneth V. Jacobo CHAPTER 2 Page 12 of 16
Module 3 – Solution of Ordinary Differential Equations
The second point is (0.5 , 4.7) where x2 = 0.5 and y2 = 4.7
Second step at i = 2
xi+1 = xi + 0.5
x3 = x2 + 0.5 = 0.5 + 0.5
x3 = 1
yi+1 = yi + (−1.2yi + 7e−0.3xi )(0.5)
y3 = y2 + (−1.2y2 + 7e−0.3x2 )(0.5) = 4.7 + [(−1.2)(4.7) + 7𝑒 −0.3(0.5) ](0.5)
y3 = 4.893
The third point is (1 , 4.893) where x3 = 1 and y3 = 4.893
Third step at i = 3
xi+1 = xi + 0.5
x4 = x3 + 0.5 = 1 + 0.5
x4 = 1.5
yi+1 = yi + (−1.2yi + 7e−0.3xi )(0.5)
y4 = y3 + (−1.2y3 + 7e−0.3x3 )(0.5) = 4.893 + [(−1.2)(4.893) + 7𝑒 −0.3(1) ](0.5)
y4 = 4.55
The fourth point is (1.5 , 4.55) where x4 = 1.5 and y4 = 4.55
Fourth step at i = 4
xi+1 = xi + 0.5
x5 = x4 + 0.5 = 1.5 + 0.5
x5 = 2
yi+1 = yi + (−1.2yi + 7e−0.3xi )(0.5)
y5 = y4 + (−1.2y4 + 7e−0.3x4 )(0.5) = 4.55 + [(−1.2)(4.55) + 7𝑒 −0.3(1.5) ](0.5)
y5 = 4.052
The fifth point is (2 , 4.052) where x5 = 2 and y5 = 4.052
Fifth step at i = 5
xi+1 = xi + 0.5
x6 = x5 + 0.5 = 2 + 0.5
x6 = 2.5
yi+1 = yi + (−1.2yi + 7e−0.3xi )(0.5)
Engr. Aline Benneth V. Jacobo CHAPTER 2 Page 13 of 16
Module 3 – Solution of Ordinary Differential Equations
y6 = y5 + (−1.2y5 + 7e−0.3x5 )(0.5) = 4.052 + [(−1.2)(4.052) + 7𝑒 −0.3(2) ](0.5)
y6 = 3.542
The sixth point is (2.5 , 3.542) where x6 = 2.5 and y5 = 3.542
For the exact solutions using analytical solution of ODE, the General solution of the ODE
dy
= −1.2y + 7e−0.3x
dx
Is
70 𝐶
𝑦= +
9𝑒 0.3𝑥 𝑒 1.2𝑥
when initial value is (0,3) , C = -43/9
Giving us the explicit solution of
70 43
𝑦= 0.3𝑥
− 1.2𝑥
9𝑒 9𝑒
Substituting the values of x at each steps with step interval equal to 0.5, the resulting exact
coordinates will be:
The error is the calculated by subtracting the exact value of y and the numerical value of y which in
turn will give us a graph:
Engr. Aline Benneth V. Jacobo CHAPTER 2 Page 14 of 16
Module 3 – Solution of Ordinary Differential Equations
Sample Problem 2
dy
Use the second-order Runge-Kutta method (modified Euler version) to solve the ODE =
dx
−1.2y + 7e−0.3x from x = 0 to x = 2.0 with the initial condition y = 3 at x = 0. Use h = 0.5.
The first point of the solution is (0,3) at i = 1.
The values of x and y are x1 = 0 and y1 = 3.
In the first step, the equations above have the form:
𝑥𝑖+1 = 𝑥𝑖 + 0.5
The value of the dependent variable yi+1 is calculated by first calculating K1 and K2 using
and then substituting them in
The equations above are applied five times with i = 1, 2, 3, and 4 since the step size is h = 0.5
and we need to solve from x = 0 to x = 2.0, the number of steps is then determined by
𝑥𝑓 − 𝑥𝑖 2.0 − 0
#𝑜𝑓 𝑠𝑡𝑒𝑝𝑠 = = =4
ℎ 0.5
First step at i = 1
xi+1 = xi + 0.5
x2 = x1 + 0.5 = 0 + 0.5
x2 = 0.5
K i = −1.2yi + 7e−0.3xi
K1 = −1.2y1 + 7e−0.3x1 = −1.2(3) + 7e−0.3(0)
K1 = 3.4
yi + 𝐾𝑖 ℎ = y1 + 𝐾1 ℎ = 3 + 3.4(0.5) = 4.7
K 2 = −1.2(𝑦𝑖 + 𝐾1 ℎ) + 7𝑒 −0.3(𝑥𝑖+1) = −1.2(𝑦1 + 𝐾1 ℎ) + 7𝑒 −0.3(𝑥2)
K 2 = −1.2(4.7) + 7𝑒 −0.3(0.5) = 0.385
Engr. Aline Benneth V. Jacobo CHAPTER 2 Page 15 of 16
Module 3 – Solution of Ordinary Differential Equations
1
yi+1 = yi + (K1 + K 2 )(h)
2
1 1
y2 = y1 + (K1 + K 2 )(h) = 3 + (3.4 + 0.385)(0.5)
2 2
y2 = 3.946
The second point is (0.5 , 3.946) where x2 = 0.5 and y2 = 3.946
Repeating this method up to fourth step, the succeeding points should be:
The third point is (1.0 , 4.188) where x3 = 1.0 and y3 = 4.188
The fourth point is (1.5 , 4.063) where x4 = 1.5 and y4 = 4.063
The fifth point is (2.0 , 3.763) where x5 = 2.0 and y5 = 3.763
Engr. Aline Benneth V. Jacobo CHAPTER 2 Page 16 of 16