# Solving partial differential equations (PDEs) fangohr/teaching/... · PDF fileSolving partial di erential equations (PDEs) Hans Fangohr Engineering and the Environment ... This equation

• View
218

0

Embed Size (px)

### Text of Solving partial differential equations (PDEs) fangohr/teaching/... · PDF fileSolving...

• Solving partial differential equations (PDEs)

Hans Fangohr

Engineering and the EnvironmentUniversity of Southampton

United [email protected]

May 3, 2012

1 / 47

• Outline I

1 Introduction: what are PDEs?

2 Computing derivatives using finite differences

3 Diffusion equation

4 Recipe to solve 1d diffusion equation

5 Boundary conditions, numerics, performance

6 Finite elements

7 Summary

2 / 47

• This lecture

tries to compress several years of material into 45 minutes

3 / 47

http://www.soton.ac.uk/~fangohr/teaching/comp6024

• What are partial differential equations (PDEs)

Ordinary Differential Equations (ODEs)one independent variable, for example t in

d2x

dt2= k

mx

often the indepent variable t is the timesolution is function x(t)important for dynamical systems, population growth,control, moving particles

Partial Differential Equations (ODEs)multiple independent variables, for example t, x and y in

u

t= D

(2u

x2+

2u

y2

)solution is function u(t, x, y)important for fluid dynamics, chemistry,electromagnetism, . . . , generally problems with spatialresolution

4 / 47

• 2d Diffusion equation

u

t= D

(2u

x2+2u

y2

)u(t, x, y) is the concentration [mol/m3]

t is the time [s]

x is the x-coordinate [m]

y is the y-coordinate [m]

D is the diffusion coefficient [m2/s]

Also known as Ficks second law. The heat equation has thesame structure (and u represents the temperature).Example:http://www.youtube.com/watch?v=WC6Kj5ySWkQ

5 / 47

• Examples of PDEs

Cahn Hilliard Equation (phase separation)

Fluid dynamics (including ocean and atmospheric models,plasma physics, gas turbine and aircraft modelling)Structural mechanics and vibrations, superconductivity,micromagnetics, . . .

6 / 47

http://en.wikipedia.org/wiki/CahnHilliard_equation

• Computing derivatives

using finite differences

7 / 47

• Overview

Motivation:

We need derivatives of functions for example foroptimisation and root finding algorithmsNot always is the function analytically known (but weare usually able to compute the function numerically)The material presented here forms the basis of thefinite-difference technique that is commonly used tosolve ordinary and partial differential equations.

The following slides show

the forward difference techniquethe backward difference technique and thecentral difference technique to approximate thederivative of a function.We also derive the accuracy of each of these methods.

8 / 47

• The 1st derivative

(Possible) Definition of the derivative (or differentialoperator d

dx)

f (x) dfdx

(x) = limh0

f(x+ h) f(x)h

Use difference operator to approximate differentialoperator

f (x) =df

dx(x) = lim

h0

f(x+ h) f(x)h

f(x+ h) f(x)h

can now compute an approximation of f (x) simply byevaluating f (twice).

This is called the forward difference because we use f(x)and f(x+ h).

Important questions: How accurate is this approximation?

9 / 47

• Accuracy of the forward difference

Formal derivation using the Taylor series of f around x

f(x + h) =

n=0

hnf (n)(x)

n!

= f(x) + hf (x) + h2f (x)

2!+ h3

f (x)

3!+ . . .

Rearranging for f (x)

hf (x) = f(x + h) f(x) h2 f(x)

2! h3 f

(x)

3! . . .

f (x) =1

h

(f(x + h) f(x) h2 f

(x)

2! h3 f

(x)

3! . . .

)=

f(x + h) f(x)h

h2 f

(x)2! h

3 f(x)3!

h . . .

=f(x + h) f(x)

h hf

(x)

2! h2 f

(x)

3! . . .

10 / 47

• Accuracy of the forward difference (2)

f (x) =f(x+ h) f(x)

h hf

(x)

2! h2f

(x)

3! . . .

Eforw(h)

f (x) =f(x+ h) f(x)

h+ Eforw(h)

Therefore, the error term Eforw(h) is

Eforw(h) = hf (x)

2! h2f

(x)

3! . . .

Can also be expressed as

f (x) =f(x+ h) f(x)

h+O(h)

11 / 47

• The 1st derivative using the backward difference

Another definition of the derivative (or differentialoperator d

dx)

df

dx(x) = lim

h0

f(x) f(x h)h

Use difference operator to approximate differentialoperator

df

dx(x) = lim

h0

f(x) f(x h)h

f(x) f(x h)h

This is called the backward difference because we usef(x) and f(x h).How accurate is the backward difference?

12 / 47

• Accuracy of the backward difference

Formal derivation using the Taylor Series of f around x

f(x h) = f(x) hf (x) + h2f(x)

2! h3f

(x)

3!+ . . .

Rearranging for f (x)

hf (x) = f(x) f(x h) + h2 f(x)

2! h3 f

(x)

3! . . .

f (x) =1

h

(f(x) f(x h) + h2 f

(x)

2! h3 f

(x)

3! . . .

)=

f(x) f(x h)h

+h2 f

(x)2! h

3 f(x)3!

h . . .

=f(x) f(x h)

h+ h

f (x)

2! h2 f

(x)

3! . . .

13 / 47

• Accuracy of the backward difference (2)

f (x) =f(x) f(x h)

h+ h

f (x)

2! h2f

(x)

3! . . .

Eback(h)

f (x) =f(x) f(x h)

h+ Eback(h) (1)

Therefore, the error term Eback(h) is

Eback(h) = hf (x)

2! h2f

(x)

3! . . .

Can also be expressed as

f (x) =f(x) f(x h)

h+O(h)

14 / 47

• Combining backward and forward differences (1)

The approximations are

forward:

f (x) =f(x+ h) f(x)

h+ Eforw(h) (2)

backward

f (x) =f(x) f(x h)

h+ Eback(h) (3)

Eforw(h) = hf (x)

2! h2 f

(x)

3! h3 f

(x)

4! h4 f

(x)

5! . . .

Eback(h) = hf (x)

2! h2 f

(x)

3!+ h3

f (x)

4! h4 f

(x)

5!+ . . .

Add equations (2) and (3) together, then the error cancelspartly!

15 / 47

• Combining backward and forward differences (2)

f (x) =f(x + h) f(x)

h+ Eforw(h)

f (x) =f(x) f(x h)

h+ Eback(h)

2f (x) =f(x + h) f(x h)

h+ Eforw(h) + Eback(h)

Eforw(h) + Eback(h) = 2h2f (x)

3! 2h4 f

(x)

5! . . .

The combined (central) difference operator is

f (x) =f(x + h) f(x h)

2h+ Ecent(h)

with

Ecent(h) = h2f (x)

3! h4 f

(x)

5! . . .

16 / 47

• Central difference

Can be derived (as on previous slides) by adding forwardand backward difference

Can also be interpreted geometrically by defining thedifferential operator as

df

dx(x) = lim

h0

f(x+ h) f(x h)2h

and taking the finite difference form

df

dx(x) f(x+ h) f(x h)

2h

Error of the central difference is only O(h2), i.e. betterthan forward or backward difference

It is generally the case that symmetric differencesare more accurate than asymmetric expressions.

17 / 47

• Example (1)

Using forward difference to estimate the derivative off(x) = exp(x)

f (x) f forw =f(x+ h) f(x)

h=

exp(x+ h) exp(x)h

Numerical example:

h = 0.1, x = 1

f (1) f forw(1.0) =exp(1.1)exp(1)

0.1= 2.8588

Exact answers is f (1.0) = exp(1) = 2.71828

(Central diff: f cent(1.0) =exp(1+0.1)exp(10.1)

0.2 = 2.72281)

18 / 47

• Example (2)

Comparison: forward difference, central difference and exactderivative of f(x) = exp(x)

0 1 2 3 4 5x

0

20

40

60

80

100

120

140

df/

dx(x

)

Approximations of df/dx for f(x)=exp(x)

forward h=1central h=1forward h=0.0001exact

19 / 47

• Summary

Can approximate derivatives of f numerically using onlyfunction evaluations of f

size of step h very important

central differences has smallest error term

name formula error

forward f (x) = f(x+h)f(x)h

O(h)backward f (x) = f(x)f(xh)

hO(h)

central f (x) = f(x+h)f(xh)2h

O(h2)

20 / 47

• Appendix: source to compute figure on page 19 I

EPS=1 #very large EPS to provoke inaccuracy

def forwarddiff(f,x,h=EPS):

# df/dx = ( f(x+h)-f(x) )/h + O(h)

return ( f(x+h)-f(x) )/h

def backwarddiff(f,x,h=EPS):

# df/dx = ( f(x)-f(x-h) )/h + O(h)

return ( f(x)-f(x-h) )/h

def centraldiff(f,x,h=EPS):

# df/dx = (f(x+h) - f(x-h))/h + O(h^2)

return (f(x+h) - f(x-h))/(2*h)

if __name__ == "__main__":

#create example plot

import pylab

import numpy as np

a=0 #left and

b=5 #right limits for x

N=11 #steps

21 / 47

• Appendix: source to compute figure on page 19 II

def f(x):

""" Our test funtion with

convenient property that

df/dx = f"""

return np.exp(x)

xs=np.linspace(a,b,N)

forward = []

forward_small_h = []

central = []

for x in xs:

forward.append( forwarddiff(f,x) )

central.append( centraldiff(f,x) )

forward_small_h.append(

forwarddiff(f,x,h=1e-4))

pylab.figure(figsize =(6 ,4))

pylab.axis([a,b,0,np.exp(b)])

pylab.plot(xs,forward ,^,label=forward h=%g%EPS)

pylab.plot(xs,central ,x,label=central h=%g%EPS)

p

Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents