Professional Documents
Culture Documents
Hans Fangohr
Engineering and the Environment
University of Southampton
United Kingdom
fangohr@soton.ac.uk
May 3, 2012
1 / 47
Outline I
1
Diusion equation
Finite elements
Summary
2 / 47
This lecture
3 / 47
2u 2u
+ 2
x2
y
2d Diusion equation
u
=D
t
2u 2u
+
x2 y 2
Examples of PDEs
Cahn Hilliard Equation (phase separation)
6 / 47
Computing derivatives
using nite dierences
7 / 47
Overview
Motivation:
We need derivatives of functions for example for
optimisation and root nding algorithms
Not always is the function analytically known (but we
are usually able to compute the function numerically)
The material presented here forms the basis of the
nite-dierence technique that is commonly used to
solve ordinary and partial dierential equations.
f (x + h) f (x)
df
(x) = lim
h0
dx
h
df
f (x + h) f (x)
f (x + h) f (x)
(x) = lim
h0
dx
h
h
hn
f (x + h) =
n=0
f (n) (x)
n!
= f (x) + hf (x) + h2
f (x)
f (x)
+ h3
+ ...
2!
3!
1
h
f (x)
f (x)
h3
...
2!
3!
f (x + h) f (x) h2
f (x)
f (x)
h3
...
2!
3!
f (x)
f (x)
f (x + h) f (x) h2 2! h3 3!
...
h
h
f (x + h) f (x)
f (x)
f (x)
h
h2
...
h
2!
3!
10 / 47
f (x + h) f (x)
f (x)
f (x)
h
h2
...
h
2!
3!
Eforw (h)
f (x) =
f (x + h) f (x)
+ Eforw (h)
h
f (x) =
f (x + h) f (x)
+ O(h)
h
11 / 47
h0
dx
h
h
This is called the backward dierence because we use
f (x) and f (x h).
How accurate is the backward dierence?
12 / 47
f (x)
f (x)
h3
+ ...
2!
3!
f (x) =
=
=
1
h
f (x)
f (x)
h3
...
2!
3!
f (x) f (x h) + h2
f (x)
f (x)
h3
...
2!
3!
f (x)
f (x)
f (x) f (x h) h2 2! h3 3!
+
...
h
h
f (x) f (x h)
f (x)
f (x)
+h
h2
...
h
2!
3!
13 / 47
f (x)
f (x) f (x h)
f (x)
+h
h2
...
h
2!
3!
Eback (h)
f (x) =
f (x) f (x h)
+ Eback (h)
h
(1)
f (x)
f (x)
h2
...
2!
3!
f (x) f (x h)
+ O(h)
h
14 / 47
f (x + h) f (x)
+ Eforw (h)
h
(2)
f (x) =
f (x) f (x h)
+ Eback (h)
h
(3)
backward
f (x)
f (x)
f (x)
f (x)
h2
h3
h4
...
2!
3!
4!
5!
f (x)
f (x)
f (x)
f (x)
Eback (h) = h
h2
+ h3
h4
+ ...
2!
3!
4!
5!
Eforw (h) = h
Add equations (2) and (3) together, then the error cancels
partly!
15 / 47
f (x + h) f (x)
+ Eforw (h)
h
f (x) f (x h)
+ Eback (h)
h
f (x + h) f (x h)
+ Eforw (h) + Eback (h)
h
Adding the error terms:
2f (x) =
f (x)
f (x)
2h4
...
3!
5!
The combined (central) dierence operator is
Eforw (h) + Eback (h) = 2h2
f (x) =
f (x + h) f (x h)
+ Ecent (h)
2h
with
Ecent (h) = h2
f (x)
f (x)
h4
...
3!
5!
16 / 47
Central dierence
Can be derived (as on previous slides) by adding forward
and backward dierence
Can also be interpreted geometrically by dening the
dierential operator as
df
f (x + h) f (x h)
(x) = lim
h0
dx
2h
and taking the nite dierence form
df
f (x + h) f (x h)
(x)
dx
2h
Error of the central dierence is only O(h2 ), i.e. better
than forward or backward dierence
It is generally the case that symmetric dierences
are more accurate than asymmetric expressions.
17 / 47
Example (1)
Using forward dierence to estimate the derivative of
f (x) = exp(x)
f (x) fforw =
f (x + h) f (x)
exp(x + h) exp(x)
=
h
h
Numerical example:
h = 0.1, x = 1
f (1) fforw (1.0) = exp(1.1)exp(1) = 2.8588
0.1
Exact answers is f (1.0) = exp(1) = 2.71828
(Central di: fcent (1.0) =
exp(1+0.1)exp(10.1)
0.2
= 2.72281)
18 / 47
Example (2)
Comparison: forward dierence, central dierence and exact
derivative of f (x) = exp(x)
140
120
df/dx(x)
100
80
60
40
20
00
19 / 47
Summary
error
O(h)
O(h)
O(h2 )
20 / 47
21 / 47
23 / 47
h0
dx
h
h
Change dierential to dierence operator in
dy
dx
= f (x, y)
y(x + h) y(x)
dy
dx
h
hf (x, y) y(x + h) y(x)
= yi+1 = yi + hf (xi , yi )
f (x, y) =
where
hi =
f (xi )
.
f (xi )
(4)
.
This equation can be derived from the Taylor series of f around x.
Suppose we guess the root to be at x and x + h is the actual
location of the root (so h is unknown and f (x + h) = 0):
f (x + h)
f (x) + hf (x) + . . .
f (x) + hf (x) + . . .
= 0
f (x) + hf (x)
f (x)
.
f (x)
(5)
25 / 47
26 / 47
Diusion equation
2
u
=D
t
2
2
+ 2
x2 y
= Du
2
x2
2
x2
2
x2
+
+
2
y 2
2
y 2
2
z 2
27 / 47
u
t
1d Diusion equation
= D u
x2
u(x,t_0)
(x,t0 )
0
x
6
28 / 47
1d Diusion eqn
u
t
= D u , time integration I
x2
2 u(x, t0 )
x2
We like to solve
u(x, t)
= g(x, t0 )
t
to compute u(x, t1 ) at some later time t1 .
Use nite dierence time integration scheme:
Introduce a time step size h so that t1 = t0 + h.
29 / 47
1d Diusion eqn
u
t
= D u , time integration II
x2
u(x, t)
u(x, t0 + h) u(x, t0 )
= lim
(6)
h0
t
h
u(x, t0 + h) u(x, t0 )
(7)
h
(8)
1d Diusion eqn
u
t
= D u , spatial part I
x2
u
2u
= D 2 = g(x, t)
t
x
2
u(x,t)
Need to compute g(x, t) = D x2 for a given u(x, t).
Can ignore the time dependence here, and obtain
g(x) = D
Recall that
2 u(x)
.
x2
2u
u
=
2
x
x x
u
x
using central
31 / 47
f (x + 1 h) f (x 1 h)
df
2
2
(x)
dx
h
32 / 47
d2 f
:
dx2
d2 f
d df
(x) =
(x)
2
dx
dx dx
1
d f (x + 1 h) f (x 2 h)
2
dx
h
1 d
1
d
1
=
f x+ h
f x h
h dx
2
dx
2
1 f (x + h) f (x) f (x) f (x h)
h
h
h
f (x + h) 2f (x) + f (x h)
=
(9)
h2
33 / 47
Recipe to solve
1
2
u
t
= D u
x2
3
4
6
7
34 / 47
A sample solution
u
t
= D u, I
x2
import numpy as np
import matplotlib . pyplot as plt
import matplotlib . animation as animation
a , b = -5 ,5
N = 51
x = np . linspace (a ,b , N )
h = x [1] - x [0]
# size of box
# number of s u b d i v i s i o n s
# p o s i t i o n s of s u b d i v i s i o n s
# d i s c r e t i s a t i o n s t e p s i z e in x - d i r e c t i o n
def total ( u ):
""" C o m p u t e s total number of moles in u . """
return (( b - a )/ float ( N )* np . sum ( u ))
def gaussdistr ( mean , sigma , x ):
""" Return gauss d i s t r i b u t i o n for given numpy array x """
return 1./( sigma * np . sqrt (2* np . pi ))* np . exp (
-0.5*( x - mean )**2/ sigma **2)
# s t a r t i n g c o n f i g u r a t i o n for u (x , t0 )
u = gaussdistr ( mean =0. , sigma =0.5 , x = x )
35 / 47
A sample solution
u
t
= D u , II
x2
def compute_g ( u , D , h ):
""" given a u (x , t ) in array , compute g (x , t )= D * d ^2 u / dx ^2
using central d i f f e r e n c e s with spacing h ,
and return g (x , t ). """
d2u_dx2 = np . zeros ( u . shape , np . float )
for i in range (1 , len ( u ) -1):
d2u_dx2 [ i ] = ( u [ i +1] - 2* u [ i ]+ u [i -1])/ h **2
# special cases at b o u n d a r y : assume Neuman b o u n d a r y
# conditions , i . e . no change of u over b o u n d a r y
# so that u [0] - u [ -1]=0 and thus u [ -1]= u [0]
i =0
d2u_dx2 [ i ] = ( u [ i +1] - 2* u [ i ]+ u [ i ])/ h **2
# same at other end so that u [N -1] - u [ N ]=0
# and thus u [ N ]= u [N -1]
i = len ( u ) -1
d2u_dx2 [ i ] = ( u [ i ] - 2* u [ i ]+ u [i -1])/ h **2
return D * d2u_dx2
def advance_time ( u , g , dt ):
""" Given the array u , the rate of change array g ,
and a t i m e s t e p dt , compute the s o l u t i o n for u
after t , using simple Euler method . """
u = u + dt * g
36 / 47
A sample solution
u
t
= D u , III
x2
return u
# show example , quick and dirtly , lots of global v a r i a b l e s
dt = 0.01
# step size or time
s t e p s b e f o r e u p d a t i n g g r a p h = 20 # p l o t t i n g is slow
D = 1.
# Diffusion coefficient
stepsdone = 0
# keep track of i t e r a t i o n s
def do_steps (j , nsteps = s t e p s b e f o r e u p d a t i n g g r a p h ):
""" F u n c t i o n called by F u n c A n i m a t i o n class . C o m p u t e s
nsteps iterations , i . e . carries forward s o l u t i o n from
u (x , t_i ) to u (x , t_ { i + nsteps }).
"""
global u , stepsdone
for i in range ( nsteps ):
g = compute_g ( u , D , h )
u = advance_time ( u , g , dt )
stepsdone += 1
time_passed = stepsdone * dt
print ( " stepsdone =%5 d , time =%8 gs , total ( u )=%8 g " %
( stepsdone , time_passed , total ( u )))
l . set_ydata ( u )
# update data in plot
fig1 . canvas . draw () # redraw the canvas
37 / 47
A sample solution
u
t
= D u , IV
x2
return l ,
fig1 = plt . figure ()
l ,= plt . plot (x ,u , b - o )
# setup a n i m a t i o n
# plot initial u (x , t )
# then compute s o l u t i o n and animate
line_ani = animation . FuncAnimation ( fig1 ,
do_steps , range (10000))
plt . show ()
38 / 47
Boundary conditions I
39 / 47
Boundary conditions II
40 / 47
Numerical issues
The time integration scheme we use is explicit because we
have an explicit equation that tells us how to compute
u(x, ti+1 ) based on u(x, ti ) (equation (8) on slide 30)
An implicit scheme would compute u(x, ti+1 ) based on
u(x, ti ) and on u(x, ti+1 ).
The implicit scheme is more complicated as it requires
solving an additional equation system just to nd
u(x, ti+1 ) but allows larger step sizes t for the time.
The explicit integration scheme becomes quickly unstable
if t is too large. t depends on the chose spatial
discretisation x.
41 / 47
Performance issues
Solutions:
Refactor for-loops into matrix operations and use
(compiled) matrix library (numpy for small systems, use
scipy.sparse for larger systems)
Use library function to carry out time integration (will
use implicit method if required), for example
scipy.integrate.odeint.
42 / 47
Finite Elements
Another widely spread way of solving PDEs is using so-called
nite elements.
Mathematically, the solution u(x) for a problem like
2u
= f (x) is written as
x2
N
u(x) =
ui i (x)
(10)
i=1
43 / 47
Finite dierences
are mathematically much simpler and
for simple geometries (such as cuboids) easier to
program
Finite elements
have greater exibility in the shape of the domain,
the specication and implementation of boundary
conditions is easier
but the basic mathematics and code is more
complicated.
44 / 47
45 / 47
Summary
Partial dierential equations important in many contexts
If no analytical solution known, use numerics.
Discretise the problem through
nite dierences (replace dierential with dierence
operator, corresponds to chopping space and time in
little cuboids)
nite elements (project solution on localised basis
functions, often used with tetrahedral meshes)
related methods (nite volumes, meshless methods).
46 / 47
changeset:
tag:
user:
date:
53:a22b7f13329e
tip
Hans Fangohr [MBP13] <fangohr@soton.ac.uk>
Fri Dec 16 10:57:15 2011 +0000
47 / 47