Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 39

Numerical

Analysis
Lecture 41
Examples of
Differential
Equations
Recall
EULER
METHOD
We considered the
differential equation of first
order with the initial
condition y(t0) = y0.

dy
 f (t , y )
dt
We obtained the solution
of the given differential
equation in the form of a
recurrence relation

ym 1  ym  hf (tm , ym )
In fact Euler’s method
constructs wi ~ y(ti ) for each
i = 0, 1,…, N-1 by deleting
the remainder term. Thus
the Euler’s Method is
w0   ,
wi 1  wi  hf (ti , wi )
for each i  0,1,..., N  1
Euler’s algorithm
Let us try to approximate the
solution of the given IVP at
(N+1) equally spaced numbers
in the interval [a ,b]
y  f (t , y ),
a  t  b, y (a )  
INPUT endpoints a, b; integer
N, initial condition (alpha)

OUTPUT approximate w to y
at the (N+1) values of t
Step 1
Set h=(b-a) / N
t=a
w = (alpha)
OUTPUT (t , w)
Step 2
For i = 0,1,…N do Step 3, 4.
Step 3
Set w = w + h f (t , w); (compute wi ).
t = a + i h (compute ti )
Step 4 OUTPUT (t , w)
Step 5 STOP
Example
Use Euler’s method to
approximate the solution of
IVP
y’= y - t + 1, 0 < t < 2,
2

y ( 0 ) = 0.5 with N = 10.


Solution
Here, h = 0.2, ti = 0.2i, w0= 0.5
and wi+1 = wi + h (wi - ti2 + 1)
= wi+0.2[wi - 0.04i +1]
2

=1.2 wi - 0.008i + 0.2


2

for i = 0,1,…,9.
The exact solution is
y ( t )= (t+1) -0.5 e
2 t
> alg051();
This is Euler's Method.
Input the function F(t,y) in
terms of t and y
For example: y-t^2+1
> y-t^2+1
Input left and right endpoints separated
by blank
>02
Input the initial condition
> 0.5
Input a positive integer for the number
of subintervals
> 10
Choice of output method:
1. Output to screen
2. Output to text file
Please enter 1 or 2
>1
Output
t w
0.000 0.5000000
0.200 0.8000000
0.400 1.1520000
0.600 1.5504000
0.800 1.9884800
1.000 2.4581760
> alg051();
This is Euler's Method.
Input the function F (t,y) in
terms of t and y
For example: y-3*t^2+4
> y-3*t^2+4
Input left and right hand points separated
by a blank
>0 1
Input the initial condition
> 0.5
Input a positive integer for the number of
subintervals
> 10
Choice of output method:
1. Output to screen
2. Output to text file
Please enter 1 or 2
>1
Output
t w
0.000 0.5000000
0.100 0.9500000
0.200 1.4420000
0.300 1.9742000
0.400 2.5446200
0.500 3.1510820
0.600 3.7911902
0.700 4.4623092
0.800 5.1615401
0.900 5.8856942
1.000 6.6312636
Recall
Runge-Kutta
(Order Four)
METHOD
The fourth-order R-K
method was described as

1
yn 1  yn  (k1  2k2  2k3  k4 )
6
where
k1  hf (tn , yn )
 h k1 
k2  hf  tn  , yn  
 2 2
 h k2 
k3  hf  tn  , yn  
 2 2
k4  hf (tn  h, yn  k3 )
Example
Solve the following differential
equation dy  t  y
dt
with the initial condition y(0) = 1,
using fourth- order Runge-Kutta
method from t = 0 to t = 0.4
taking h = 0.1
Solution
The fourth-order Runge-Kutta
method is described as
1
y  y  ( k  2k  2k  k )
n 1
6
n 1 2 (1) 3 4

where k1  hf (tn , yn )
h k 

k2  hf  tn  , yn  1 
 2 2
 h k 
k3  hf  tn  , yn  2 
 2 2
k4  hf (tn  h, yn  k3 )
In this problem,
f (t , y )  t  y, h  0.1, t0  0, y0  1.

As a first step, we calculate


k1  hf (t0 , y0 )  0.1(1)  0.1
k2  hf (t0  0.05, y0  0.05)
 hf (0.05,1.05)  0.1[0.05  1.05]
 0.11
k3  hf (t0  0.05, y0  0.055)
 0.1(0.05  1.055)
 0.1105
k4  0.1(0.1  1.1105)  0.12105
Now, we compute from
1
y1  y0  (k1  2k2  2k3  k4 )
6
1
 1  (0.1  0.22  0.2210  0.12105)
6
 1.11034
Therefore y(0.1) = y1=1.1103
In the second step, we have
to find y2 = y(0.2)
We compute
k1  hf (t1 , y1 )  0.1(0.1  1.11034)  0.121034
 h k1 
k2  hf  t1  , y1  
 2 2
 0.1[0.15  (1.11034  0.060517)]  0.13208
 h k2 
k3  hf  t1  , y1  
 2 2
 0.1[0.15  (1.11034  0.06604)]  0.132638
k4  hf (t1  h, y1  k3 )
 0.1[0.2  (1.11034  0.132638)]  0.1442978
From Equation (1), we see that
1
y2  1.11034  [0.121034  2(0.13208)
6
2(0.132638)  0.1442978]  1.2428

Similarly we calculate,
k1  hf (t2 , y2 )  0.1[0.2  1.2428]  0.14428
 h k1 
k2  hf  t2  , y2    0.1[0.25  (1.2428  0.07214)]  0.156494
 2 2
 h k2 
k3  hf  t1  , y1    0.1[0.3  (1.2428  0.078247)]  0.1571047
 2 2
k4  hf (t2  h, y2  k3 )  0.1[0.3  (1.2428  0.1571047)]  0.16999047
Using equation (1), we compute
1
y (0.3)  y3  y2  (k1  2k 2  2k3  k 4 )  1.399711
6
Finally, we calculate
k1  hf (t3 , y3 )  0.1[0.3  1.3997]  0.16997
 h k 
k2  hf  t3  , y3  1   0.1[0.35  (1.3997  0.084985)]  0.1834685
 2 2
 h k 
k3  hf  t3  , y3  2   0.1[0.35  (1.3997  0.091734)]  0.1841434
 2 2
k4  hf (t3  h, y3  k3 )  0.1[0.4  (1.3997  0.1841434)]  0.19838434
Using them in equation (1),
we get
y (0.4)  y4
1
 y3  (k1  2k2  2k3  k4 )
6
 1.58363

which is the required result.


Runge-Kutta Order Four
w0  
k1  hf (ti , wi )
 h k1 
k2  hf  ti  , wi  
 2 2
 h k2 
k3  hf  ti  , wi  
 2 2
k4  hf (ti  h, wi  k3 )

1
wi 1  wi  (k1  2k 2  2k3  k 4 )
(1)
6
RK4 algorithm
Let us try to approximate the
solution of the given IVP at
(N+1) equally spaced numbers
in the interval [a ,b]
y  f (t , y ),
a  t  b, y (a )  
INPUT endpoints a, b; integer
N, initial condition (alpha)

OUTPUT approximate w to y
at the (N+1) values of t
Step 1
Set h=(b-a) / N
t=a
w = (alpha)
OUTPUT (t , w)
Step 2
For i = 0,1,…N do Step 3 - 5.
Step 3
K1  hf (t , w)
Set
 h K1 
K 2  hf  t  , w  
 2 2 
 h K2 
K 3  hf  t  , w  
 2 2 
K 4  hf (t  h, w  K 3 )

1
wi 1  wi  (k1  2k 2  2k3  k 4 )
6
Step 3
Set w = w + h f (t , w); (compute wi ).
t = a + i h (compute ti )
Step 4 OUTPUT (t , w)
Step 5 STOP
> alg052();
This is the Runge-Kutta
Order Four Method.
Input the function F(t,y) in
terms of t and y
For example: y-t^2+1
> y-t^2+1
Input left and right endpoints
separated by blank
>02
Input the initial condition
> 0.5
Input a positive integer for the
number of subintervals
> 10
Choice of output method:
1. Output to screen
2. Output to text file
Please enter 1 or 2
>1
Output
t w
0.000 0.5000000
0.200 0.8292933
0.400 1.2140762
0.600 1.6489220
0.800 2.1272027
1.000 2.6408227
1.200 3.1798942
1.400 3.7323401
1.600 4.2834095
1.800 4.8150857
2.000 5.3053630
Numerical
Analysis
Lecture 41

You might also like