Professional Documents
Culture Documents
Motivation
1
First Order Equations
2
Euler Method
Euler Method
Divide the region of interest [a,b] up into discrete values of
x = nh, n = 0,1, , N , spaced at interval h=(b-a)/N. Use the forward
difference approximation for the differential coefficient:
y − yn
f ( xn , yn ) = yn' ≈ n+1 .
h
Rearrange to give:
yn+1 ≈ yn + hf ( xn , yn )
yn+1 ≈ yn + hf ( xn , yn )
0
0
x x x
0 x 1 2
3
dy
Example Lets solve y ′ = = f ( x, y ) = x + y + 1 for x = [0, 2 ] with initial condition y(0) = 0
dx
and compare the Euler Solution with the exact solution y = 2e x − x − 2 (check this!) with
h =1.0. and plot the result:
n xn yn fn True yn
Euler Method solution of
0 0.000 0.000 1.000 0.000 10 y′ = x + y + 1
1 1.000 1.000 3.000 2.437
2 2.000 4.000 7.000 10.778
h=1
The Euler solution rapidly diverges y 5 h = 0.1
from the true solution Exact y
h2 h2
Expand in Taylor series: yn+1 = yn + hyn′ + yn′′ + = yn + hf n + y′′ +
2 2
4
Modified Euler Method
yn+1 ≈ yn + hf ( xn , yn )
A better estimation of the slope from (xn, yn) to (xn+1, yn+1) would be
h
yn +1 ≈ yn + { f ( xn , yn ) + f ( xn+1 , yn+1 )},
2
5
Modified Euler algorithm
y1
Step 1: Predictor
yn∗+1 = yn + hf ( xn , yn ) y y 1*
y0
Step 2: Corrector h /2 h /2
h
yn +1 = yn + { f ( xn , yn ) + f (xn+1 , yn*+1 )} 0
x
2 0
x 0 x 1
Example
dy
Use modified Euler on our example y′ = = f ( x, y ) = x + y + 1 for x = [0 ,2] with
dx
initial condition y(0) = 0. For h =1.0.
n xn y*n f*n yn
True fn
yn Modified Euler Method solution of
10
0 0.00 0.00 - 0.000 1.00 0.00 y′ = x + y + 1
0 0 0 0
1 1.00 1.00 3.00 2.000 4.00 2.43 h= 1
0 0 0 0 7 y 5 h = 0.1
Exact y
2 2.00 5.00 8.00 8.000 11.0 10.7
0 0 0 0 7
0
The solution is much more accurate than that for the simple Euler method.
In the h = 0.1 case plotted above, the difference is only 0.111 after 2 seconds.
Engineering Computation ECL7-12
6
Modified Euler Method solution of
Euler Method solution of
10
10 y′ = x + y + 1 y′ = x + y + 1
h= 1 h=1
y 5 h = 0.1y 5 h = 0.1
Exact y Exact y
0
0
0.0 0.5 1.0 1.5 2.0
0.0 0.5 1.0 1.5 2.0
x
x
Thus
∂f ∂f
yn +1 = yn +
h
2
f n + f n + h n + hf n n + O h 2 + ε
∂x ∂y
( )
h 2 ∂f n h 2 ∂f n
= yn + hf n +
2 ∂x
+
2
fn
∂y
+ O h3 ( ) +ε
7
Thus
yn +1 = yn +
h
2
∂f
∂x
∂f
∂y
( )
f n + f n + h n + hf n n + O h 2 +
= yn + hf n +
h 2 ∂f n h 2 ∂f n
2 ∂x 2
+ fn
∂y
( )
+ O h3 +
= yn + hf n +
h 2 ∂f n ∂f n
2 ∂x ∂y
+ ( )
f n + O h3 .
( )
3
• It can be seen from this that the local, per step, error = O h .
• Hence the global error for the modified Euler method is O(h2).
• Also called a Runge-Kutta method of order 2
8
Runge-Kutta methods
• The 4th order Runge-Kutta method is popular, and uses several
predictive steps, not just one. The algorithm is discussed in Kreyzig
(pp.1040, 7th Ed.) and is listed in HLT (p.29).
Multi-Step Methods.
9
Multi-Step Methods (2)
• Adams-Bashforth technique
h
y np+1 = yn + (55 f n − 59 f n−1 + 37 f n−2 − 9 f n−3 )
24
h
ycn+1 = yn + (9 f pn+1 + 19 f n − 5 f n−1 + f n−2 )
24
• A-B and 4th R-K are of similar accuracy but A-B is more efficient since
the yn->y(n-2), f(n-1)->f(n-2) can be stored from previous steps and
only two computations of f(x,y) are required per step.
• Many computer algorithms start with 4th R-K to get 4 points and then
change to A-B for speed.
10
Second Order and Higher equations
dY
or = F ( x, Y ) .
dx
11
Example Simple harmonic oscillator
d y y′ y ′0 0
= with initial conditions = .
dx y ′ −y y0 1
function shm(h)
% Euler algorithm to evaluate y''= -y , y(0)= 1 , y'(0) =
0 .
% h is the step size.
x = [0:h:7]'; % x range
fn = cos(x); % exact solution
for i=1:(length(x)-1)
y(i+1) = y(i) + h*yd(i); %Euler algorithm
yd(i+1)=yd(i) - h*y(i);
end
plot(x,fn,x,y,'*')
12
2
1.5
0.5
-0.5
-1
-1.5
0 1 2 3 4 5 6 7
Accuracy is poor with h = 0.2 it improves with either smaller h or, preferably, by
using modified Euler or Runge Kutta.
Summary
13