Professional Documents
Culture Documents
Lecture 10
SURANAREE
INSTITUTE OF ENGINEERING
UNIVERSITY OF TECHNOLOGY
dy
= f (t , y )
dt
where t = independent variable
y = dependent variable
Initial Condition
y(t0) = y0
m
c
d 2x
dx
ma + cv + kx = m
+ c + kx = 0
dt
dt
EULERS METHOD
(t t0 ) 2
y (t0 ) +L
From Taylor series: y (t ) = y (t0 ) + (t t0 ) y (t0 ) +
2!
Retaining only first derivative:
y (t ) = y0 + h f (t0 , y0 )
y1 = y0 + h f (t0 , y0 )
y2 = y1 + h f (t1 , y1 )
h
t0
t1
yi = yi 1 + h f (ti 1 , yi 1 )
dy
= t 2 y, y (0) = 1
dt
1
Exact solution: y = ( 2t 1 + 5e 2t )
4
Set h = 0.2
ti
f (ti-1, yi-1)
0.0
0.2
0.4
0.6
NA
0-2(1) = -2.0
0.2-2(0.6) = -1.0
0.4-2(0.4) = -0.4
Euler
yi=yi-1+h f (ti-1, yi-1)
initial cond. = 1.0
1.0+0.2(-2.0) = 0.6
0.6+0.2(-1.0) = 0.4
0.4+0.2(-0.4) = 0.32
Exact
Error
1.0000
0.6879
0.5117
0.4265
0
-0.0879
-0.1117
-0.1065
Example:
dy
= t 2 y,
dt
y (0) = 1, h = 0.2
rhs1.m
function dydt = rhs1(t,y)
dydt = t-2*y;
>> [t,y] = odeEuler(rhs1,0.6,0.2,1.0)
t =
0
0.2000
0.4000
0.6000
y =
1.0000
0.6000
0.4000
0.3200
Exact Solution :
>> y0 = (1/4)*(2*t-ones(size(t))+5*exp(-2*t))
y0 =
1.0000
0.6879
0.5117
0.4265
>> t0=0:0.01:0.6;
>> y0 = (1/4)*(2*t0-ones(size(t0))+5*exp(-2*t0));
>> plot(t0,y0,t,y)
Euler:
y0i = yi-1 + hk1
slope:
k2 = f(ti, y0i)
slope:
k1 = f(ti-1, yi-1)
h
ti-1
ti
k1 + k 2
f (t i 1, y i 1 ) + f (t i , y i0 )
Average slope =
=
2
2
k + k2
y i = y i 1 + h 1
2
Use slope:
(k1 + k2)/2
h
ti-1
ti
Heuns Method
dy
= f (t , y )
dt
k1 = f (ti 1 , yi 1 )
y i0
k2 = f (ti 1 + h, yi 1 + hk1 )
Euler
k +k
yi = yi 1 + h 1 2
2
Heun
True
yi-1
h
ti-1
ti
Predictor-Corrector Approach
Heuns Method:
k1 = f (ti 1 , yi 1 )
k2 = f (ti 1 + h, yi 1 + hk1 )
yi = yi 1 + h
Eulers Method:
yi = yi 1 + h f (ti 1 , yi 1 )
k1 + k2
2
yi
f (ti 1 , yi 1 ) + f (ti , y )
yi 1 + h
2
0
i
y = 4e 0.8 x 0.5 y ,
y (0) = 2
Predictor,
y=
4 0.8 x 0.5 x
e e
+ 2e 0.5 x
(
)
1.3
yi0 = yi 1 + h f (ti 1 , yi 1 )
y10 = 2 + 1 (4e 0 0.5(2)) = 5
True value: y =
4 0.8(1) 0.5(1)
e
e
+ 2e 0.5(1) = 6.1946
(
)
1.3
Relative error, Et =
nd
rd
Corrector,
3 Corrector,
6.1946 6.7011
100% = 8.18%
6.1946
% Predictor of y1
y = 5
>> y = 2+(1/2)*((4*exp(0)-0.5*2)+(4*exp(0.8*1)-0.5*y))
% 1st Corrector of y1
y = 6.7011
Press
and
Enter
y = 6.7011
% 2nd Corrector of y1
y = 6.2758
% 3rd Corrector of y1
y = 6.3821
% 4th Corrector of y1
y = 6.3555
% 5th Corrector of y1
y = 6.3609
% until no change
MATLABs Implementation
func1.m
function dydx = func1(x,y)
dydx = 4*exp(0.8*x)-0.5*y;
odeHeun.m
function [x,y] = odeHeun(diffeq,xn,h,y0,iter)
% Input:
iter = number of corrector iterations
x = (0:h:xn);
n = length(x);
y = y0*ones(n,1);
for i=2:n
y(i) = y(i-1)+h*feval(diffeq,x(i-1),y(i-1));
for j=1:iter
y(i) = y(i-1)+h*(feval(diffeq,x(i-1),y(i-1)) ...
+ feval(diffeq,x(i),y(i)))/2;
end
end
y_true =
2.0000
6.1946
14.8439
33.6772
75.3390
Et =
0.00
2.68
3.09
3.17
3.18
Et =
0.00
19.28
23.19
24.24
24.54
60
True
Euler
Heun 15
40
20
0
0
2
x
yi = yi 1 + l kl
l =1
l =1
=1
2
1
yi = yi 1 + h k1 + k 2
3
3
k1 = f ( xi 1 , yi 1 )
3
k2 = f xi 1 + h,
4
yi 1 + k1h
4
h
( k1 + 4k2 + k3 )
6
where
k1 = f ( xi 1 , yi 1 )
1
1
k2 = f xi 1 + h, yi 1 + k1h
2
2
k3 = f ( xi 1 + h, yi 1 k1h + 2k 2 h )
h
( k1 + 2k2 + 2k3 + k4 )
6
where
k1 = f ( xi 1 , yi 1 )
1
1
k2 = f xi 1 + h, yi 1 + k1h
2
2
1
1
k3 = f xi 1 + h, yi 1 + k2 h
2
2
k4 = f ( xi 1 + h, yi 1 + k3 h )
y = 4e 0.8 x 0.5 y ,
y (0) = 2
k1 = 4e0 0.5(2) = 3
1
k2 = 4e0.8(0.5) 0.5(2 + 3 1) = 4.2173
2
1
k3 = 4e0.8(0.5) 0.5(2 + 4.2173 1) = 3.9130
2
k4 = 4e0.8(1) 0.5(2 + 3.9130 1) = 5.9457
1
(3 + 2 4.2173 + 2 3.9130 + 5.9457 )
6
= 6.2011
y1 = 2 +
True value: y =
4 0.8(1) 0.5(1)
e
e
+ 2e 0.5(1) = 6.1946
(
)
1.3
Relative error, Et =
6.1946 6.2010
100% = 0.1038%
6.1946
MATLABs Implementation
odeRK4.m
function [x,y] = odeRK4(diffeq,xn,h,y0)
x = (0:h:xn);
n = length(x);
y = y0*ones(n,1);
for i=2:n
k1 = feval(diffeq,x(i-1),y(i-1));
k2 = feval(diffeq,x(i-1)+h/2,y(i-1)+k1*h/2);
k3 = feval(diffeq,x(i-1)+h/2,y(i-1)+k2*h/2);
k4 = feval(diffeq,x(i-1)+h,y(i-1)+k3*h);
y(i) = y(i-1)+h/6*(k1+2*k2+2*k3+k4);
end
y_true =
2.0000
6.1946
14.8439
33.6772
75.3390
Et(%) =
0.00
0.1038
0.1250
0.1309
0.1328
y (0) = 2
func1.m
For x = 0 to 4, y(0) = 2
>>
>>
>>
>>
x0 = 0; xn = 4; y0 = 2;
[x45,y45] = ode45(func1,[x0,xn],y0);
xtrue = (0:0.1:4);
ytrue = (4/1.3)*(exp(0.8*xtrue)...
- exp(-0.5*xtrue))+ 2*exp(-0.5*xtrue);
>> plot(xtrue,ytrue,x45,y45)