Numerical Solution To ODE's
Numerical Solution To ODE's
Ordinary differential equations (ODEs) play a central role in modeling problems of engineering, physics, chemistry, and
several other areas. Math 208 (Differential Equations) discusses the analytical solution to these ODE’s. However, in your
career as an engineer, you will encounter ODE’s that cannot be solved by those analytic methods or whose solutions are
so difficult that other approaches are needed. It is precisely in these real-world projects that numeric methods for ODEs
and are used.
Euler’s Method
𝑑𝑦
= 𝑓(𝑡, 𝑦), 𝑦(𝑡0 ) = 𝑦0
𝑑𝑡
Pick a step size h > 0, then
∆𝑡 = ℎ, 𝑡𝑛+1 = 𝑡𝑛 + ℎ
∆𝑦 = ℎ𝑓(𝑡𝑛 , 𝑦𝑛 ) 𝑦𝑛+1 = 𝑦𝑛 + ∆𝑦
Example:
𝑑𝑦
= 𝑡 + 𝑦, 𝑦(0) = 0
𝑑𝑡
Using h = 0.1
𝑓(𝑡𝑛 , 𝑦𝑛 ) = 𝑡𝑛 + 𝑦𝑛
∆𝑡 = 0.1, 𝑡𝑛+1 = 𝑡𝑛 + 0.1
∆𝑦 = 0.1(𝑡𝑛 + 𝑦𝑛 ) 𝑦𝑛+1 = 𝑦𝑛 + ∆𝑦
n t yn f(t,y) del y
0 0 0 0 0
1 0.1 0 0.1 0.01
2 0.2 0.01 0.21 0.021
3 0.3 0.031 0.331 0.0331
4 0.4 0.0641 0.4641 0.04641
5 0.5 0.11051 0.61051 0.061051
6 0.6 0.171561 0.771561 0.0771561
7 0.7 0.2487171 0.9487171 0.09487171
8 0.8 0.34358881 1.14358881 0.11435888
9 0.9 0.45794769 1.35794769 0.13579477
10 1 0.59374246 1.59374246 0.15937425
∆𝑡 = ℎ, 𝑡𝑛+1 = 𝑡𝑛 + ℎ
𝑘1 = 𝑓(𝑡𝑛 , 𝑦𝑛 )
ℎ ℎ𝑘1
𝑘2 = 𝑓 (𝑡𝑛 + , 𝑦𝑛 + )
2 2
ℎ ℎ𝑘2
𝑘3 = 𝑓 (𝑡𝑛 + , 𝑦𝑛 + )
2 2
𝑘4 = 𝑓(𝑡𝑛 + ℎ, 𝑦𝑛 + ℎ𝑘3 )
ℎ
∆𝑦 = (𝑘 + 2𝑘2 + 2𝑘3 + 𝑘4 )
6 1
𝑦𝑛+1 = 𝑦𝑛 + ∆𝑦
Example:
𝑑𝑦
= 𝑡 + 𝑦, 𝑦(0) = 0
𝑑𝑡
Using h = 0.1
𝑓(𝑡𝑛 , 𝑦𝑛 ) = 𝑡𝑛 + 𝑦𝑛
∆𝑡 = 0.1, 𝑡𝑛+1 = 𝑡𝑛 + 0.1
𝑘1 = 𝑡𝑛 + 𝑦𝑛
ℎ ℎ𝑘1
𝑘2 = (𝑡𝑛 + ) + (𝑦𝑛 + )
2 2
ℎ ℎ𝑘2
𝑘3 = (𝑡𝑛 + ) + (𝑦𝑛 + )
2 2
𝑘4 = (𝑡𝑛 + ℎ) + (𝑦𝑛 + ℎ𝑘3 )
ℎ
∆𝑦 = (𝑘 + 2𝑘2 + 2𝑘3 + 𝑘4 )
6 1
𝑦𝑛+1 = 𝑦𝑛 + ∆𝑦
n t yn k1 k2 k3 k4 del y
0 0 0 0 0.05 0.0525 0.10525 0.00517083
1 0.1 0.00517083 0.10517083 0.16042938 0.1631923 0.22149006 0.01623174
2 0.2 0.02140257 0.22140257 0.2824727 0.28552621 0.34995519 0.02845593
3 0.3 0.0498585 0.3498585 0.41735142 0.42072607 0.4919311 0.04196574
4 0.4 0.09182424 0.49182424 0.56641545 0.57014501 0.64883874 0.0568964
5 0.5 0.14872064 0.64872064 0.73115667 0.73527847 0.82224849 0.07339732
6 0.6 0.22211796 0.82211796 0.91322386 0.91777916 1.01389588 0.09163366
7 0.7 0.31375163 1.01375163 1.11443921 1.11947359 1.22569899 0.11178794
8 0.8 0.42553956 1.22553956 1.33681654 1.34238039 1.4597776 0.13406185
9 0.9 0.55960141 1.45960141 1.58258148 1.58873049 1.71847446 0.15867833
10 1 0.71827974 1.71827974 1.85419373 1.86098943 2.00437869 0.18588375
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
-0.1 0 0.2 0.4 0.6 0.8 1 1.2
The graph above shows the difference between the result of the Euler’s Method, Runge-Kutta Method, and the
analytical solution. It can be shown that the result of RK4 is much closer to the analytical solution, thus more accurate,
as compared to the Euler’s Method. One way of making the solution more accurate is by decreasing the value of h.
Systems of Differential Equations
Euler’s Method
𝑑𝑥
= 𝑓(𝑥, 𝑦, 𝑡), 𝑥(𝑡0 ) = 𝑥0
𝑑𝑡
𝑑𝑦
= 𝑔(𝑥, 𝑦, 𝑡), 𝑦(𝑡0 ) = 𝑦0
𝑑𝑡
Pick a step size h > 0, then
∆𝑡 = ℎ, 𝑡𝑛+1 = 𝑡𝑛 + ℎ
∆𝑥 = ℎ𝑓(𝑥𝑛 , 𝑦𝑛 , 𝑡𝑛 ) ∆𝑦 = ℎ𝑔(𝑥𝑛 , 𝑦𝑛 , 𝑡𝑛 )
𝑥𝑛+1 = 𝑥𝑛 + ∆𝑥 𝑦𝑛+1 = 𝑦𝑛 + ∆𝑦
Example:
𝑑𝑥
= 𝑥 + 𝑦 − 𝑡, 𝑥(0) = −0.5
𝑑𝑡
𝑑𝑦
= 𝑥 2 − 𝑦𝑡, 𝑦(0) = −1
𝑑𝑡
Using h = 0.1
∆𝑡 = ℎ, 𝑡𝑛+1 = 𝑡𝑛 + ℎ
𝑥𝑛+1 = 𝑥𝑛 + ∆𝑦 𝑦𝑛+1 = 𝑦𝑛 + ∆𝑦
Example:
𝑑𝑥
= 𝑥 + 𝑦 − 𝑡, 𝑥(0) = −0.5
𝑑𝑡
𝑑𝑦
= 𝑥 2 − 𝑦𝑡, 𝑦(0) = −1
𝑑𝑡
Using h = 0.1
∆𝑡 = ℎ, 𝑡𝑛+1 = 𝑡𝑛 + ℎ
𝑘𝑥4 = (𝑥𝑛 + ℎ𝑘𝑥1 ) + (𝑦𝑛 + ℎ𝑘𝑦3 ) − (𝑡 + ℎ) 𝑘𝑦4 = (𝑥𝑛 + ℎ𝑘𝑥1 )2 − (𝑦𝑛 + ℎ𝑘𝑦3 )(𝑡 + ℎ)
ℎ ℎ
∆𝑥 = 6 (𝑘𝑥1 + 2𝑘𝑥2 + 2𝑘𝑥3 + 𝑘𝑥4 ) ∆𝑦 = 6 (𝑘𝑦1 + 2𝑘𝑦2 + 2𝑘𝑦3 + 𝑘𝑦4 )
𝑥𝑛+1 = 𝑥𝑛 + ∆𝑦 𝑦𝑛+1 = 𝑦𝑛 + ∆𝑦
Higher-Order Differential Equation
𝑑2 𝑦 𝑑𝑦
2
= 𝑓 (𝑥, 𝑦, ) , 𝑦(𝑡0 ) = 𝑦0 , 𝑦 ′ (𝑡0 ) = 𝑦0′
𝑑𝑡 𝑑𝑡
Introduce a new variable
𝑑𝑦 𝑑2 𝑦 𝑑𝑣
𝑣= = 𝑦′, 𝑡ℎ𝑢𝑠 =
𝑑𝑡 𝑑𝑡 2 𝑑𝑡
The equation can be reduced two systems of differential equations and can be solved as such
𝑑𝑣
= 𝑓(𝑥, 𝑦, 𝑣), 𝑣(𝑡0 ) = 𝑦0′
𝑑𝑡
𝑑𝑦
= 𝑣, 𝑦(𝑡0 ) = 𝑦0
𝑑𝑡
Example:
A bullet shot directly above the ground has a movement characterized by the equation below
𝑑2 𝑦 𝑐 𝑑𝑦 2
= −𝑔 + 𝑖 ( )
𝑑𝑡 2 𝑚 𝑑𝑡
𝑑𝑦
−1 𝑖𝑓 𝑑𝑡
> 0
Where 𝑖 = { 𝑑𝑦
+1 𝑖𝑓 < 0
𝑑𝑡
Where g is the acceleration due to gravity, m is the mass of the bullet and c is the drag coefficient due to air friction.
(Note: The “i” just tells us the direction of the drag force of air.) A bullet with m = 5 g and c = 0.005 g/m is shot from the
ground upward with an initial velocity of 250 m/s. Determine the position vs time of the bullet.
Solution:
𝑑2 𝑦 1 𝑑𝑦 2
= −9.81 + 𝑖 ( ) , 𝑦(0) = 0, 𝑦 ′ (0) = 250
𝑑𝑡 2 1000 𝑑𝑡
Introduce a new variable
𝑑𝑦
𝑣=
𝑑𝑡
The equation will become
𝑑𝑣 𝑖
= −9.81 + 𝑣 2 , 𝑣(0) = 250
𝑑𝑡 1000
𝑑𝑦
= 𝑣, 𝑦(0) = 0
𝑑𝑡
Euler’s Method
Using h = 1
𝑖
𝑓(𝑥𝑛 , 𝑦𝑛 , 𝑡𝑛 ) = −9.81 + 1000 𝑣𝑛2 , 𝑔(𝑥𝑛 , 𝑦𝑛 , 𝑡𝑛 ) = 𝑣𝑛
𝑖
∆𝑣 = −9.81 + 1000 𝑣𝑛2 ∆𝑦 = 𝑣𝑛
𝑣𝑛+1 = 𝑣𝑛 + ∆𝑣 𝑦𝑛+1 = 𝑦𝑛 + ∆𝑦
n t v y del v del y
0 0 250 0 -72.31 250
1 1 177.69 250 -41.3837 177.69
2 2 136.3063 427.69 -28.3894 136.3063
3 3 107.9169 563.9963 -21.4561 107.9169
4 4 86.46082 671.9131 -17.2855 86.46082
5 5 69.17534 758.3739 -14.5952 69.17534
6 6 54.58012 827.5493 -12.789 54.58012
7 7 41.79113 882.1294 -11.5565 41.79113
8 8 30.23463 923.9205 -10.7241 30.23463
9 9 19.5105 954.1552 -10.1907 19.5105
10 10 9.319836 973.6657 -9.89686 9.319836
Using h = 1
∆𝑡 = ℎ, 𝑡𝑛+1 = 𝑡𝑛 + ℎ
𝑖
𝑘𝑣1 = −9.81 + 1000 𝑣𝑛2 𝑘𝑦1 = 𝑣𝑛
𝑖 ℎ𝑘𝑣1 2 ℎ𝑘𝑣1
𝑘𝑣2 = −9.81 + 1000 (𝑣𝑛 + 2
) 𝑘𝑦2 = 𝑣𝑛 + 2
𝑖 ℎ𝑘𝑣2 2 ℎ𝑘𝑣2
𝑘𝑣3 = −9.81 + 1000 (𝑣𝑛 + 2
) 𝑘𝑦3 = 𝑣𝑛 + 2
𝑖
𝑘𝑣4 = −9.81 + 1000 (𝑣𝑛 + ℎ𝑘𝑣3 )2 𝑘𝑦4 = 𝑣𝑛 + ℎ𝑘𝑣3
ℎ ℎ
∆𝑣 = (𝑘𝑣1 + 2𝑘𝑣2 + 2𝑘𝑣3 + 𝑘𝑣4 ) ∆𝑦 = (𝑘𝑦1 + 2𝑘𝑦2 + 2𝑘𝑦3 + 𝑘𝑦4 )
6 6
𝑥𝑛+1 = 𝑥𝑛 + ∆𝑦 𝑦𝑛+1 = 𝑦𝑛 + ∆𝑦
n t v y kv1 ky1 kv2 ky2 kv3 ky3 kv4 ky4 del v del y
0 0 250 0 -72.31 250 -55.5397 213.845 -59.1962 222.2302 -46.2161 190.8038 -57.9997 218.8257
1 1 192.0003 218.8257 -46.6741 192.0003 -38.2573 168.6633 -39.6946 172.8717 -33.007 152.3057 -39.2642 171.2293
2 2 152.7362 390.055 -33.1383 152.7362 -28.3515 136.167 -29.009 138.5604 -25.1184 123.7272 -28.8296 137.653
3 3 123.9066 527.7081 -25.1628 123.9066 -22.2033 111.3251 -22.535 112.8049 -20.0862 101.3716 -22.4543 112.2564
4 4 101.4523 639.9644 -20.1026 101.4523 -18.1641 91.40103 -18.3423 92.37024 -16.7173 83.11005 -18.3054 92.01748
5 5 83.14687 731.9819 -16.7234 83.14687 -15.4028 74.78517 -15.502 75.44546 -14.3858 67.64485 -15.4865 75.20883
6 6 67.66038 807.1908 -14.3879 67.66038 -13.4662 60.46642 -13.5221 60.92729 -12.741 54.13825 -13.5176 60.76434
7 7 54.1428 867.9551 -12.7414 54.1428 -12.0922 47.77208 -12.1233 48.09671 -11.5756 42.0195 -12.1247 47.98331
8 8 42.01813 915.9384 -11.5755 42.01813 -11.1226 36.23037 -11.1391 36.45681 -10.7635 30.87903 -11.1438 36.37858
9 9 30.87438 952.317 -10.7632 30.87438 -10.4599 25.49276 -10.4676 25.64444 -10.2264 20.40674 -10.4741 25.59259
10 10 20.40026 977.9096 -10.2262 20.40026 -10.0437 15.28717 -10.0465 15.37841 -9.9172 10.35376 -10.054 15.34753