Heat-conduction/Diffusion Equation

87
MATH 212 NE 217 Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada Copyright © 2011 by Douglas Wilhelm Harder. All rights reserved. Advanced Calculus 2 for Electrical Engineering Advanced Calculus 2 for Nanotechnology Engineering Heat-conduction/ Diffusion Equation

description

Heat-conduction/Diffusion Equation. Outline. This topic discusses numerical solutions to the heat-conduction/ diffusion equation: Discuss the physical problem and properties Examine the equation Approximate solutions using a finite-difference equation Consider numerical stability Examples. - PowerPoint PPT Presentation

Transcript of Heat-conduction/Diffusion Equation

Page 1: Heat-conduction/Diffusion Equation

MATH 212NE 217

Douglas Wilhelm Harder

Department of Electrical and Computer Engineering

University of Waterloo

Waterloo, Ontario, Canada

Copyright © 2011 by Douglas Wilhelm Harder. All rights reserved.

Advanced Calculus 2 for Electrical EngineeringAdvanced Calculus 2 for Nanotechnology Engineering

Heat-conduction/Diffusion Equation

Page 2: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

2

Outline

This topic discusses numerical solutions to the heat-conduction/ diffusion equation:– Discuss the physical problem and properties

– Examine the equation

– Approximate solutions using a finite-difference equation• Consider numerical stability• Examples

Page 3: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

3

Outcomes Based Learning Objectives

By the end of this laboratory, you will:– Understand the heat-conduction/diffusion equation

– Understand how to approximate partial differential equations using finite-difference equations

– Set up solutions in one spatial dimension

– Understand the implementation of insulated boundaries

Page 4: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

4

Motivating Example

Suppose you have a metal bar in contact with a body at 100 °C– If the bar is insulated, over time, the entire length of the bar will be at

100 °C

100 °C0 °C

Page 5: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

5

Motivating Example

At time t = 0, one end of the bar is brought in contact with a heat sink at 0 °C

100 °C0 °C

Page 6: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

6

Motivating Example

At the end closest to the heat sink, the bar will cool rapidly; however, the cooling is not uniform

100 °C0 °C

Page 7: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

7

Motivating Example

Over time, however, the temperature will drop linearly from 100 °C to 0 °C across the bar

100 °C0 °C

Page 8: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

8

The Heat-conduction/Diffusion Equation

The equation that describes the change in temperature over time is given by the partial differential equation

where u(x, t) is a real-valued function of space and time and > 0 is the

thermal diffusivity coefficient heat conduction

diffusivity coefficient diffusion

21u u

t

Page 9: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

9

The Laplacian Equation

The heat-conduction/diffusion equation is a time-independent version of Laplace’s equation

where u(x) is a real-valued function of space

20 u

Page 10: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

10

The Laplacian Operator

The Laplacian operator depends on the dimensions of space argument– In one dimension, and therefore

– In two dimensions, and therefore

– In three dimensions, and therefore

xx2

22

def uu

x

x

y

x

2 22

2 2

def u uu

x y

x

y

z

x2 2 2

22 2 2

def u u uu

x y z

Page 11: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

11

The Laplacian Equation

In Laboratory 1, we saw that the only solution to Laplace’s equation in one dimension with boundary values is a straight line

The solution of

is

2

20

a

b

du x

dxu a u

u b u

b aa

u uu x u x a

b a

Page 12: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

12

The Laplacian Operator

Notice if that u(x, t) already satisfies Laplace’s equation,

then

or

That is, if a solution already satisfies Laplace’s equation, it will not vary over time– In one dimension, if the bar is already linearly varying from 0 to 100,

placing a heat source and sink at each end will not change the temperature

2 0u

210u u

t

10u

t

Page 13: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

13

Applications

Applications of the heat-conduction/diffusion equation include:– Heat equation

– Fick’s laws of diffusion

– Thermal diffusivity in polymers

Steven Byrnes

Oleg Alexandrov

Page 14: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

14

Rate of Change Proportional to Concavity

In one dimension, what does this equation mean?

– Recall we assumed that > 0

– If the function u is concave up at (x, t), the rate of change of u over time will be positive

• That is, it will become less concave up

– If the function u is concave down at (x, t), the rate of change of u over time will be negative

• That is, it will become less concave up

2

2

uu

t x

Page 15: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

15

Rate of Change Proportional to Concavity

We can see this visually:

– If the function u is concave up at (x, t), the rate of change of u over time will be positive

2

20

u

x

2

2

uu

t x

Page 16: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

16

Rate of Change Proportional to Concavity

We can see this visually:

– If the function u is concave down at (x, t), the rate of change of u over time will be negative

2

20

u

x

2

2

uu

t x

Page 17: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

17

Rate of Change Proportional to Concavity

We can see this visually:

– Of course, a function may be concave up and down in different regions

Ultimately, the concavity tends to disappear—that is, as time goes to infinity, u(x, t) becomes a straight line…

2

20

u

x

2

20

u

x

2

2

uu

t x

Page 18: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

18

Examples

Consider heat conduction problem:– A bar initially at 5 °C with one end touching a heat sink also at 5 °C

– At time t = 0, the other end is placed in contact with a heat source at80 °C

Page 19: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

19

Examples

As time passes, the bar warms up

Page 20: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

20

Examples

A plot of the temperature over time across the bar is given by this solution:– The end placed in contact with the 80 °C heats up faster than points

closer to the heat sink

– As the concavity becomes smaller, the rate of change also gets smaller

Page 21: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

21

Examples

A plot of the temperature over time across the bar is given by this solution:– The end placed in contact with the 80 °C heats up faster than points

closer to the heat sink

– As the concavity becomes smaller, the rate of change also gets smaller

Page 22: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

22

Examples

Consider another example:– A bar initially at 100 °C is placed at time t = 0 in contact with a heat

source of 80 °C at one end and a heat sink at 5 °C

Page 23: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

23

Examples

The ultimate temperature will be no different:– A uniform change from 80 °C down to 5 °C

Page 24: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

24

Example

A plot of the temperature over time across the bar is given by this solution:– Both ends drop from 100 °C however, the centre cools slower than does

the bar at either end point

Page 25: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

25

Example

A plot of the temperature over time across the bar is given by this solution:– Both ends drop from 100 °C however, the centre cools slower than does

the bar at either end point

Page 26: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

26

Rate of Change Proportional to Concavity

Ultimately, the concavity tends to disappear—that is, as time goes to infinity, u(x, t) becomes a straight line…

Important note:– This statement is only true in one spacial dimension

– In higher spacial dimensions, u(x, t) will approach a solution to Laplace’s equation in the given region

Page 27: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

27

Approximating the Derivative

Questions:– How do we approximate partial derivatives?

– What are the required conditions?

Page 28: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

28

Approximating Partial Derivatives

Recall our approximations of the derivatives:

In this case, however, we are dealing with partial derivatives

(1)

(2)2

22

u x h u x hu x

hu x h u x u x h

u xh

Page 29: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

29

Approximating Partial Derivatives

If you recall the definition of the partial derivative, we focus on one variable and leave the other variables constant

To ensure clarity:– h is used to indicate small steps in space

– t is used to indicate a small step in time

0

0

, ,, lim

, ,, lim

h

t

u x h t u x tu x t

x hu x t t u x t

u x tt t

Page 30: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

30

Approximating Partial Derivatives

The obvious extension is to define

, ,,

, ,,

u x h t u x tu x t

x hu x t t u x t

u x tt t

Page 31: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

31

Approximating Partial Derivatives

If we substitute the second into the heat-conduction/diffusion equation, we

or

2

2

, ,1 u x t t u x t u

t x

2

2, ,

uu x t t u x t t

x

Page 32: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

32

Similarity with IVPs

If this looks familiar to you, it should:

Recall Euler’s method: if y(1)(t) = f (t, y(t)) and y(t1) = y1, it follows that

2

2, ,

uu x t t u x t t

x

1 1 1 1,y t t y t t f t y The slope at (t1, y1)

Page 33: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

33

Approximating the Second Partial Derivative

The next step is to approximate the concavity

We will use our 2nd-order formula:

to get

2

2, ,

uu x t t u x t t

x

2

2 2

, 2 , ,,

u x h t u x t u x h tu x t

x h

2

2

, 2 , ,, ,

, , 2 , ,

u x h t u x t u x h tu x t t u x t t

h

tu x t u x h t u x t u x h t

h

Page 34: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

34

Approximating the Second Partial Derivative

There is only one small interesting issue:– What happens if the coefficient

is very large?

– It should be obvious; however, we will be approximating these at discrete points:

2, , , 2 , ,

tu x t t u x t u x h t u x t u x h t

h

, 1 , 1, , 1,22k k k k k

tu u u u u

h

Page 35: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

35

Approximating the Second Partial Derivative

There is only one small interesting issue:– Consider the total sum:

– If , the contribution from the boundary conditions will always

increase; thus, for stability, we require

20.5

t

h

1 1

, 1 , 1, , 1,22 2

1

, 1, 2, , 1,2 22

2x x

x

x x

n n

k k k k kk k

n

k n nk

tu u u u u

h

t tu u u u u

h h

20.5

t

h

Page 36: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

36

Initial and Boundary Conditions

For a 1st-order ODE, we require a single initial condition

(1)

1 1

,y t f t y t

y t y

Page 37: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

37

Initial and Boundary Conditions

For a 2nd-order ODE, we require either two initial conditions

or a boundary condition:

(2) (2) (2)1 2 3

a

b

c y x c y x c y x g x

y a y

y b y

(2) (1)

1 1

(1) (1)1 1

, ,y t f t y t y t

y t y

y t y

Time variables are usuallyassociated with initial conditions

Space variables are usuallyassociated with boundary conditions

Page 38: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

38

Initial and Boundary Conditions

For the heat-conduction/diffusion equation,

we have:– A first-order partial derivative with respect to time

– A second-order partial derivative with respect to space

21u u

t

Page 39: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

39

Initial and Boundary Conditions

For the heat-conduction/diffusion equation,

we require:– For each space coordinate, we require an initial condition for the time

– For each time coordinate, we require a boundary conditions for the space

21u u

t

Page 40: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

40

Initial and Boundary Conditions

In this case, we will require an initial value for each space coordinate– Keeping in the spirit of one dimension:

• If we were monitoring the progress of the temperature of a bar, we would know the initial temperature at each point of the bar at time t = t1

• If we were monitoring the diffusion of a gas , we would have to know the initial concentration of the gas

Page 41: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

41

Initial and Boundary Conditions

Is a one-dimensional system restricted to bars and thin tubes?– Insulated wires, impermeable tubes, etc.

No, many systems where two spacial dimensions are significant larger than one may be approximated by a one-dimensional system away the boundaries– Consider the effect of a thin insulator between two

materials that have approximately uniformtemperature

– Consider the diffusion of particles across amembrane separating liquids with differentconcentrations

In all such cases, it may be able toapproximate the system as a one-dimensionalsystem

Page 42: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

42

Approximating the Solution

Just as we did with BVPs, we will divide the spacial interval [a, b] into nx points or nx – 1 sub-intervals

Page 43: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

43

Approximating the Solution

Just as we did with BVPs, we will divide the spacial interval [a, b] into nx points or nx – 1 sub-intervals

– The initial state at time t = t1 would be defined by a function uinit(x) where

uinit:[a, b] → R

Page 44: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

44

Approximating the Solution

As with Euler’s method, we will attempt to approximate the solution u(x, t) at discrete times in the future– Given the state at time t = t1, approximate the solution at time t = t2

Page 45: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

45

Approximating the Solution

Never-the-less, we must still determine the 2nd-order component

– This requires two boundary values at a and b

21u u

t

Page 46: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

46

Approximating the Solution

Indeed, at each subsequent point, we will require the boundary values– Over time, the boundary values could change

Page 47: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

47

Approximating the Solution

Therefore, we will need two functions abndry(t) and bbndry (t)

– At each step, we would evaluate and determine two the boundary conditions

Page 48: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

48

Approximating the Solution

Therefore, we will need two functions abndry(t) and bbndry (t)

– We would continue from our initial time tinitial and continue to a final time tfinal

Page 49: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

49

Approximating the Solution

Like the spatial dimension, we would break the time interval into nt

discrete points where final initial

1t

t tt

n

initial 1kt t k t

Page 50: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

50

Approximating the Solution

Therefore, we will need two functions abndry(t) and bbndry (t)

– We would continue from our initial time tinitial and continue to a final time tfinal

Page 51: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

51

Approximating the Solution

Now that we have functions that define both the initial conditions and the boundary conditions, we must solve how the heat conducts or the material diffuses

Page 52: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

52

Approximating the Solution

Given the state at time t = t1, we approximate the state at time t = t2

Page 53: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

53

Approximating the Solution

Given the state at time t = t2, we approximate the state at time t = t3

Page 54: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

54

Approximating the Solution

Given the state at time t = t3, we approximate the state at time t = t4

Page 55: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

55

Approximating the Solution

We continue this process until we have approximated the solution at each of the desired times

Page 56: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

56

Approximating the Solution

This last image suggests strongly that our solution will be a matrix

Given the heat-conduction/diffusion equation

we will approximate the solution u(x, t) where

a < x < b and tinitial < t < tfinal

by calculating an nx × nt matrix U where U(k, )ℓ approximate the solution at time (xk, tℓ) with

xk = a + (k – 1)h

tℓ = tinitial + (ℓ – 1)t

21u u

t

final initial

1t

t tt

n

1x

b ah

n

Page 57: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

57

Step 1: Error Checking

Once the parameters are validated, the next step is to ensure

If this condition is not met, we should exit and provide a useful error message to the user:– For example,

The ratio kappa*dt/h^2 = ??? > 0.5, consider using nt = ???

where• The first ??? is replaced by the calculated ratio, and

• The second ??? is found by calculating the smallest integer for nt that would be acceptable to bring this ratio under 0.5

– You may wish to read up on the floor and ceil commands

20.5

t

h

Page 58: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

58

Step 2: Initialization

We will begin by creating the nx × nt matrix

– The zeros command is as good as any

Page 59: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

59

Step 2: Initialization

Next, we assign the initial values

init 1 bndry 2 bndry 3 bndry 4 bndry 5 bndry 6 bndry 7 bndry 8 bndry 9 bndry 10 bndry 11 bndry 12

init 2

init 3

init 4

init 5

init 6

init 7

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ?

u x a t a t a t a t a t a t a t a t a t a t a t

u x

u x

u x

u x

u x

u x

init 8

init 9 bndry 2 bndry 3 bndry 4 bndry 5 bndry 6 bndry 7 bndry 8 bndry 9 bndry 10 bndry 11 bndry 12

? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?u x

u x b t b t b t b t b t b t b t b t b t b t b t

nx

Page 60: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

60

init 1 bndry 2 bndry 3 bndry 4 bndry 5 bndry 6 bndry 7 bndry 8 bndry 9 bndry 10 bndry 11 bndry 12

init 2

init 3

init 4

init 5

init 6

init 7

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ?

u x a t a t a t a t a t a t a t a t a t a t a t

u x

u x

u x

u x

u x

u x

init 8

init 9 bndry 2 bndry 3 bndry 4 bndry 5 bndry 6 bndry 7 bndry 8 bndry 9 bndry 10 bndry 11 bndry 12

? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?u x

u x b t b t b t b t b t b t b t b t b t b t b t

Step 2: Initialization

Next, calculate the boundary values

nx

nt

Page 61: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

61

Step 3: Solving

In order to calculate both the initial and boundary values, we will require two vectors:– A column vector of x values, and

– A row vector of t values

Both of these can be constructed using linspace– Both of these will be returned to the user

Page 62: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

62

Step 3: Solving

We still have to calculate the interior values

init 1 bndry 2 bndry 3 bndry 4 bndry 5 bndry 6 bndry 7 bndry 8 bndry 9 bndry 10 bndry 11 bndry 12

init 2

init 3

init 4

init 5

init 6

init 7

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ?

u x a t a t a t a t a t a t a t a t a t a t a t

u x

u x

u x

u x

u x

u x

init 8

init 9 bndry 2 bndry 3 bndry 4 bndry 5 bndry 6 bndry 7 bndry 8 bndry 9 bndry 10 bndry 11 bndry 12

? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?u x

u x b t b t b t b t b t b t b t b t b t b t b t

nx

nt

Page 63: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

63

Step 3: Solving

For this, we go back to our finite difference equation:

init 1 bndry 2 bndry 3 bndry 4 bndry 5 bndry 6 bndry 7 bndry 8 bndry 9 bndry 10 bndry 11 bndry 12

init 2

init 3

init 4

init 5

init 6

init 7

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ?

u x a t a t a t a t a t a t a t a t a t a t a t

u x

u x

u x

u x

u x

u x

init 8

init 9 bndry 2 bndry 3 bndry 4 bndry 5 bndry 6 bndry 7 bndry 8 bndry 9 bndry 10 bndry 11 bndry 12

? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?u x

u x b t b t b t b t b t b t b t b t b t b t b t

2, , , 2 , ,

tu x t t u x t u x h t u x t u x h t

h

Page 64: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

64

Step 3: Solving

For this, we go back to our finite difference equation:

Replace the x with xk and t with tℓ:

2, , , 2 , ,

tu x t t u x t u x h t u x t u x h t

h

2, , , 2 , ,k k k k k

tu x t t u x t u x h t u x t u x h t

h

Page 65: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

65

Step 3: Solving

For this, we go back to our finite difference equation:

Replace the x with xk and t with tℓ:

Recall that xk + h = xk + 1 and tℓ + t = tℓ + 1

2, , , 2 , ,

tu x t t u x t u x h t u x t u x h t

h

2, , , 2 , ,k k k k k

tu x t t u x t u x h t u x t u x h t

h

1 1 12, , , 2 , ,k k k k k

tu x t u x t u x t u x t u x t

h

Page 66: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

66

Step 3: Solving

Finally, given the equation

recall that we are approximating the solution at u(xk, tℓ) by uk,ℓ:

1 1 12, , , 2 , ,k k k k k

tu x t u x t u x t u x t u x t

h

, 1 , 1, , 1,22k k k k k

tu u u u u

h

Page 67: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

67

init 1 bndry 2 bndry 3 bndry 4 bndry 5 bndry 6 bndry 7 bndry 8 bndry 9 bndry 10 bndry 11 bndry 12

init 2 2,2

init 3 3,2

init 4 4,2

init 5 5,2

init 6 6,2

? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ?

u x a t a t a t a t a t a t a t a t a t a t a t

u x u

u x u

u x u

u x u

u x u

init 7 7,2

init 8 8,2

init 9 bndry 2 bndry 3 bndry 4 bndry 5 bndry 6 bndry 7 bndry 8 bndry 9 bndry 10 bndry 11 bndry 12

? ? ?

? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ?

u x u

u x u

u x b t b t b t b t b t b t b t b t b t b t b t

Step 3: Solving

Using this formula

we can first calculate u2,2 through un – 1,2

, 1 , 1, , 1,22k k k k k

tu u u u u

h

x

Page 68: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

68

init 1 bndry 2 bndry 3 bndry 4 bndry 5 bndry 6 bndry 7 bndry 8 bndry 9 bndry 10 bndry 11 bndry 12

init 2 2,2 2,3

init 3 3,2 3,3

init 4 4,2 4,3

init 5 5,2 5,3

init

? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ?

u x a t a t a t a t a t a t a t a t a t a t a t

u x u u

u x u u

u x u u

u x u u

u x

6 6,2 6,3

init 7 7,2 7,3

init 8 8,2 8,3

init 9 bndry 2 bndry 3 bndry 4 bndry 5 bndry 6 bndry 7 bndry 8 bndry 9 bndry 10 bndry 11 bndry 12

? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ?

u u

u x u u

u x u u

u x b t b t b t b t b t b t b t b t b t b t b t

Step 3: Solving

Using this formula

and then u2,3 through un – 1,3 and so on

, 1 , 1, , 1,22k k k k k

tu u u u u

h

x

Page 69: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

69

Useful Matlab Commands

Recall that you can both access and assign to sub-matrices of a given matrix:

>> U = [1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15; 16 17 18 19 20]

U =

1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

16 17 18 19 20

>> U(:,1) = [0 0 0 0]';

>> U(1,2:end) = [42 42 42 42];

>> U(2:end - 1,3) = [91 91]'

U =

0 42 42 42 42

0 7 91 9 10

0 12 91 14 15

0 17 18 19 20

Page 70: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

70

Useful Matlab Commands

Matlab actually gives you a lot of power when it comes to assigning entries by row or by column:

>> U = [1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15; 16 17 18 19 20]U = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

>> U([1, 3],2:end) = [42 42 42 42; 91 91 91 91]U = 1 42 42 42 42 6 7 8 9 10 11 91 91 91 91 16 17 18 19 20

Page 71: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

71

Useful Matlab Commands

The throw command in Matlab allows the user to format its message in a manner similar to the sprintf command in Matlab (and C)

function [] = error_example( a, b ) throw( MException( 'MATLAB:invalid_argument', ...

'the argument %f (%d) and %f (%d) were passed', a, a, b, b ) );end

When run, the output is:>> error_example( 3, pi )??? Error using ==> ff at 2the argument 3.000000 (3 ) and 3.141593 (3.141593e+000) were

passed

Page 72: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

72

Useful Matlab Commands

You can, of course, modify this:function [] = error_example( a, b ) throw( MException( 'MATLAB:invalid_argument', ...

'the ratio of %d and %d is %f', a, b, a/b ) );end

When run, the output is:>> error_example( 3, 5 )??? Error using ==> error_example at 2the ratio of 3 and 5 is 0.600000

See>> help sprintf

Page 73: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

73

Useful Matlab Commands

A very useful command is the diff command:>> v = [1 3 6 7 5 4 2 0]v = 1 3 6 7 5 4 2 0

>> diff( v )ans = 2 3 1 -2 -1 -2 -2>> v(2:end) - v(1:end - 1)ans = 2 3 1 -2 -1 -2 -2

>> diff( v, 2 )ans = 1 -2 -3 1 -1 0>> v(3:end) - 2*v(2:end - 1) + v(1:end - 2)ans =

1 -2 -3 1 -1 0

Page 74: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

74

Useful Matlab Commands

Why use diff instead of a for loop?– We could simply use two nested for loops:

>> for it = 2:nt for ix = 2:(nx - 1) % calculate U(ix, it) end end

however, it would be better to use:>> for it = 2:nt % calculate U(2:nx - 1, it) using diff end

In Matlab– Most commands call compiled C code

– For loops and while loops are interpreted

Page 75: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

75

Useful Matlab Commands

How slow is interpreted code?– This function implements matrix-matrix-multiplication

function [ M12 ] = multiplication( M1, M2 ) [m, n1] = size( M1 ); [n2, p] = size( M2 ); if n1 ~= n; error( 'dimensions must agree' ); end; M12 = zeros( m, p );

for i=1:m for j=1:n1 for k = 1:p M12( i, k ) = M12( i, k ) + M1(i,j)*M2(j,k); end end endend

Page 76: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

76

Useful Matlab Commands

Now, consider:>> M = rand( 1300, 1000 );>> N = rand( 1000, 1007 ); >> tic; M*N; tocElapsed time is 0.196247 seconds.>> tic; multiplication( M, N ); tocElapsed time is 35.530549 seconds.

>> 35.530549/0.196247ans = 181.0502

What’s two orders of magnitude between you and your employer?

Page 77: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

77

Useful Matlab Commands

Suppose we want to implement a function which is only turned on for, say, one period

In Matlab, you could use the following:

function y = f( x ) y = -cos(x) .* ((x >= 0.5*pi) & (x <= 2.5*pi ));end

Page 78: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

78

Useful Matlab Commands

The output of>> x = linspace( 0, 10, 101 );>> plot( x, f( x ), 'ro' )

In order to understand the output, consider>> >> x = linspace( 0, 10, 11 );|>> x >= 0.5*pians = 0 0 1 1 1 1 1 1 1 1 1>> x <= 2.5*pians = 1 1 1 1 1 1 1 1 0 0 0>> (x >= 0.5*pi) & (x <= 2.5*pi)ans = 0 0 1 1 1 1 1 1 0 0 0

Page 79: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

79

Example 1

As a first example:>> [x1, t1, U1] = diffusion1d( 0.1, [0,1], 6, [1, 3], 12, @u_init1,

@u_bndry1 );>> size(x1)ans = 6 1

>> size(t1)ans = 1 12

>> size(U1)ans = 6 12

>> mesh( t1, x1, U1 ) xt

b = 1

a = 0tinitial = 1

tfinal = 3

uinit (x) = 0.9

bbndry (t) = 4.1

abndry (t) = 1.7

nt = 12nx = 6

= 0.1

Page 80: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

80

Example 1

As a first example:>> [x1, t1, U1] = diffusion1d( 0.1, [0,1], 6, [1, 3], 12, @u_init1,

@u_bndry1 );

The two functions are:function u = u_init1( x )

u = x*0 + 0.9;end

fucntion u_bndry1( t )u = [t*0 + 1.7; t*0 + 4.1];

end

xt

b = 1

a = 0tinitial = 1

tfinal = 3

uinit (x) = 0.9

bbndry (t) = 4.1

abndry (t) = 1.7

nt = 12nx = 6

= 0.1

Page 81: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

81

Example 1

Because the large size of both h and t, there is still some numeric fluctuations

20.4545

t

h

xt

Page 82: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

82

Example 2

Decreasing h and t produces a better result>> [x2, t2, U2] = diffusion1d( 0.1, [0,1], 100, [1, 3], 4000, @u_init1,

@u_bndry1 );>> size(x2)ans = 100 1

>> size(t2)ans = 1 4000

>> size(U2)ans = 100 4000

>> mesh( t2, x2, U2 ) xt

20.4902

t

h

h = 0.01t = 0.0005

Page 83: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

83

Example 3

If we exceed 0.5, the approximation begins to fail>> [x2, t2, U2] = diffusion1d( 0.1, [0,1], 100, [1, 3], 3916, @u_init1,

@u_bndry1 );>> mesh( t2, x2, U2 )

xt

20.5007

t

h

Stable when nt = 4000

Page 84: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

84

Example 4

As the ratio exceeds 0.5, it quickly breaks down:>> [x2, t2, U2] = diffusion1d( 0.1, [0,1], 100, [1, 3], 3900, @u_init1,

@u_bndry1 );>> mesh( t2, x2, U2 )

xt

20.5027

t

h

Stable when nt = 4000

2 × 1014

Page 85: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

85

Summary

We have looked at the heat-conduction/diffusion equation– Considered the physical problem

– Considered the effects in one dimension

– Found a divided-difference equation approximating the PDE

• Saw that

• Examples

– In the laboratory, you will consider insulated boundaries

20.5

t

h

Page 86: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

86

References

[1] Glyn James, Modern Engineering Mathematics, 4th Ed., Prentice Hall, 2007, p.778.

[2] Glyn James, Advanced Modern Engineering Mathematics, 4th Ed., Prentice Hall, 2011, p.164.

Page 87: Heat-conduction/Diffusion Equation

Heat-conduction/Diffusion Equation

87

Usage Notes

• These slides are made publicly available on the web for anyone to use

• If you choose to use them, or a part thereof, for a course at another institution, I ask only three things:

– that you inform me that you are using the slides,– that you acknowledge my work, and– that you alert me of any mistakes which I made or changes which you make, and

allow me the option of incorporating such changes (with an acknowledgment) in my set of slides

Sincerely,

Douglas Wilhelm Harder, MMath

[email protected]