Vous êtes sur la page 1sur 14

- Basic Ideas

- Eulers Method
- Higher Order One-step Methods
- Predictor-Corrector Approach
- Runge-Kutta Methods
- Adaptive Stepsize Algorithms
Numerical Methods for Numerical Methods for Civil Civil Engineers Engineers
Lecture Lecture 10 10 Ordinary Differential Equations Ordinary Differential Equations
Mongkol J I RAVACHARADET
S U R A N A R E E INSTITUTE OF ENGINEERING
UNIVERSITY OF TECHNOLOGY SCHOOL OF CIVIL ENGINEERING
( , )
dy
f t y
dt
=
where t = independent variable
y = dependent variable
Initial Condition y(t
0
) = y
0
Rate of change of one variable with respect to another.
2
0
d x dx
ma cv kx m c kx
dt dt
+ + = + + =
Example: Mass-spring system
m
k
c
x
Ordinary Differential Equations Ordinary Differential Equations
New value = Old value + slope x step size
From Taylor series:
2
0
0 0 0 0
( )
( ) ( ) ( ) ( ) ( )
2!
t t
y t y t t t y t y t


= + + +L
Retaining only first derivative:
0 0 0
( ) ( , ) y t y h f t y = +
where h = (t - t
0
), y
0
= y(t
0
), and f(t
0
, y
0
) = y(t
0
)
t
y
t
0
t
1
h
Predict
True
error
1 0 0 0
( , ) y y h f t y = +
2 1 1 1
( , ) y y h f t y = +
1 1 1
( , )
i i i i
y y h f t y

= +
EULER EULER S METHOD S METHOD
Example: Manual Calculation with Eulers Method
2 , (0) 1
dy
t y y
dt
= =
Exact solution:
( )
2
1
2 1 5
4
t
y t e

= +
t
i
f (t
i-1
, y
i-1
)
Euler
y
i
=y
i-1
+h f (t
i-1
, y
i-1
)
Exact Error
Set h = 0.2
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
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
1.0000
0.6879
0.5117
0.4265
0
-0.0879
-0.1117
-0.1065
Implementing Eulers Method
function [t,y] = odeEuler(diffeq,tn,h,y0)
% Input: diffeq = (string) name of m-file that
% evaluate right hand side of ODE
% tn = stopping value of independent variable
% h = stepsize
% y0 = initial condition at t=0
t = (0:h:tn);
n = length(t);
y = y0*ones(n,1);
for i=2:n
y(i) = y(i-1)+h*feval(diffeq,t(i-1),y(i-1));
end
odeEuler.m
Example:
2 , (0) 1, 0.2
dy
t y y h
dt
= = =
function dydt = rhs1(t,y)
dydt = t-2*y;
rhs1.m
>> [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
>> y0 = (1/4)*(2*t-ones(size(t))+5*exp(-2*t))
y0 =
1.0000
0.6879
0.5117
0.4265
Exact Solution :
>> t0=0:0.01:0.6;
>> y0 = (1/4)*(2*t0-ones(size(t0))+5*exp(-2*t0));
>> plot(t0,y0,t,y)
Heun Heun s s Method Method: : estimate slope from derivatives at the beginning
and at the end of the interval.
Improvements of Improvements of E Euler uler s s M Method ethod
t
y
t
i-1
slope:
k
1
= f(t
i-1
, y
i-1
)
t
i
Euler:
y
0
i
= y
i-1
+ hk
1
h
slope:
k
2
= f(t
i
, y
0
i
)
t
y
t
i-1
t
i
h
2
) , ( ) , (
2
slope Average
0
1 1 2 1 i i i i
y t f y t f k k +
=
+
=

+
+ =

2
2 1
1
k k
h y y
i i
Use slope:
(k
1
+ k
2
)/2
1 1 1
2 1 1 1
1 2
1
( , )
( , )
2
i i
i i
i i
k f t y
k f t h y hk
k k
y y h

=
= + +
+
= +
y
i-1
t
i-1
h
t
i
True
Euler
Heun
Heun Heun s s M Method ethod
( , )
dy
f t y
dt
=
0
i
y
Heuns Method:
1 1 1
2 1 1 1
1 2
1
( , )
( , )
2
i i
i i
i i
k f t y
k f t h y hk
k k
y y h

=
= + +
+
= +
1 1 1
( , )
i i i i
y y h f t y

= +
Eulers Method:
0
1 1 1
Predictor: ( , )
i i i i
y y h f t y

= +
0
1 1
1
( , ) ( , )
Corrector:
2
i i i i
i i
f t y f t y
y y h

+
= +
Predictor Predictor- -Corrector Approach Corrector Approach
Iterating the Corrector of Iterating the Corrector of Heun Heun s s Method Method
To improve the estimation
0
1 1
1
( , ) ( , )
2
i i i i
i
f t y f t y
y h

+
+
i
y
Example: Use Heuns method to integrate
0.8
4 0.5 , (0) 2
x
y e y y

= =
From x = 0 to 4, step size = 1
Analytical solution:
( )
0.8 0.5 0.5
4
2
1.3
x x x
y e e e

= +
0
1 1 1
0 0
1
Predictor, ( , )
2 1(4 0.5(2)) 5
i i i i
y y h f t y
y e

= +
= + =
0
1 1
1
0 0.8(1)
1
( , ) ( , )
Corrector,
2
(4 0.5(2)) (4 0.5(5))
2 1 6.7011
2
i i i i
i i
f t y f t y
y y h
e e
y

+
= +
+
= + =
True value:
( )
0.8(1) 0.5(1) 0.5(1)
4
2 6.1946
1.3
y e e e

= + =
6.1946 6.7011
Relative error, 100% 8.18%
6.1946
t
E

= =
0 0.8(1)
nd
1
(4 0.5(2)) (4 0.5(6.7011))
2 Corrector, 2 1
2
6.2758, 1.31%
t
e e
y
E
+
= +
= =
0 0.8(1)
rd
1
(4 0.5(2)) (4 0.5(6.2758))
3 Corrector, 2 1
2
6.3821, 3.03%
t
e e
y
E
+
= +
= =
Error may increases for large step sizes.
Iteration using up-arrow in MATLAB
>> y = 2+1*(4*exp(0)-0.5*2) %Predictor of y1
y = 5
>> y = 2+(1/2)*((4*exp(0)-0.5*2)+(4*exp(0.8*1)-0.5*y))
y = 6.7011 %1st Corrector of y1
Enter Press and
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
function dydx = func1(x,y)
dydx = 4*exp(0.8*x)-0.5*y;
func1.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
odeHeun.m
Et =
0.00
2.68
3.09
3.17
3.18
y_true =
2.0000
6.1946
14.8439
33.6772
75.3390
For x = 0 to 4, step size = 1, y(0) = 2, Iteration = 15
>> [x,y] = odeHeun(func1,4,1,2,15)
x =
0
1
2
3
4
y =
2.0000
6.3609
15.3022
34.7433
77.7351
Compare with Eulers Method:
>> [x,y] = odeEuler(func1,4,1,2)
x =
0
1
2
3
4
y =
2.0000
5.0000
11.4022
25.5132
56.8493
Et =
0.00
19.28
23.19
24.24
24.54
Comparison of True Solution with Eulers and Heuns Method
>> xtrue = (0:0.1:4);
>> ytrue =(4/1.3)*(exp(0.8*xtrue)-exp(-0.5*xtrue))...
+ 2*exp(-0.5*xtrue);
>> [xeuler,yeuler] = odeEuler(func1,4,1,2);
>> [xheun,yheun] = odeHeun(func1,4,1,2,15);
>> plot(xtrue,ytrue,xeuler,yeuler,o,xheun,yheun,+)
0 1 2 3 4
0
20
40
60
80
x
y
True
Euler
Heun 15
1
1
m
i i l l
l
y y k

=
= +

General Formula using Weighted Averageof slopes


For Heuns method
1
=
2
= 0.5
The weights must satisfy
1
1
m
l
l

=
=

Second-Order RK (Ralstons) Method:


1 1 2
1 1 1
2 1 1 1
1 2
3 3
( , )
3 3
,
4 4
i i
i i
i i
y y h k k
k f x y
k f x h y k h




= + +


=

= + +


R RUNGE UNGE- -KUTTA (RK) KUTTA (RK) METHODS METHODS
Third-Order Runge-Kutta Methods
( )
1 1 2 3
4
6
i i
h
y y k k k

= + + +
where
( )
1 1 1
2 1 1 1
3 1 1 1 2
( , )
1 1
,
2 2
, 2
i i
i i
i i
k f x y
k f x h y k h
k f x h y k h k h



=

= + +


= + +
Fouth-Order Runge-Kutta Methods
where
( )
1 1 1
2 1 1 1
3 1 1 2
4 1 1 3
( , )
1 1
,
2 2
1 1
,
2 2
,
i i
i i
i i
i i
k f x y
k f x h y k h
k f x h y k h
k f x h y k h




=

= + +



= + +


= + +
The most popular RK methods
( )
1 1 2 3 4
2 2
6
i i
h
y y k k k k

= + + + + Classical 4th-order RK:


Example: Use 4th-order RK method to integrate
0.8
4 0.5 , (0) 2
x
y e y y

= =
From x = 0 to 4, step size = 1
Step 1: x
0
= 0, y
0
= 2
0
1
0.8(0.5)
2
0.8(0.5)
3
0.8(1)
4
4 0.5(2) 3
1
4 0.5(2 3 1) 4.2173
2
1
4 0.5(2 4.2173 1) 3.9130
2
4 0.5(2 3.9130 1) 5.9457
k e
k e
k e
k e
= =
= + =
= + =
= + =
( )
1
1
2 3 2 4.2173 2 3.9130 5.9457
6
6.2011
y = + + + +
=
True value:
( )
0.8(1) 0.5(1) 0.5(1)
4
2 6.1946
1.3
y e e e

= + =
6.1946 6.2010
Relative error, 100% 0.1038%
6.1946
t
E

= =
MATLABs Implementation
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
odeRK4.m
For x = 0 to 4, step size = 1, y(0) = 2, Iteration = 15
>> [x,y] = odeRK4(func1,4,1,2)
x =
0
1
2
3
4
y =
2.0000
6.2010
14.8625
33.7213
75.4392
y_true =
2.0000
6.1946
14.8439
33.6772
75.3390
Et(%) =
0.00
0.1038
0.1250
0.1309
0.1328
MATLABs ode23 and ode45 Routines
ode23 use second- and third-order RK simultaneously
ode45 use fourth- and fifth-order RK simultaneously
>> 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)
For x = 0 to 4, y(0) = 2
Example:
0.8
4 0.5 , (0) 2
x
y e y y

= =
func1.m
Adaptive Adaptive Stepsize Stepsize Algorithms Algorithms

Vous aimerez peut-être aussi