Transcript
Page 1: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Introduction to Mathematical Modelling:Ordinary Differential Equations and Heat Equations

Knut–Andreas Lie

SINTEF ICT, Dept. Applied Mathematics

INF2340 / Spring 2005 – p. 1

Page 2: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Overview

1. Ordinary differential equations• Example of ODE models

− radioactive decay, Newton’s second law, populationmodels

• Numerical solution of ODEs− forward and backward Euler, Runge-Kutta methods

2. Heat equations• from physical problem to simulator code• steady heat conduction

− finite differences, linear algebra• unsteady heat conduction

− finite differences, boundary conditions

INF2340 / Spring 2005 – p. 2

Page 3: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Ordinary Differential Equations (ODEs)

y(m) = f(

t, y, y, y, . . . , y(n−1))

Simple example: radioactive decay

Given a quantity q of a radioactive matter, which decays ata certain constant rate k. The model reads

dq(t)

dt= r(t) = −k · q.

Solution: q(t) = q(t0)e−k(t−t0).

In general: finding a solution is not so easy, although there areapproaches in certain special cases −→ numerical approach!

INF2340 / Spring 2005 – p. 3

Page 4: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

ODEs cont’d

Very simple example from high school physics:

Consider the motion of a body with mass m under constantforce f , which is initially at rest at position x0.

Newton’s second law reads

f = ma = mx(t)

where x(t) is the position of the body at time t. Thecorresponding ODE then reads

f = mx(t), x(0) = x0, x(0) = 0

This equation is easily integrated

x(t) = x0 + f2m t2.

INF2340 / Spring 2005 – p. 4

Page 5: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Population models

Consider the dynamics of a single species (isolated or with nopredators)

• constant birth rate b per time and individual• constant death rate d per time and individual• hence, constant growth rate λ = b − d

The model (Maltus, 1798):

p(t) = λ · p(t)

Solution p(t) = p0eλt predicts exponential growth or decay.

INF2340 / Spring 2005 – p. 5

Page 6: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Population models cont’d

Is there any realism in this?• between 1700 and 1960: growth rate of about 0.02,

population doubles in 34.67 years• generally: limited resources on earth slows down growth

More realism (Verhulst et al., 19th century):• linear rates: b(t) = b0 − b1p(t), d(t) = d0 − d1p(t)

−→ new model:p(t) = −k(p(t) − p∞)

Solution:p(t) = p∞ + (po − p∞)e−kt

INF2340 / Spring 2005 – p. 6

Page 7: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Population models cont’d

What about realism now?• Populations tend to follow a S-shape (logistic model)• New model: p(t) = a · p(t) − b · p2(t)

• New solution: p(t) = a·p0

b·p0+(a−b·p0)e−at

And so on ... adding more than one species (predator–pray) ...

Do we have equilibrium, is it attractive or repellent, ...?

INF2340 / Spring 2005 – p. 7

Page 8: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Population models cont’d

An ODE model from modern research, describing the dynamics ofHIV-1 infection in vivo (Perelson& Nelson, SIAM Review 41/1, 1999) :

The rate of change of uninfected cells T , productively infected cells T ∗,and virus V :

dT

dt= s + pT

`

1 − T/Tmax´

− dT T − kV T

dT ∗

dt= kV T − δT ∗

dV

dt= NδT ∗

− cV.

Here:

dT – death rate of uninfected cells

δ – death rate of infected cells

p – rate of proliferation (continuous development of cells in tissue)

N – virus production per infected cell

c – clearance rate

INF2340 / Spring 2005 – p. 8

Page 9: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Numerical solution of ODEs – Euler’s method

We wish to solve the equation:

y′ = f(y, t), y(a) = α, a ≤ t ≤ b

Obvious solution – use finite differences:• generate a mesh: ti = a + ih, for each i = 0, . . . , N , where

h = (b − a)/N is called stepsize• apply forward differences to the equation

y(ti+1) = y(ti) + hf(y(ti), ti), i = 0, . . . , N − 1

This gives an explicit formula for each y(ti+1) once y0 is known.

INF2340 / Spring 2005 – p. 9

Page 10: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Example: Euler’s method for u′ = 3u

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

2

4

6

8

10

12

14

16

18

20Exact h=1/4 h=1/16h=1/64

INF2340 / Spring 2005 – p. 10

Page 11: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Another Euler method – backward Euler

Once again we consider:

y′ = f(y, t), y(a) = α, a ≤ t ≤ b

and introduce a mesh: ti = a + ih, for each i = 0, . . . , N

This time we apply backward differences

y(ti+1) = y(ti) + hf(y(ti+1), ti+1), i = 0, . . . , N − 1

This gives an equation for each y(ti+1) once y0 is known.

INF2340 / Spring 2005 – p. 11

Page 12: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Two different methods

• forward Euler: explicit method

y(ti+1) = y(ti) + hf(y(ti), ti)

the new value is given by a formula

• backward Euler: implicit method

y(ti+1) = y(ti) + hf(y(ti+1), ti+1)

the new value is given by an algebraic equation

INF2340 / Spring 2005 – p. 12

Page 13: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Example: u′ = −4π sin(4πx)

u(xi) = u(xi−1) − 4π sin(4πxi−1) u(xi) = u(xi−1) − 4π sin(4πxi)

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

−1

−0.8

−0.6

−0.4

−0.2

0

0.2

0.4

0.6

0.8

1 Exact Forward Euler Backward Euler

INF2340 / Spring 2005 – p. 13

Page 14: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Discretisation errors

When using finite differences in forward Euler, we make a localdiscretisation error at each point

y(ti+1) − y(ti)

h= f(y(ti), ti) + error

Consider y = f(y). Expanding y(ti+1) by a Taylor polynomial:

y(ti) + hy(ti) + y(τ) 12h2 − y(ti) = hf(y(ti)) + h · error

for ti ≤ τ ≤ ti+1. Now since y(ti) = f(y(ti)),

error = y(τ) · 12h =

d

dtf(y(τ)) · 1

2h

We say that forward Euler is a first-order method.

INF2340 / Spring 2005 – p. 14

Page 15: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Discretisation errors cont’d

We say that the scheme is consistent if

l(h) = maxt∈[a,b]

y(t + h) − y(t)

h− f(y(t), t)

∣→ 0 as h → 0

Similarly, we define the global discretisation error as

e(h) = maxti∈[a,b]

∣yi − y(ti)∣

∣,

where y(ti) is the exact solution and yi is computed by our scheme.

The scheme is convergent if

e(h) → 0 for t → 0

INF2340 / Spring 2005 – p. 15

Page 16: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Higher order methods – Runge–Kutta

Consider the forward Euler method and try to evaluate f(·) at someother point

yi+1 = yi + hf(yi + β).

Let us repeat the error analysis

y(ti+1) − y(ti) = f(yi)h + f(yi)12h2 + f(τ) 1

6h3

= hf(yi + β) + h · error

Assume now that the error equals 16 f(τ)h2. Now f(yi) = f ′(yi)f(yi)

and we have

f(yi) + f ′(yi)f(yi)12h = f(yi + β)

= f(yi) + βf ′(yi) + β2 + . . .

This means that β = 12h f(yi).

INF2340 / Spring 2005 – p. 16

Page 17: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Runge-Kutta methods cont’d

Thus, we have a new second-order method

yi+1 = yi + hf(

yi + 12h f(yi)

)

There are other alternatives also, e.g.,

yi+1 = yi + 12h

[

f(yi) + f(

yi + hf(yi))]

The Runge-Kutta methods are all on the form:

• Approximate the solution at a point ti ≤ τ ≤ ti+1 by the intermediate stepw = yi + (τ − ti)f(yi)

• Combine yi, w, f(yi), f(w) to get a more accurate solution yi+1.

For higher-order methods we use more intermediate steps.

INF2340 / Spring 2005 – p. 17

Page 18: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

From ODEs to PDEs

So far, we have used ODEs:• involve derivatives with respect to only one variable• if the unknown function depends on more variables, these

have been treated as constants

Now, partial differential equations (PDEs):• involve (partial) derivatives with respect to more than one

variable• the unknown function depends on more than one variable• stationary problems: no time-dependence• unsteady problems. time-dependence, but maybe steady

limit

INF2340 / Spring 2005 – p. 18

Page 19: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Mathematical model: ∂tu = ∆u + f

• Models propagation of heat within a given object• Examples

− a heated rod (1D)− a heated plate or smoothing of images (2D)− heat distribution in a furnace (3D)

• Functions of interest− u(x,t), u(x,y), u(x,y,t), u(x,y,z), u(x,y,z,t), ...

• More generally, heat propagation depends on theconductivity of the material

ut = ∇(K(x, u)∇u) + f(x, t, u)

INF2340 / Spring 2005 – p. 19

Page 20: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Heat conduction in the continental crust

x

sT

x=b

x=0

-Q

• Knowing the temperature at the earth’s surface and the heat flowfrom the mantle, what is the temperature distribution through thecontinental crust?

• Interesting question in geology and geophysics – and for thosenations exploring oil resources...

INF2340 / Spring 2005 – p. 20

Page 21: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Physical and mathematical model• Our prototype differential equation (an ODE in 1D):

−∂2u

∂x2= f(x)

Here u is the temperature.

• Needs to be equipped with boundary conditions

• Very simple equation, but it has applications to− fluid flow in channels− deflection of electric cables− strength analysis of beams− ...

• In multidimensions −∆u = f is the elliptic Poission equation,which is fequently occuring in mathematical models

INF2340 / Spring 2005 – p. 21

Page 22: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Heat conduction in the continental crust cont’d

x

sT

x=b

x=0

-Q

Physical assumptions:

• Crust of infinite area

• Steady state heat flow

• Heat generated by radioactivedecay

Physical quantities:

• u(x) : temperature

• q(x) : heat flux (velocity of heat)

• s(x) : heat release per unit timeand mass

INF2340 / Spring 2005 – p. 22

Page 23: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Derivation of the model

x

x=0

x=b

outflow

inflow

s(x)=R exp(-x/L)

Physical principles:

• First law of thermodynamics:

net outflow of heat = total generated heat

• Fourier’s law: heat flows from hot to cold regions (i.e. heat velocity is poportionalto changes in temperature)

q(x) = −λu′(x)

• Heat generation due to radioactive decay:

s(x) = R exp(−x/L)

INF2340 / Spring 2005 – p. 23

Page 24: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Derivation of the model cont’d

x

s(x) h

q(x-h/2)

q(x+h/2)

x=0

x=b

From the first law of thermodynamics:

q(x + h/2) − q(x − h/2)

h= s(x)

Using a Taylor expansion:

q(x + h/2) − q(x − h/2)

h= q′(x) +

1

24q′′′(x)h2 + . . .

Hence, as h → 0 we have

q′(x) = s(x)

INF2340 / Spring 2005 – p. 24

Page 25: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Derivation of the model cont’d

x

sT

x=b

x=0

-Q

Combining the 1st law of thermodynamics (q′ = s) with Fourier’s law (q = −λu), we get

−d

dx

λdu

dx

«

= s(x)

Boundary conditions:

• u(0) = Ts (at the surface of the earth)

• q(b) = −Q (at the bottom of the crust)

INF2340 / Spring 2005 – p. 25

Page 26: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Mathematical model

−d

dx

(

λdu

dx

)

= Re−x/L, u(0) = Ts, λ(b)u′(b) = −Q

Observe that u depends upon seven parameters:u = u(x; λ, R, L, b, Ts, Q)!

Suppose that we want to investigate the influence of the differentparameters. Assume (modestly) three values of each parameter:−→ Number of possible combinations: 36 = 729.

Using scaling we can reduce the six physical parametersλ, R, L, b, Ts, Q to only two!

INF2340 / Spring 2005 – p. 26

Page 27: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Scaling

We introduce dimensionless quantities (and assume that λ isconstant):

x = xb, u = Ts + Qbu/λ, s(bx) = Rs(x)

This gives

−d2u

dx2= γe−x/β, u = 0,

du

dx(1) = 1

where we have two dimensionless quantities

β = b/L, γ = bR/Q

Dropping the bars, we get an equation on the form

−u′′(x) = f(x), x ∈ (0, 1), u(0) = 0, u′(1) = 1

INF2340 / Spring 2005 – p. 27

Page 28: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Discretisation

• Introduce a grid xi = (i − 1)h

and compute the unknown atgrid points ui = u(xi)

u

u u

u u3

4 51

2

x=1x=0x

• Differential equation fulfilled at each node

−u′′(xi) = f(xi)

• Approximate by standard finite differences

ui+1 − 2ui + ui−1 = −h2fi, i = 1, . . . , n − 1

As opposed to the ODEs we have seen earlier, this is a linearsystem of unknowns

INF2340 / Spring 2005 – p. 28

Page 29: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Discretising boundary conditions• u(0) = 0 simply becomes u1 = 0

• u′(1) = 1 can be approximated as

un+1 − un−1

2h= 1

• Problem: un+1 is not in the mesh!

• Solution: Use the discrete differential equation for i = n:

un−1 − 2un + ui+1 = −h2fn

and the discrete boundary condition to eliminate un+1

• The result is2un−1 − 2un = −2h − h2fn

INF2340 / Spring 2005 – p. 29

Page 30: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Linear system of equations

u1 = 0

u1 −2u2 +u3 = −h2f2

u2 −2u3 +u4 = −h2f3

. . . . . . . . ....

. . . . . . . . ....

. . . . . . . . ....

. . . . . . . . ....

un−2 −2un−1 +un = −h2fn−1

2un−1 −2un = −2h − h2fn

INF2340 / Spring 2005 – p. 30

Page 31: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Using linear algebraWe write the system as Au = b:

2

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

4

1 0 0 0 · · · · · · · · · · · · 0

1 −2 1 0. . .

. . .. . . 0

0 1 −2 1. . .

. . .. . . 0

.... . .

. . .. . .

. . .. . .

......

. . .. . .

. . ....

.... . .

. . .. . .

. . ....

.... . .

. . .. . .

. . .. . .

......

. . .. . .

. . . 1 −2 1

0 · · · · · · · · · · · · · · · 0 2 −2

3

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

5

2

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

4

u1

u2

u3

...

...

...

...

un−1

un

3

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

5

=

2

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

6

4

0

−h2f2

−h2f3

...

...

...

...

−h2fn−1

−2h − h2fn

3

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

7

5

This system can be solved by Gaussian elimination.

INF2340 / Spring 2005 – p. 31

Page 32: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

ImplementationAssembly of matrix in pseudocode:

Matrix( real ) A(n,n)Vector(real ) b(n ), u(n)

// Assemble matrix and left−hand sidefor i=1:n

if i==1 // Left boundaryA(1,1) = 1;b(1) = 0

else if i==n // Right boundaryA(i , i−1) = 2; A(i , i ) = −2;b( i ) = −2∗h − h∗h∗f(x(i ));

else // InteriorA(i , i−1) = 1; A(i , i ) = −2; A(i , i +1) = 1;b( i ) = −h∗h∗f(x(i ));

end

INF2340 / Spring 2005 – p. 32

Page 33: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

In C++int main(int argc, char ∗∗argv){

:double ∗b, ∗u;double ∗∗A;

:b = new double[n];u = new double[n];A = new double∗[n];A [0] = new double[n∗n];for ( i =1; i<n; i++)

A[i ] = A[i−1]+n;

for ( i =0; i<n; i ++) {b[ i ] = u[ i ] = 0.0;for ( j =0; j<n; j++)A[i ][ j ] = 0.0;

}

h = 1.0/( n−1);for ( i =0; i<n; i ++) {

x = i∗h;if ( i==0) {

A[ i ][ i ] = 1; b[ i ] = 0; }else if ( i>0 && i<n−1) {

A[ i ][ i−1] = 1; A[i ][ i ] = −2; A[i ][ i +1] = 1;b[ i ] = h∗h∗(alpha+1)∗pow(x,alpha);

} else {A[ i ][ i−1] = 2; A[i ][ i ] = −2;b[ i ] = −2∗h + h∗h∗(alpha+1)∗pow(x,alpha);

}}solveSys(A, u, b , n);

// output solution ...return 0;

}

INF2340 / Spring 2005 – p. 33

Page 34: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Solution of linear systemAbstract formulation of Gaussian elimination:

• Compute the LU factorization: A = LU, i.e., LUx = b

• Solve Ly = b (forward elimination)

• Solve Ux = y (backward substitution)

LU factorization is feasible since A is tridiagonal.

void solveSys(double ∗∗A, double ∗x, double ∗b, int n) {int i , j , k ; double m;

for ( i =0; i<n−1; i++)for ( j=i+1; j<n; j ++) {

m = A[j ][ i ]/ A[ i ][ i ];for ( k=i ; k<n; k++)

A[j ][ k] −= m∗A[i][k ];b[ j ] −= m∗b[i];A[ j ][ i ] = −m;

}

/∗ Backward substitution ∗/for ( i=n−1; i>=0; i−−) {

x[ i ] = b[ i ];for ( k=i+1; k<n; k++)

x[ i ] −= A[i ][ k]∗x[k ];x[ i ] /= A[i ][ i ];

}

return;}

INF2340 / Spring 2005 – p. 34

Page 35: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Evaluation of algorithm

Observation:

A is tridiagonal, i.e., the only nonzero entries are ai,i−1,ai,i, and ai,i+1

Gaussian elimination:

• is designed for a general dense matrix

• storage requirement: n2 real numbers

• number of operations: O(n3)

Save memory and CPU-time by utilizing the tridiagonal structure.

INF2340 / Spring 2005 – p. 35

Page 36: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Tridiagonal matrices

Tridiagonal matrix:

• nonzero elements: 3n − 2

• in Gaussian elimination:− forward elimination: only need to eliminate lower diagonal− backward substitution: only need to substitute values along

upper diagonal

−→ number of operations is O(n)!

Linear systems arising from the discretization of differential equationstypically contain a lot of zeros (as we saw above). Gaussianelimination therefore performs a lot of unnecessary computions.Generally, one therefore prefers methods that utilize the specialstructure of the linear system.

INF2340 / Spring 2005 – p. 36

Page 37: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Implementation in C++int main(int argc, char ∗∗argv){

:double ∗b, ∗u, ∗Am, ∗Ac, ∗Ap;

:Am = new double[n];Ac = new double[n];Ap = new double[n];

for ( i =0; i<n; i++)b[ i ] = u[ i ] = Am[i] = Ac[i ] = Ap[i ] = 0.0;

h = 1.0/( n−1);

.

for ( i =0; i<n; i ++) {x = i∗h;if ( i==0){

Am[i ] = 0; Ac[i ] = 1; Ap[i ] = 0; b[ i ] = 0; }else if ( i>0 && i<n−1) {

Am[i ] = 1; Ac[i ] = −2; Ap[i ] = 1;b[ i ] = h∗h∗(alpha+1)∗pow(x,alpha);

} else {Am[i] = 2; Ac[i ] = −2; Ap[i ] = 0;b[ i ] = −2∗h + h∗h∗(alpha+1)∗pow(x,alpha);

}}

solveSys(Am, Ac, Ap, u, b, n);:

INF2340 / Spring 2005 – p. 37

Page 38: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Gaussian elimination

void solveSys(double ∗Am, double ∗Ac, double ∗Ap, double ∗x, double ∗b, int n){

int i , j , k ; double m;

/∗ Forward elimination ∗/for ( i =1; i<n; i++) {

m = Am[i]/Ac[i−1];Am[i] = −m; Ac[i] −= m∗Ap[i−1];b[ i ] −= m∗b[i−1];

}

/∗ Backward substitution ∗/i = n−1;x[ i ] = b[ i ]/ Ac[i ];for ( i−−; i>=0; i−−)

x[ i ] = ( b[ i ] − Ap[i]∗x[ i +1])/Ac[i ];return;

}

INF2340 / Spring 2005 – p. 38

Page 39: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Evaluation of program

Advantages:

• Reduced memory requirementn2 double + n double∗ + 1 double∗∗ −→ 3n double + 3 double∗

• Reduced CPU requirements:

Table: CPU time in seconds for grid with n unknowns

125 250 500 1000 2000

heat 0.07 0.6 5.9 48.9 391.5

heatTri 0.01 0.01 0.01 0.03 0.05

Disadvantages:

• We had to rewrite major portions of the code

• Storage scheme for tridiagonal matrix is shown explicit

We will come back to this later in the lecturesINF2340 / Spring 2005 – p. 39

Page 40: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Heat conduction in 2D

• Matrix for the discretization of−∇2 = f :

• Only 5n out of n2 entries arenonzero.

• Storage: store only nonzeroentries 0 5 10 15 20 25 30 35

0

5

10

15

20

25

30

35

nz = 166

You will get to know the discretisation of the 2D operatorintimately in the first assignment...

INF2340 / Spring 2005 – p. 40

Page 41: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

The heat equation ut = ∆u

Two types of boundary conditions

Dirichlet boundary conditions• fix u on part of the boundary

u(x, y, z) = g(x, y, z)

Neumann boundary conditions• fix the normal derivative of u on part of the boundary

∂u

∂n(x, y, z) = f(x, y, z)

⇒ the solution is unique if the boundary data are Dirichlet or amixture of Dirichlet and Neumann

⇒ the solution is determined up to an additive constant for pureNeumann conditions

INF2340 / Spring 2005 – p. 41

Page 42: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Finite-difference methods

ut = uxx, u(0, t) = u(1, t) = 0, u(x, 0) = u0(x)

Introduce a grid in time and space (ih, nk) and define uni = u(ih, nk).

Then we use finite-differences

• Spatial discretisation: central difference as above

∂2u

∂x2≈

uni+1 − 2un

i + uni−1

h2

• Temporal discretisation: forward or backward Euler

∂u

∂t≈

uni − un−1

i

kor

∂u

∂t≈

un+1i − un

i

k

INF2340 / Spring 2005 – p. 42

Page 43: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Finite-difference methods

Forward Euler — explicit marching algorithm (r = k/h2)

un+1i = un

i + r(

uni+1 − 2un

i + uni−1

)

, u0i = u0(ih)

The scheme is stable for r < 1/2, meaning that |uni | may grow

uncontrolled if k is choosen too large −→ possibly very small timesteps.

Backward Euler — solution of linear system

−run+1i−1 + (1 + 2r)un+1

i − run+1i+1 = un

i

The system is written (I − rA)un+1 = un, where A was introduced

above. The scheme is unconditionally stable, meaning that k can bechoosen indenpendent of h. However, the scheme is less accurate forlarge k.

INF2340 / Spring 2005 – p. 43

Page 44: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Boundary conditions (explicit scheme)

Two types of boundaries:

• For Dirichlet conditions we can simply set the value

un0 = g(0, nk), un

m = g(mh, nk)

and not discretise the PDE at the boundary points

• For Neumann conditions, we can either introduce extra cells(ghost cells) outside the domain, for which

∂u

∂n≈

1

2h(un

1 − un−1) = f(0, nk)

−→ un−1 = un

1 − 2hf(0, nk),

or we can modify the stencil

un+10 = un

0 + 2r(un1 − un

0 − hf(0, nk))

INF2340 / Spring 2005 – p. 44

Page 45: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Two spatial dimensions

ut = uxx + uyy

Explicit discretisation

un+1ij = un

ij + r(

uni+1,j + un

i,j+1 − 4uni,j + un

i−1,j + uni,j−1

)

This scheme is stable provided r = k/h2 < 1/4.

The implicit scheme is defined analogously and gives apentadiagonal matrix as seen above.

INF2340 / Spring 2005 – p. 45

Page 46: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Boundary conditions in 2D

Consider Neumann boundary conditions:

∂u

∂n≡ ∇u · n = 0

Assume a rectangular domain. At the vertical (x =constant)boundaries the condition reads:

0 =∂u

∂n= ∇u · (±1, 0) = ±

∂u

∂x

Similarly at the horizontal boundaries (y =constant)

0 =∂u

∂n= ∇u · (0,±1) = ±

∂u

∂y

INF2340 / Spring 2005 – p. 46

Page 47: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Implementing boundary conditions

Consider the left boundary (i = 1, j = 1, . . . , ny). Now, let us apply thefinite difference stencil:

−1 0 1 2 3 4 5 6 7 8 9 10−1

0

1

2

3

4

5

6

7

8

9

10Ghost cells

The computations involve cells outside our domain. This is a problem.The obvious answer is to use the boundary condition, e.g.,

u2,j − u0,j

2∆x= 0 ⇒ u0,j = u2,j

But how do we include this into the scheme..?

INF2340 / Spring 2005 – p. 47

Page 48: IntroductiontoMathematicalModelling ... · Our prototype differential equation (an ODE in 1D): @2u @x2 = f(x) Here u is the temperature. Needs to be equipped with boundary conditions

Implementing boundary conditions cont’d

The two approaches are as before: ghost cells or modifiedstencils

−1 0 1 2 3 4 5 6 7 8 9 10−1

0

1

2

3

4

5

6

7

8

9

10Ghost cells

−1 0 1 2 3 4 5 6 7 8 9 10−1

0

1

2

3

4

5

6

7

8

9

10Modified stencile

Which approach to choose depends upon the application andthe complexity of the boundary.

INF2340 / Spring 2005 – p. 48


Top Related