Professional Documents
Culture Documents
Chap18 (Ecuaciones Diferenciales)
Chap18 (Ecuaciones Diferenciales)
ODEs:
Initial-Value Problems
Differential Equations
There are ordinary differential equations -
functions of one variable
dv cd 2
g v
dt m
d2x dx
m 2 c kx 0
etc. dt dt
Higher-Order ODE’s
Can always turn a higher order ODE into a set of 1st
order ODEs
d3x d2x
d t
dx
Example: a 3 b 2 c
dt dt dt
dz 1
dt a d ( t ) bz cy
dx d2x dy
Let y , z 2 then z
dt dt dt
dx
dt y
So solutions to first-order ODEs are important
Linear and Nonlinear ODEs
u u 2u
u 2
t x x
Ordinary Differential Equations
dT
Heat conduction (Fourier’s law) q k
dx
dc
Diffusion (Fick’s law) J D
dx
•Example of an d 2x dx
ODE m c kx f (t )
dt 2 dt
x
k f(t)
What order is this ODE?
m
If f(t) = 0, ODE is homogenous.
c
Free-body kx
If f(t) is not equal to 0, f(t)
ODE is non-homogenous. diagram m
dx
c
dt
d 2x dx
m c kx f (t )
2 dt
dt
Solutions for ODEs
The solution for the homogenous ODE.
x(t ) C1S1 (t ) C2 S2 (t )
C1 & C2 are two arbitrary constants and
S1 (t ) & S2 (t ) are two general solutions.
The solution for the non-homogenous ODE
x(t ) C1S1 (t ) C2 S 2 (t ) P (t )
P (t ) is the particular solutions.
The arbitrary constants C1 & C2 are determined by
the Initial-value or Boundary-value conditions.
Initial-Value & Boundary-Value Conditions
dy
f ( t , y) ; y( t0 ) y0
dt
Numerical approximations
New value = old value + slope × step size
yi 1 yi h
Runge-Kutta Methods
Runge Kutta methods (One Step Methods)
Idea is that
New value = old value + slope*step size
yi 1 yi h
Slope is generally a function of t, hence y(t)
Different methods differ in how to estimate
One-Step Method
yi 1 yi h
All one-step methods
can be expressed in
this general form, the
only difference being
the manner in which
the slope is estimated
Initial Conditions
The solution of
ODE depends on
the initial condition
dy
y f ( t , y); y(t 0 ) y0
dt
y0
t0 h t1 h t2 h t3
Example: Euler’s Method
dy
t y , y(0 ) 1, 0t1
dt
Analytic solution y ( 1 t 2 / 4 ) 2
Euler method yi 1 yi hfi , f t y
h = 0.50
y(0.5 ) y(0 ) hf (0,1.0 ) 1 (0.5)(0 1 ) 1.0
y( 1.0 ) y(0.5 ) hf (0.5 ,1.0 ) 1.0 (0.5 )( 0.5 1.0 ) 1.25
Example: Euler’s Method
h = 0.25 yi 1 yi hfi , f t y
y(0.25 ) y(0 ) hf (0, 1.0 )
1 (0.25 )( 0 1 ) 1.0
y(0.5 ) y(0.25 ) hf (0.25 , 1.0 )
1.0 (0.25 )( 0.25 1.0 ) 1.0625
y(0.75 ) y(0.5 ) hf (0.5 , 1.0625 )
1.0625 (0.25 )( 0.5 1.0625 ) 1.191347
y( 1.0 ) y(0.75 ) hf (0.75 , 1.191347 )
1.191347 (0.25 )( 0.75 1.191347 ) 1.39600
Euler’s method:
y
dy
y t y ; y( 0 ) 1
dt
h = 0.25
1
h = 0.5
t
0 0.25 0.5 0.75 1.0
Euler’s Method (modified M-file)
Euler’s Method
>> tt=0:0.01*pi:pi;
>> ye=1.5*exp(-tt)+0.5*sin(tt)-0.5*cos(tt);
>> [t,y]=Eulode('example2_f',[0 pi],1,0.1*pi);
step t y
1 0.0000000000 1.0000000000
2 0.3141592654 0.6858407346
3 0.6283185307 0.5674580652
dy
4 0.9424777961 0.5738440394 y sin t ; y(0 ) 1
5 1.2566370614 0.6477258022 dt
6 1.5707963268 0.7430199565
7 1.8849555922 0.8237526182
8 2.1991148575 0.8637463173
9 2.5132741229 0.8465525934
10 2.8274333882 0.7652584356
11 3.1415926536 0.6219259596
>> H=plot(t,y,'r-o',tt,ye);
>> set(H,'LineWidth',3,'MarkerSize',12)
>> print -djpeg ode01.jpg
Euler’s Method (h = 0.1)
dy
y sin t ; y(0 ) 1
dt
Euler’s Method (h = 0.05)
dy
y sin t ; y(0 ) 1
dt
Truncation Errors
There are
Local truncation errors - error from
application at a single step
Propagated truncation errors - previous
errors carried forward
The sum is “global truncation error”
Global & Local Errors
Global error
Local error
y y
yi+1 yi+1
yi yi
Heun’s method
Midpoint (or improved polygon) method
Third-order Runge-Kutta methods
Fourth-order Runge-Kutta methods
Heun’s Method
f ( t i , yi ) f ( t i 1 , y )
0
yi 1 yi i1
h
2
Heun’s Method
Iterate the corrector of Heun’s method to obtain
an improved estimate
Predictor Corrector
Heun’s Method with Iterative Correctors
Heun’s Method with Iterative Correctors
» te=0:0.02*pi:pi; ye=example2_e(xe);
» [t1,y1]=Euler('example2_f',[0 pi],1,0.1*pi);
» [t2,y2]=Heun_iter('example2_f',[0 pi],1,0.1*pi,0);
» [t3,y3]=Heun_iter('example2_f',[0 pi],1,0.1*pi,5);
» H=plot(te,ye,t1,y1,'r-d',t2,y2,'g-s',t3,y3,'m-o');
» set(H,'LineWidth',3,'MarkerSize',12)
» [te' ye' y1',y2',y3']
t yEuler yHeun yHeun_iter ytrue
0 1.0000 1.0000 1.0000 1.0000
0.3142 0.6858 0.7837 0.7704 0.7746
0.6283 0.5675 0.7018 0.6830 0.6896
0.9425 0.5738 0.7064 0.6872 0.6951
1.2566 0.6477 0.7559 0.7395 0.7479
1.5708 0.7430 0.8152 0.8036 0.8118
1.8850 0.8238 0.8565 0.8503 0.8578
2.1991 0.8637 0.8592 0.8584 0.8648
2.5133 0.8466 0.8112 0.8149 0.8199
2.8274 0.7653 0.7082 0.7154 0.7188
3.1416 0.6219 0.5540 0.5631 0.5648
Heun’s Method with Iterative Correctors
dy
y sin( t ) ; y(0 ) 1
dt
Heun’s with
5 iterations
Heun’s
method
Exact
Euler’s
t
Example: Heun’s Method
h = 0.5 dy
t y , y(0 ) 1, 0t1
dt
First Step
Preditor : y10 y0 f ( t0 , y0 ) h 1 (0 1 )( 0.5 ) 1.0
f ( t0 , y0 ) f ( t 1 , y10 )
1st Corrector : y y0
1
1 h 1 0.5 (0 1 0.5 1.0 )( 0.5 ) 1.1250
2
f ( t0 , y0 ) f ( t 1 , y11 )
2nd Corrector : y1 y0
2
h 1 0.5 (0 1 0.5 1.1250 )( 0.5 ) 1.1326
2
f ( t0 , y0 ) f ( t 1 , y12 )
3rd Corrector : y1 y0
3
h 1 0.5 (0 1 0.5 1.1326 )( 0.5 ) 1.1330
2
f ( t0 , y0 ) f ( t 1 , y13 )
4th Corrector : y1 y0
4
h 1 0.5 (0 1 0.5 1.1330 )( 0.5 ) 1.1331
2
f ( t0 , y0 ) f ( t 1 , y14 )
5 th Corrector : y1 y0
5
h 1 0.5 (0 1 0.5 1.1331 )( 0.5 ) 1.1331
2
Example: Heun’s Method
h = 0.5 dy
t y , y(0 ) 1, 0x1
dt
Second Step
yi 1 yi ( t i , yi , h) h
Where is an increment function which represents
the weighted-average slope over the interval
a1 k1 a2 k2 an kn
Where a’s are constants and k’s are slopes evaluated
at selected x locations
Runge-Kutta (RK) Methods
One-Step Method
General Form of nth-order Runge-Kutta Method
yi 1 yi h
a1 k 1 a 2 k 2 a n k n
k1 f ( t i , yi )
k2 f ( t i p1 h, yi q11k1 h)
k3 f ( t i p2 h, yi q21k1 h q22 k2 h)
kn f ( t i pn1 h, yi qn1, 1 k1 h qn1, 2 k2 h qn1,n1 kn1 h)
Where p’s and q’s are constants
k’s are recurrence relationships
Second-Order RK Methods
Taylor series expansion
k1 f ( t , y )
k2 f ( t p1 h, y q11k1 h) f ( t , y) ( p1 h) f t ( t , y) ( q11k1 h) f y ( t , y)
y( t h) y( t ) ha1 f ( t , y) a2 f ( t p1 h, y q11k1 h)
y( t ) ha1 f a2 ( f p1 hft q11k1 hf y )
yi 1 yi ( a1 k1 a2 k2 )h
k1 f ( t i , yi )
k2 f ( t i p1 h, yi q11k1 h)
a1 a2 1
a2 p1 1 / 2
a q 1 / 2
2 11
Second-Order RK Methods
General Second-order Runge-Kutta methods
k2
k1 f ( t i , yi ) f1
k1 a1 k1 + a2 k2 k2 f ( t i p1 h, yi q11k1 h)
y y ( a k a k )h
i1 i 1 1 2 2
Weighted-average slope
xi xi+p1h xi+1
= xi+h
Heun’s Method
Heun’s method with a single corrector (a2 = 1/2):
Choose a2 = 1/2 and solve for the other 3 constants
a1 = 1/2, p1 = 1, q11 = 1
k1 f ( t i , yi )
k2 f ( t i h, yi k1 h)
1 1
yi 1 yi k1 k2 h
2 2
Use average slope over the interval
Midpoint Method
Another Second-order Runge-Kutta method
Choose a2 = 1 a1 = 0, p1 = 1/2, q11 = 1/2
k2
k 1 f ( t i , yi )
k1
h 1
k2 f ( t i , yi k1 h)
2 2
yi 1 yi k2 h
xi xi+1/2 xi+1
Midpoint (2nd-order RK) Method
Midpoint (2nd-order RK) Method
>> [t,y] = midpoint('example2_f',[0 pi],1,0.05*pi);
step t y
1 0.0000000000 1.0000000000
2 0.1570796327 0.8675816988
3 0.3141592654 0.7767452235
4 0.4712388980 0.7206165079
5 0.6283185307 0.6927855807
6 0.7853981634 0.6872735266
7 0.9424777961 0.6985164603
8 1.0995574288 0.7213629094
9 1.2566370614 0.7510812520
dy
10
11
1.4137166941
1.5707963268
0.7833740988
0.8143967658
y sin( t )
12 1.7278759595 0.8407772414
dt
13 1.8849555922 0.8596353281 y( 0 ) 1
14 2.0420352248 0.8685989204
15 2.1991148575 0.8658156821
16 2.3561944902 0.8499586883
17 2.5132741229 0.8202249154
18 2.6703537556 0.7763257790
19 2.8274333882 0.7184692359
20 2.9845130209 0.6473332788
21 3.1415926536 0.5640309524
>> tt = 0:0.01*pi:pi; yy = example2_e(tt);
>> H = plot(t,y,'r-o',tt,yy);
>> set(H,'LineWidth',3,'MarkerSize',12);
Midpoint (RK2) Method
dy
y sin( t ) ; y(0 ) 1
dt
Ralston’s Method
Second-order Runge-Kutta method
Choose a2 = 2/3 a1 = 1/3, p1 = q11 = 3/4
k2
k 1 f ( t i , yi )
3 3
k2 f ( t i h, yi k1 h)
4 4
1 2
k1 yi 1 yi ( k 1 k 2 ) h
3 3
k1/3 + 2k2/3
k 1 f ( t i , yi )
k2 f ( t i p1 h, yi q11k1 h)
k3 f ( t i p2 h, yi q21k1 h q22 k2 h)
yi 1 yi ( a1 k1 a2 k2 a3 k3 )h
Weighted slope
a1 k1 a2 k2 a3 k3
Third-Order Runge-Kutta Methods
General Third-order Runge-Kutta methods
k3
k2
Weighted-
average
k1 value of
three slopes
k1 , k 2 , k 3
k 1 f ( t i , yi ) k 1 f ( t i , yi )
2 2 1 1
k2 f ( t i h, yi k1 h) k2 f ( t i h, yi k1 h)
3 3 2 2
2 2 3 3
k3 f ( t i 3 h, yi 3 k2 h) k3 f ( t i 4 h, yi 4 k2 h)
1 1
yi 1 y i ( 2 k 1 3 k 2 3 k 3 ) h yi 1 y i ( 2 k 1 3 k 2 4 k 3 ) h
8 9
Nystrom Method Nearly Optimum Method
3rd-order
Runge-Kutta Method
Reduce to Simpson’s 1/3 rule for f = f(t)
k 1 f ( t i , yi )
1 1
k2 f ( t i h, yi k1 h)
2 2
k3 f ( t i h, yi k1 h 2 k2 h)
1
yi 1 yi ( k 1 4 k 2 k 3 ) h
6
rd
3 -Order Heun Method
k 1 f ( t i , yi )
1 1
k2 f ( t i h, yi k1 h)
3 3
2 2
k3 f ( t i 3 h, yi 3 k2 h)
1
yi 1 yi ( k 1 3 k 3 ) h
4
Classical 4th-order
Runge-Kutta Method
One-step method
Reduce to Simpson’s 1/3 rule for f = f(t)
k1 f ( t i , yi )
k2 1 1
f ( t i h, yi k1 h)
2 2
k 1 1
f ( t i h, yi k2 h)
3 2 2
k f ( t i h, yi k3 h)
4
1
yi 1 yi ( k 1 2 k 2 2 k 3 k 4 ) h
6
Classical 4th-order
Runge-Kutta Method
k2
k4
k3
k1
1
( k1 2 k 2 2 k 3 k 4 )
6
ti ti + h/2 ti + h
Example: Classical
4th-order RK Method
h = 0.5 dy
t y , y(0) 1, 0t1
dt
k6 f (ti h, yi 3 k1h 2 k 2 h 12 k3h 12 k 4h 8 k5h )
7 7 7 7 7
System of ODEs
A system of simultaneous ODEs
n equations with n initial conditions
dy1
dt f1 ( t , y1 , y2 , , yn )
dy2 f 2 ( t , y1 , y2 , , yn )
dt
dyn f n ( t , y1 , y2 , , yn )
dt
System of ODEs
Bungee Jumper’s velocity and position
Two simultaneous ODEs
dx
dt v v ( t )
gm gc d
tanh t
cd m
dv g c d v 2
dt m gc d
x ( t ) c ln cosh m t
m
x (0 ) v (0 ) 0 d
Second-Order ODE
d 2 y dy
g ( t , y, )
dt 2
dt
y( t ) , dy (t )
0 0
dt
0 1
yi 1 yi hf ( t i , yi )
Two ODE-IVPs
Euler method
y1,i 1 y1, i f 1 ( t i , y1, i , y2 , i ) h y1,i (0.5 y1, i ) h
y2 , i 1 y2 , i f 2 ( t i , y1, i , y2 , i ) h y2 , i ( 4 0.1 y1, i 0.3 y2 , i ) h
y1 (0.5 ) y1 (0 ) 0.5y1 (0 )h 4 ( 0.5)(4)(0.5) 3
y2 (0.5 ) y2 (0 ) 4 0.1y1 (0 ) 0.3 y2 (0 )h 6 4 0.1( 4 ) 0.3(6 )(0.5 ) 6.9
y1 ( 1.0 ) y1 (0.5 ) 0.5y1 (0.5 )h 3 ( 0.5 )( 3 )(0.5 ) 2.25
y2 ( 1.0 ) y2 (0.5 ) 4 0.1y1 (0.5 ) 0.3 y2 (0.5 )h 6.9 4 0.1( 3 ) 0.3(6.9 )(0.5 ) 7.715
Euler’s Method for a System of ODEs
n = 100
n = 200
n = 500
n = 1000
Higher Order ODEs
In general, nth-order ODE
y ( n ) f ( t , y, y, y, , y ( n1) )
y( t0 ) 0 , y( t0 ) 1 , , y ( n 1) ( t0 ) n 1
y1 y y1 y2 , y1 ( t0 ) α0
y2 y 2y y3 , y2 ( t 0 ) α1
let y3 y y3 y4 , y3 ( t 0 ) α 2
yn y ( n 1 ) yn f ( t, y1 , y2 , , yn ) , yn ( t0 ) αn 1
System of First-Order ODE-IVPs
Example
y f ( t , y, y, y) t 2 4 ty 3 y 5 y
y(0 ) 2, y(0 ) 4 , y(0 ) 1
Convert to three first-order ODE-IVPs
let y1 y, y2 y dy/dt, y3 y d 2 y / dt 2
y1 f 1 ( t , y1 , y2 , y3 ) y2 y1 (0 ) 2
y2 f 2 ( t , y1 , y2 , y3 ) y3 y2 ( 0 ) 4
y (0 ) 1
3
y f 3 ( t , y1 , y 2 , y 3 ) t 2
4 ty1 3 y2 5 y3 3
In Vector Notation y f ( t , y )
Euler’s Method for Systems of
First-Order ODEs
Euler’s Method
y1 ( i 1) y1 ( i ) f1 ( t ( i ), y1 ( i ), y2 ( i ), y3 ( i )) h
y2 ( i 1) y2 ( i ) f 2 ( t ( i ), y1 ( i ), y2 ( i ), y3 ( i )) h
y ( i 1) y ( i ) f ( t ( i ), y ( i ), y ( i ), y ( i )) h
3 3 3 1 2 3
Example
y f ( t , y, y, y) t 2 4 ty 3 y 5 y
y(0 ) 2, y(0 ) 4 , y(0 ) 1
y1 y2 y1 (0 ) 2
y2 y3 y2 ( 0 ) 4
y (0 ) 1
3
y t 2
4 ty1 3 y2 5 y3 3
Example: Euler’s Method
First step: t(0) = 0, t(1) = 0.5 (h = 0.5)
y1 ( 1) y1 (0 ) f1 (0 )h y1 (0 ) y2 (0 )h 2 ( 4 )( 0.5 ) 4.0
y2 ( 1) y2 (0 ) f 2 (0 )h y2 (0 ) y3 (0 )h 4 ( 1)( 0.5 ) 4.5
y
3 ( 1) y 3 ( 0 ) f 3 ( 0 ) h y 3 ( 0 ) 0
2
4 (0 ) y1 (0 ) 3 y2 (0 ) 5 y3 (0 )
1 (0 ) 2 4 (0 )( 2 ) 3( 4 ) 5 ( 1) (0.5 ) 2.5
y ( i 1) y ( i )
1
( k1, 1 2 k 2 , 1 2 k 3 , 1 k 4 , 1 ) h
Applicable for
1 1
6 any number of
y ( i 1) y ( i ) 1 ( k 2 k 2 k k )h equations
2 2
6
1, 2 2, 2 3, 2 4,2
Hand Calculations: RK4 Method
Solve the following ODE from t = 0 to t = 1 with h = 0.5
dy1
dt f1 0.5 y1 y1 (0 ) 4
2 f 4 0.1 y 0.3 y y2 (0 ) 6
dy
dt 2 1 2
y1 (0.5 ) y1 (0 ) ( 1 / 6 )( k1, 1 2 k2 , 1 2 k3 , 1 k4 , 1 )h
4 ( 1 / 6 )[( 2 ) 2( 1.75 ) 2( 1.78125 ) 1.554688]( 0.5 ) 3.115234
y2 (0.5 ) y2 (0 ) ( 1 / 6 )( k1, 2 2 k2 , 2 2 k3 , 2 k4 , 2 )h
6 ( 1 / 6 )[( 1.8 ) 2( 1.715 ) 2( 1.715125 ) ( 1.631794 )]( 0.5 ) 6.857670
4th-order Runge-Kutta Method for ODEs
n = 25
n = 50
n = 100
Comparison of Numerical Accuracy
Nonlinear Pendulum
» tspan=[0 15]; y0=[pi/2 0];
» [t1,y1]=Euler_sys(‘pendulum',tspan,y0,15/100);
» [t2,y2]=RK2_sys(‘pendulum',tspan,y0,15/100);
» [t3,y3]=RK4_sys(‘pendulum',tspan,y0,15/100);
» H1=plot(t1,y1(:,1),t2,y2(:,1),t3,y3(:,1)); hold on;
» H2=plot(t1,y1(:,2),'b:',t2,y2(:,2),'g:',t3,y3(:,2),'r:');
Euler
RK2
RK4
Example: More than 2 ODE-IVPs
dy1
dt f 1 36 y1 30 y2 20 y3 10 y4 ; y1 (0 ) 1
dy2 f 2 61 y1 50 y2 36 y3 18 y4 ; y2 (0 ) 0
dt
dy3 f 3 34 y1 29 y2 25 y3 13 y4 ; y3 (0 ) 0
dt
dy
4 f 4 10 y1 10 y2 10 y3 6 y4 ; y4 ( 0 ) 0
dt
36 30 20 10 function f = example(t, y)
% solve y' = Ay = f, y0 = [1 0 0 0]'
61 50 36 18 % four first-order ODE-IVPs
A A = [ -36 30 -20 10
34 29 25 13 -61 50 -36 18
-34 29 -25 13
10 10 10 6 -10 10 -10 6];
y=[ y(1) y(2) y(3) y(4)]';
t0 1 0 0 0 f = A*y;
4th-order Runge-Kutta Method for ODE-IVPs
Symbols: n = 20
Lines : n =100
Chapter 20
Prob. 20.1 (40), (Hand Calculation, and use
MATLAB plotting for graph)
20.3 a) , b) and c) (40)(Hand Calculation)
Prob. 20.8 (30) (decomposing into two 1st
ODEs and then using MATLAB Program)