You are on page 1of 54

Lecture 8:

Stiff systems and Finite Differences


J.K. Ryan@tudelft.nl
WI3097TU
Delft Institute of Applied Mathematics
Delft University of Technology

22 October 2012

()

Stiff systems and Finite Differences

22 October 2012

1 / 54

Outline

Review
Application of methods
Truncation error

Stiff Differential Equations

Finite Difference Methods

()

Stiff systems and Finite Differences

22 October 2012

2 / 54

Global vs. Local Truncation Error

The global truncation error is related to the local truncation


error by
en+1 = hn+1 + Q(h)en
which can further be written as
en+1 = h

n
X

[Q(h)]j n+1j

j=0

()

Stiff systems and Finite Differences

22 October 2012

3 / 54

Higher Order Methods


4th order Runge-Kutta (RK4)

The 4th order Runge-Kutta scheme is:


k1 = hf (tn , wn ) (Forward Euler)
k1
h
k2 = hf (tn + , wn + )
2
2
h
k2
k3 = hf (tn + , wn + )
2
2
k4 = hf (tn + h, wn + k3 )
RK4 is the average of these slopes
wn+1 = wn +

()

1
(k1 + 2(k2 + k3 ) + k4 )
6

Stiff systems and Finite Differences

22 October 2012

4 / 54

Higher Order Methods


4th order Runge-Kutta (RK4)

RK4:
Uses an average of different slopes (called predictors).
The amplification factor is
1
1
1
Q(h) = 1 + h + (h)2 + (h)3 + (h)4
2
6
24
The stability condition is
h<

2.8
||

This is a 4th order method, n+1 O(h4 ).

()

Stiff systems and Finite Differences

22 October 2012

5 / 54

ODE Systems

Numerical methods for systems are derived similar to the scalar


case, but we use vector notation:
Test system:
y0 =
A
y + g(t)
|{z}
A k k matrix

where

y=

y1 (t)
y2 (t)

yk (t)

is a vector of length k (as is g(t)) .

()

Stiff systems and Finite Differences

22 October 2012

6 / 54

ODE Systems
Euler Forward

Euler forward for systems is written as:


wn+1 = wn + hf(tn , wn ).
Or, componentwise:
(j)

wn+1 = wn(j) + hf (i) (tn , wn(1) , wn(2) , . . . , wn(k) ),


for j = 1, . . . , k.
which is just the scalar method.
wn+1 =

(I + hA)
|

{z

wn + hg(tn )

Amplification matrix

()

Stiff systems and Finite Differences

22 October 2012

7 / 54

ODE Systems
Euler Backward

Backward Euler can be written as


wn+1 = wn + hf(tn+1 , wn+1 ).
Or
wn+1 =

(I hA)1
|

{z

(wn + hg(tn+1 ))

Amplification matrix

()

Stiff systems and Finite Differences

22 October 2012

8 / 54

ODE Systems
Truncation Error

The application of single-step methods to ODE systems is done


componentwise.
Similarly, the global and local truncation error is also
componentwise.
(j)

n+1 =
(j)

1 (j)
(j)
(y (tn+1 ) zn+1 )
h
(j)

en+1 = y (j) (tn+1 ) wn+1

(Local)

(Global)

for j = 1, . . . , k.
Note: This is the coupled global truncation error.

()

Stiff systems and Finite Differences

22 October 2012

9 / 54

ODE Systems
Truncation Error

The relation between global and local truncation error depends


on the numerical method.
For Forward Euler, we have
en+1 = hn+1 +

(I + hA)
|

{z

en

Amplification matrix

This is the coupled global truncation error. Given, A = SS 1 ,


and n+1 = S 1 en+1 . this is
n+1 = hn+1 + (I + h)n
This is the equation for the decoupled global error.

()

Stiff systems and Finite Differences

22 October 2012

10 / 54

ODE Systems
Stability

For stability of the system, the eigenvalues,


j = + i,
must have negative real part,
< 0.

()

Stiff systems and Finite Differences

22 October 2012

11 / 54

ODE Systems
Stability

For the Euler Forward method, the requirement that


|Q(h)| = |I + hA| < 1
means that each eigenvalue must satisfy
(1 + h)2 + (h)2 < 1.
The system is stable if
h < min
j

()

2|(j) |
,
((j) )2 + ( (j) )2

j = 1, . . . , k

Stiff systems and Finite Differences

22 October 2012

12 / 54

Stiff Differential Equations


Test Equation

For stiff equations, consider the equation


y 0 = (y F (t)) + F 0 (t)
y (0) = y0
where < 0 and F (t) is smooth.
This has solution
y (t) = (y0 F (0))e t +
|

()

{z

rapidly decays

F (t)
| {z }

quasi-stationary

Stiff systems and Finite Differences

22 October 2012

13 / 54

Stiff Differential Equations


Recall, for stability: |Q(h)| < 1
Forward or Modified Euler: h < 2/||
RK4: h < 2.8/||
The global truncation error at step n + 1 is given by
:0

:0

n1
[Q(h)]
en+1 =h
[Q(h)]
n 1 + h
 2 + . . .

+ hQ(h)n1 + h

n
|{z}

has more influence

because
lim [Q(h)]n = 0.

()

Stiff systems and Finite Differences

22 October 2012

14 / 54

Stiff Differential Equations

lim en+1 = hn

For stiff equations


Global truncation errors adapts to local truncation error.
Gets smaller after passing through a transient region.

()

Stiff systems and Finite Differences

22 October 2012

15 / 54

Stiff Differential Equations


Another way to look at this:
Consider a system with the solution as
y(t) = 1 (t)e 10t + 1 (t)e 1000t
The eigenvalues are 1 = 10 and 2 = 1000. The stability
of Forward Euler and Runge-Kutta is given as

Forward Euler
RK4

()

1 = 10
h < 2/| 10|
h < 2.8/| 10|

Stiff systems and Finite Differences

2 = 1000.
h < 2/| 1000|
h < 2.8/| 1000|

22 October 2012

16 / 54

Stiff Differential Equations


Recall: The stability is given by the minimum h for all
eigenvalues.
Prohibitively restrictive
Requires more operations (smaller interval subdivisions)
More possibility for roundoff error
But is would be easier to integrate if we have a larger stability
region.
Therefore, we consider implicit methods.

()

Stiff systems and Finite Differences

22 October 2012

17 / 54

Stiff Differential Equations


y0 = Ay
Trapezoidal method:
h
wn+1 =wn + (f (tn , wn ) + f (tn+1 , wn+1 ))
2
h
=wn + (Awn + Awn+1 )
2
h
h
(I A)wn+1 =(I + A)wn
2
2

This is the coupled approximation.

()

Stiff systems and Finite Differences

22 October 2012

18 / 54

Stiff Differential Equations

If A is diagonalizable, then A = SS 1 . Then


h
(I A)wn+1
2
h
(I SS 1 )wn+1
2
h
S 1 (I SS 1 )wn+1
2
h
1
(S S 1 )wn+1
2

()

h
=(I + A)wn
2
h
=(I + SS 1 )wn
2
h
=S 1 (I + SS 1 )wn
2
h 1
1
=(S + S )wn
2

Stiff systems and Finite Differences

22 October 2012

19 / 54

Stiff Differential Equations


This simplifies to
h
h
(S 1 S 1 )wn+1 =(S 1 + S 1 )wn
2
2
h
h
(I )S 1 wn+1 =(I + )S 1 wn
2
2
Let n+1 = S 1 wn+1 . Then
h
h
(I )n+1 = (I + )n
2
2
So that the decoupled scheme is
h
= I
2


n+1

()

1 

h
I + n
2

Stiff systems and Finite Differences

22 October 2012

20 / 54

Stiff Differential Equations


For the j th component,
n+1 =

1 h2 j
1 + h2 j

For stability, we need



1


1 +

or

h

2 j

h

2 j

< 1,

h
h
|1 j | < |1 + j |,
2
2

()

j = 1, . . . , k.

Stiff systems and Finite Differences

22 October 2012

21 / 54

Stiff Differential Equations


Assume that j = + i. Then the stability condition is
h
h
|1 ( + i)| < |1 + ( + i)|,
2
2

j = 1, . . . , k,

which is the same as


h
1+
2

2

2

h
< 1
2


2

2

which always happens if < 0.


The Trapezoidal method is unconditionally stable.

()

Stiff systems and Finite Differences

22 October 2012

22 / 54

Stiff Differential Equations


Performing the same analysis for Backward Euler, the stability
condition is:



1

|Q(h)| =
< 1,
1 hj
For complex eigenvalues, j = + i, this translates to
1
< 1,
(1 + h)2 + (h)2
which always happens when < 0.
The Backward Euler method is also unconditionally stable.

()

Stiff systems and Finite Differences

22 October 2012

23 / 54

Stiff Differential Equations

Actually, we can say something more about theses implicit


methods.

Definition (Superstability)
A numerical method is called superstable if
lim

()

|Q(h)| < 1.

Stiff systems and Finite Differences

22 October 2012

24 / 54

Stiff Differential Equations


For the Trapezoidal method,

1

lim |Q(h)| = lim
h
h 1 +

1

2 h

1

2 h

lim



1/2


1/2
| {z }

=1

Lhospitals rule

NOT superstable

For the Backward Euler method,


lim

|Q(h)| =



1

=0
h 1 h

lim

()

Stiff systems and Finite Differences

Superstable

22 October 2012

25 / 54

Stiff Differential Equations


Plots of the amplification
factor,
Q(h) =

1 12 h
1+ 12 h

for the Trapezoidal method


and
Q(h) =

1
1h

for Backward Euler.


Backward Euler is superstable.

()

Stiff systems and Finite Differences

22 October 2012

26 / 54

Finite Difference Methods for Boundary Value Problems


Application: Stationary heat conduction
T (x ) = temperature in a bar
(K)
Q(x ) = heat production
(J/m3 s)
= heat condition coefficient
Model equation
d 2T
= Q(x ),
dx 2
T (0) = T0 ,

0 < x < L,

T (L) = TL
Alternatively, we could give the heat flow at one end:
A
()

dT
(L) = AqL .
dx

Stiff systems and Finite Differences

22 October 2012

27 / 54

Finite Difference Methods

General second order boundary value problem (BVP):


(p(x )y 0 )0 + r (x )y 0 + q(x )y = f (x ),

xL < x < xR

a0 y (xL ) + a1 y (xL ) = cL
b0 y (xR ) + b1 y 0 (xR ) = cR
where p(x ) > 0 and q(x ) > 0.

()

Stiff systems and Finite Differences

22 October 2012

28 / 54

Finite Difference Methods


Note: We have a nonunique solution if
a0 = b0 = 0,
q(x ) = 0

(p(x )y 0 )0 + r (x )y 0 = f (x ),

xL < x < xR

a1 y (xL ) = cL
b1 y 0 (xR ) = cR

Non-uniqueness means the equation is not well-posed.

()

Stiff systems and Finite Differences

22 October 2012

29 / 54

Finite Difference Methods

Possible boundary conditions


a0 y (xL ) + a1 y 0 (xL ) = cL
b0 y (xR ) + b1 y 0 (xR ) = cR

Dirichlet: a0 y (xL ) = cL a1 = 0
Neumann: b1 y 0 (xR ) = cR b0 = 0
Robin: a0 6= 0, b0 6= 0 (linear combination of Dirichlet &
Neumann)

()

Stiff systems and Finite Differences

22 October 2012

30 / 54

Finite Difference Methods

Begin with the easy case: r (x ) = 0, p(x ) = 1, and Dirichlet


BC.
y 00 + q(x )y = f (x ),

xL < x < xR ,

y (xL ) = y (xR ) = 0
Take xL = 0 and xR = 1.

()

Stiff systems and Finite Differences

22 October 2012

31 / 54

Finite Difference Methods


Step 1

Subdivide the domain, [xL , xR ] = [0, 1] into N + 1 equal


elements:
h=

()

xR xL
1
Domain length
=
=
=h
Number elements
N +1
N +1

Stiff systems and Finite Differences

22 October 2012

32 / 54

Finite Difference Methods


Step 2

Define the nodal points as


xj = xL + jh,

j = 0, . . . , N + 1

which gives
xj = jh,

j = 0, . . . , N + 1

Note: x0 = xL = 0 and xN+1 = xR = 1

()

Stiff systems and Finite Differences

22 October 2012

33 / 54

Finite Difference Methods


Step 3

Discretize the equation, y 00 + q(x )y = f (x ).


Denote yj = y (xj ), qj = q(xj ), fj = f (xj ).
Need to approximate y 00 (x )
Choose 2nd order divided difference
yj00

()

wj+1 2wj + wj1


.
h2

Stiff systems and Finite Differences

22 October 2012

34 / 54

Finite Difference Methods


Step 3

The approximation to the equation, y 00 + q(x )y = f (x ),


becomes

()

1
(wj+1 2wj + wj1 ) + qj wj = fj
h2
w0 = wN+1 = 0

Stiff systems and Finite Differences

j = 1, . . . , N

22 October 2012

35 / 54

Finite Difference Methods

Plug in j = 1 :

1
(w2 2w1 + w0 ) + q1 w1 = f1
|{z}
h2
=0

()

1
2 (w2 2w1 ) + q1 w1 = f1
h

Stiff systems and Finite Differences

22 October 2012

36 / 54

Finite Difference Methods

Plug in j = N :

1
(wN+1 2wN + wN1 ) + qN wN = fN
h2 | {z }
=0

()

1
(2wN wN1 ) + qN wN = fN
h2

Stiff systems and Finite Differences

22 October 2012

37 / 54

Finite Difference Methods

Plug in j = 2, . . . , N 1 :

1
(wj+1 2wj + wj1 ) + qj wj = fj
h2

To solve this, we rewrite as a linear system:


Aw = f
where A is a N N matrix and w and f are vectors of length N

()

Stiff systems and Finite Differences

22 October 2012

38 / 54

Finite Difference Methods


Step 4

To find the form of A, write all the equations together


1
(2w1 w2 ) + q1 w1 = f1 ,
h2

1
(wj+1 2wj + wj1 ) + qj wj = fj ,
h2
1
2 (wN1 2wN ) + qN wN = fN ,
h

()

Stiff systems and Finite Differences

j =1
j = 2, . . . , N 1
j = N.

22 October 2012

39 / 54

Finite Difference Methods

Aw = f
with
A=
where

w=

()

w1
w2

wN1
wN

1
K +M
h2

f1
f2

f=

fN1

Stiff systems and Finite Differences

fN

22 October 2012

40 / 54

Finite Difference Methods

A=

1
K +M
h2

where
M = diag(q1 , q2 , . . . , qN )
and K is a tri-banded symmetric diagonal matrix

K =

()

2
1
0

0
0

1 0
0 0
2 1 0 0
1 2 1 0

0 1 2 1
0
0 1 2

Stiff systems and Finite Differences

22 October 2012

41 / 54

Example

Example
Consider the boundary value problem
y 00 (x ) = sin(x ),
y (0) = 0,

x [0, ],

y () = 0.

Determine the exact solution. Determine the numerical solution


w with N = 2. Compute y w.

()

Stiff systems and Finite Differences

22 October 2012

42 / 54

Example
Exact solution

We can integrate twice to determine the exact solution:


y 00 (x ) = sin(x )
y 0 (x ) =

sin(x ) dx = cos(x ) + C1

y (x ) =

(cos(x ) + C1 ) dx = sin(x ) + C1 x + C2 .

Plugging in the boundary values:


y (x = 0) = y (0) = sin(0) + C1 0 + C2 = 0,
y (x = ) = y (pi) = sin() + C1 + C2 = 0,
gives C1 = C2 = 0. So that the exact solution is
y (x ) = sin(x ).
()

Stiff systems and Finite Differences

22 October 2012

43 / 54

Example
Step 1: Subdivide the domain

In this case, xL = 0 and xR = .


We need to subdivide the domain using N = 2 :
h=

xR xL
=
= .
N +1
3
3

So that
h=

()

Stiff systems and Finite Differences

22 October 2012

44 / 54

Example
Step 2: Nodal Points

Now, we can define the nodal points. These are defined as


xj = xL + jh,

j = 0, . . . , N + 1

In this case, we have

xj = j ,
3

j = 0, . . . , 3

Or,
x0 = 0,

()

x1 =

,
3

x2 =

2
,
3

Stiff systems and Finite Differences

x3 = .

22 October 2012

45 / 54

Example
Step 3: Discretize Equation

Now, we must discretize the equation,


y 00 (x ) = sin(x ).
To do so, we need to approximate the second derivative, y(x).
Let us choose central differences,
y 00 (x )

()

1
(wj+1 2wj + wj1 )
h2

Stiff systems and Finite Differences

22 October 2012

46 / 54

Example
Step 3: Discretize Equation

The discretized equation is then

1
(wj+1 2wj + wj1 ) = sin(xj ),
h2

j = 0, . . . , 3

with boundary conditions


w0 = 0,

()

w3 = 0.

Stiff systems and Finite Differences

22 October 2012

47 / 54

Example
Step 3: Discretize Equation

Let us be more exact: for j = 1 we have

1
(w2 2w1 + w0 ) = sin(x1 ),
h2

with w0 = 0 and sin(x1 ) = sin

. Thus, we can write this as

h2 .
3

 

(2w1 w2 ) = sin

()

Stiff systems and Finite Differences

22 October 2012

48 / 54

Example
Step 3: Discretize Equation

For j = 2 we have

1
(w3 2w2 + w1 ) = sin(x2 ),
h2

with w3 = 0 and sin(x2 ) = sin

2
3

. Then this becomes

2
(2w2 w1 ) = sin
h2
3


()

Stiff systems and Finite Differences

22 October 2012

49 / 54

Example
Step 4: Matrix Equation

We then have the system of equations


Aw = f
where

"

w=

()

w1
w2

"

f=

sin 3 
sin 2
3

Stiff systems and Finite Differences

 #

22 October 2012

50 / 54

Example
Step 4: Matrix Equation

A is a 2 2 matrix:
"

A=

2 1
1 2

So that we need to solve


"

()

2 1
1 2

#"

w1
w2

"

sin 3 h2
2
sin 2
3 h

Stiff systems and Finite Differences

22 October 2012

51 / 54

Example
Step 5: The approximation

The approximation is then


"

w1
w2

1
=
3

"

2 1
1 2

#"

sin 3 h2
2
sin 2
3 h


0.9497
0.9497

Plugging in h = 3 , we have
"

()

w1
w2

"

3
2
3
2

2
3
2
3

 #

"

Stiff systems and Finite Differences

22 October 2012

52 / 54

Example
Step 6: The errors

Thus we have

y=

y0
y1
y2
y3

0 

sin

3
=
sin 2
3

=
3

The error is then

yw =

()

y0
y1
y2
y3

w0
w1
w2
w3


3

2
=

23

0
0

0.9497
0.0837

0.9497 0.0837

Stiff systems and Finite Differences

22 October 2012

53 / 54

Material addressed

Review
Application of methods
Truncation error

Stiff Differential Equations

Finite Difference Methods

()

Material in book:
Chapter 6, Sections 9-10
Chapter 7, Sections 1-2
Useful exercises:
Ch 6: 1-11; Ch7: 2a, 3

Stiff systems and Finite Differences

22 October 2012

54 / 54

You might also like