CH 6 - Numerical Methods of Solving Differential Equations: Dy DX F X, y y X
CH 6 - Numerical Methods of Solving Differential Equations: Dy DX F X, y y X
dy
What do we do when a differential equation is difficult to solve analytically? Given =f x , y and
dx
an initial value y x 0 = y 0 , we will use numerical methods to evaluate y x at values near x 0 .
We want to find y x 1 . if x 0 and x 1 are close, the points y 1 and y x 1 will be close
too. So, we will approximate y x 1 by y 1 . To find y 1 , we need to find the equation of
l1 .
Equation of l 1 : y= y 0 f x 0, y 0 x−x 0 , so y 1 = y 0 f x 0, y 0 x1 − x0
Now, how do we approximate y x 2 ? One way is by y 21 using l 3 . But we have a
problem in doing so as we don't exactly know the slope of l 3 as we don't know y x 1
precisely.
But, if y 1 is a very good approximation to y x 1 , we can use that to estimate the slope of
the tangent line at x 1 . So, practically, we will use l 2 (a line that passes through x1, y 1
that has a slope of f x1, y 1 ) instead of l 3 .
From l 2 , we will find y 2 , and use that to approximate y x 2 .Equation of l 2 :
y= y 1f x 1, y 1 x− x1
y x 2≈ y 2= y 1 f x 1, y 1 x2 −x 1
We continue in this fashion:
y x 3≈ y 3= y 2 f x2, y 2 x 3− x 2
⋮
y x n1≈ y n1= y nf x n , y n x n1−x n
• Denoting f x n , y n =f n and x n 1−x n =h , we have Euler's method as follows
y n1= y n h f n
dy
Example1. = y , y 0=1 , use Euler's method with size h=0.1 to find the approximate
dx
values of the solution at x=0.1, 0.2, 0.3, 0.4 . Also find the approximate value of y 100 .
Solution:
f x , y = y and y 0=1
y 0.1≈ y 1= y 0f x 0, y 0 h , but f x 0, y 0= y 0 =1
11∗0.1=1.1
But, finding y 100 will not be an easy task if we follow the above process. We need to find all the
numbers in [ y 0.4 , y 99.9] . Since it is tiresome, we won't do that! But we can find a general
formula for out approximation, and plug in the numbers we want.
To do this, we need to solve the difference equation y n1= y n f x n , y n h . For this case,
f x n , y n = y n , so, the difference equation becomes y n1= y n hy n=1h y n .
So, let's find the solution to the difference equation y n1−1h y n=0
This difference equation is linear, homogeneous, and with constant coefficients, which means it can be
solved using the characteristic equation method.
−h1=0
=h1
y n=c 1 n
y n=c1 h1n
To find the constant c 1 , we use the initial value
y 0=c1=1
y n=h1n
Now, that we have our general approximation formula, we can use it find the approximate value of
y nh , n=0, 1, 2,⋯ . Let's see if what we have already gotten are right.
y 0.1≈ y 1=h1=1.1
y 0.2≈ y 2 =h12=1.1 2=1.21
3 3
y 0.3≈ y 3=h1 =1.1 =1.331
4 4
y 0.4 ≈ y 4 h1 =1.1 =1.4641
From the trend above, we can see that y n /10≈ y n . So, y 100= y 1000 /10≈ y 1000 .
1000 1000
y 1000 =1h =1.1
One solution is to redo the whole process by changing the value of h , so that a=nh . If we do
that, we just apply our general formula, with the changed h .
What is y 0.15 ?
With h=0.1 , we don't find an integer index to approximate it. If we try, y 0.15≈ y 1.5 . But we
can change the value of h so that we get an integer index. If we make h=0.05 ,
y 0.15≈ y 0.15/ 0.05= y 3 , and y 3=1h3=1.053
0.15 is in the second interval, [0.1, 0.2]. The same line is used to do the approximation in this
interval. So, for this, x i=0.1 .To approximate y(0.15), we need to determine the equation of the line.
y x i = y 0.1≈ y 1=1h=1.1 . So, the equation of the line becomes y=1.11.1 x−0.1 .
So, y 0.15=1.11.10.15−0.1=1.155 .
The equation of this line can be used for any x∈[0.1,0.2] . So, we can check that
y 0.1=1.11.1 0.1−0.1=1.1 and y 0.2=1.11.1 0.2−0.1=1.21 .
dy
Example2. =x use Euler's method with size h and y x 0 = y 0 to find a difference equation.
dx
Solve the difference equation to find a general formula for the approximation of y x .
Find y 1, y 2, y 3 using the general formula, and using the direct method. Also Assuming
h=0.01, x 0 =0, y 0=2 , find y 56 , y 67.015 [NB: Don't change value of h]
Solution:
y n1= y n f x n , y n h , but f x n , y n =x n , so y n1= y n hx n . But, what is x n ? We know that
x 1=x 0h , x 2=x 02h , x 3=x 03h ,⋯ .So, x n=x 0nh .Using this, the difference equation is:
General solution: y Gn =?
n
Homogeneous part: y n1− y n =0 . This is easy to solve. =1 So, y n=c1 1 .
nh 2 h2 p G
y np=nanb=n hx 0− . Combining y n and y n , we get:
2 2
n 2 h2 nh2
y n=c1 − nhx 0
2 2
To find c 1 , use the initial value y x 0 = y 0 .Evaluate y n at n=0 . y 0=c1
2 2 2
n h nh
Therefore, y n= y 0 − nhx 0
2 2
Direct Method:
y 1= y 0 f x 0, y 0 h= y 0hx 0
2
y 2= y 1f x 1, y 1 h= y 0hx 0hx1 = y 0hx 0 h x 0h=h 2hx 0 y 0
y 3= y 2hf x 2, y 2=h22hx 0 y 0hx 2=h22hx 0 y 0h x 0 2h=3h 23hx 0 y 0
0.012
y 56≈ y 56/ 0.01= y 5600=2 5600 2−5600=1569.72 .
2
x2
The exact solution y x =2 . Using this, y 56=2562 /2=1570 . The relative error in
2
approximation is E=1570−1569.72/1570=0.0014
2
0.001
If h=0.001 , y 56≈ y 56000=2560002−56000=1569.972 . The relative error in this
2
approximation is 1570−1569.972/1570=.00001783 .
In a given interval [x i , x ih] , the equation of the line used for approximation is
y= y i f x i , y i x−x i . For the current problem, y= y i xi x −xi . For the current interval,
x i=67.01 and y i is the approximation of y 67.01≈ y 6701 =2246.835 .
y 67.015=2246.83567.0167.015−67.01=2247.17005
f ' xi , y i h2 f n−1 x i , y i n
y i1= y if x i , y i h ⋯ h O hn1
2! n!
Euler's formula : i1 i y = y f x i , y i h
f ' x i , y i h2 f ' ' x i , yi h3 f n−1 x i , y i hn
Error formula: Et = ⋯ O hn1
2! 3! n!
Example 4:
Use Et to estimate the error of the first step of the previous example
Solution:
2 3 3 4
f ' x i , y i h f ' ' x i , yi h f xi , y i h
For this specific example, Et = as f 4 =0
2! 3! 4!
f x , y =−2x312x 2−20x8.5
f ' x , y =−6x224x – 20
f ' ' x , y =−12x24
f 3 x , y =−12
f 4 x , y =0
y n1= y n hk 2
• y n1 is determined by the present value y n plus the product of the size of the
interval, h , and the estimated slope.
• The slope is the weighted average of slopes
• k 1 slope @ the beginning of the interval
• k 2 slope at the midpoint of the interval, using slope k 1 to determine the value
h
of y at the point x n using Euler's method
2
• k 3 slope at the midpoint, but now using the slope k 2 to determine the y-value
• k 4 slope at the end of the interval, with its y-value determined using k 3
• In averaging the4slopes, greater weight is given to the slopes at the midpoint
1
• Slope= k 12 k 2 2 k 3k 4
6
dy
Example1. = y , y 0=1 , use Runge-kutta 2nd order method with size h=0.1 to find the
dx
approximate values of the solution at x=0.1 . Also find the approximate value of y 100 .
Solution:
Given: f x n , y n = y n , x 0=0, y 0 =1
y 0.1= y x 1 ≈ y 1= y 0hk 2
h h
k 2=f x 0 , y 0 k 1 and k 1=f x 0, y 0 , so
2 2
h
k 1= y 0=1 and k 2= y 0 k 1=10.05∗1=1.05 , therefore
2
y 1=10.11.05=1.105
Now, compare the accuracy of the solution obtained using Euler's method and the one using RK2.
The ODE given in the problem is easy to solve analytically: y x =e x y 0.1=1.10517092
| Eeuler |=1.10517092−1.1=0.00517092, | E RK2 |=1.10517092−1.105=1.7092e-04
To find y 100 , we solve the difference equation we get from RK2, which is
h2
y n1= y n hy n y n
2
2
h
y n1 – 1h y n=0 , solving this difference equation, we get:
2
2 n
h
y n=1h
2
0.12 1000
y 100≈ y 1000 =10.1 =1.1051000
2
Example 2:
Solve the problem using RK4:
h
y 1= y 0 k 12 k 22 k 3 k 4
6
k 1=f x 0, y 0 = y 0=1
h k
k 2=f x 0 , y 0h 1 =10.05=1.05
2 2
h k 0.105
k 3 =f x 0 , y 0h 2 =1 =1.0525
2 2 2
k 4=f x 0h , y 0h k 3 =10.10525=1.10525
0.1
y 1=1 12∗1.052∗1.05251.10525=1.105170833333333
6
E RK4=1.10517092−1.10517083=8.99999998971168e-08 , which is by far a better
approximation
To find y 100 , we need to solve the difference equation that results from RK4.
h
y n1= y n k 12 k 22 k 3 k 4
6
k 1=f x n , y n= y n
h h h h h
k 2=f x n , y n k 1 = y n k 1= y n y n= y n 1
2 2 2 2 2
h h h h h h2
k 3 =f x n , y n k 2= y n y n 1 = y n 1
2 2 2 2 2 4
h h2 h 2 h3
k 4=f x nh , y nhk 3= y nh y n 1 = y n 1h
2 4 2 4
2 h3
6 y n3 h y nh y n y n
1 4 h h2 h3
Slope= k 12k 22k 3k 4 = = y n y n y n y n
6 6 2 6 24
2 3 4
h h h
y n1= y n h∗slope= y n 1h
2 6 24
h2 h3 h4
Difference equation: y n1 – y n 1h =0
2 6 24
We can see that RK4 results in a better approximation than the others.