the crank-nicolson method and insulated boundaries douglas wilhelm harder, m.math. lel department of...
TRANSCRIPT
The Crank-Nicolson Methodand
Insulated Boundaries
Douglas Wilhelm Harder, M.Math. LELDepartment of Electrical and Computer Engineering
University of Waterloo
Waterloo, Ontario, Canada
ece.uwaterloo.ca
© 2012 by Douglas Wilhelm Harder. Some rights reserved.
2
Outline
This topic discusses numerical approximations to solutions to the heat-conduction/diffusion equation:– Consider the Crank-Nicolson method for approximating the heat-
conduction/diffusion equation– This is an implicit method
• Uses Matlab from Laboratories 1 and 2• Unconditionally stable
– Defines the characteristics of insulated boundaries– Implement insulated boundaries into the Crank-Nicolson method
The Crank-Nicolson Method
3
Outcomes Based Learning Objectives
By the end of this laboratory, you will:– Understand the Crank-Nicolson method– Understand the definition and approximations of insulated
boundaries
The Crank-Nicolson Method
4
Review
In Laboratory 1, you solved a boundary-value problem:– Given the finite-difference equation
d+uk − 1 + duk + d−uk + 1 = g(xk),
there are three unknowns:
uk – 1 uk uk + 1
– This requires us to set up a systemof n – 2 linear equations which must besolved
– The boundary values give us u1 and un
The Crank-Nicolson Method
5
Review
In Laboratory 2, we used an explicit method:– Given the finite-difference equation
– All the values on the right-hand sideare known, we need only evaluate thisfor u2, k + 1 through un − 1 , k + 1
, 1 , 1, , 1,22i k i k i k i k i k
tu u u u u
h
x
The Crank-Nicolson Method
6
Review
We found the finite-difference equation
by substituting
into 2
2
u u
t x
, 1 , 1, , 1,22i k i k i k i k i k
tu u u u u
h
, 1 ,
21, , 1,
2 2
,
2,
i k i ki k
i k i k i ki k
u uu x t
t tu u u
u x tx h
Both focus on (xi, tk)
The Crank-Nicolson Method
7
The Crank-Nicolson Method
What happens if we focus on the point (xi, tk + 1)?
, 1 ,1
21, 1 , 1 1, 1
12 2
,
2,
i k i ki k
i k i k i ki k
u uu x t
t hu u u
u x tx h
These focus on (xi, tk + 1)
The Crank-Nicolson Method
8
The Crank-Nicolson Method
This gives us the finite-difference equation
The linear equation now has:– One known ui, k
– Three unknowns ui – 1,k + 1, ui,k + 1, ui + 1, k + 1
The Crank-Nicolson Method
, 1 , 1, 1 , 1 1, 122i k i k i k i k i k
tu u u u u
h
9
The Crank-Nicolson Method
Compare the two:
, 1 , 1, , 1,22i k i k i k i k i k
tu u u u u
h
, 1 , 1, 1 , 1 1, 122i k i k i k i k i k
tu u u u u
h
The Crank-Nicolson Method
10
The Crank-Nicolson Method
Given two equations, we can add them:
, 1 , 1, , 1,2
, 1 , 1, 1 , 1 1, 12
2
2
i k i k i k i k i k
i k i k i k i k i k
tu u u u u
ht
u u u u uh
, 1 , 1, , 1,2
1, 1 , 1 1, 12
2 2 2
2
i k i k i k i k i k
i k i k i k
tu u u u u
ht
u u uh
+
The Crank-Nicolson Method
11
The Crank-Nicolson Method
First, substitute
, 1 , 1, , 1,
1, 1 , 1 1, 1
2 2 2
2
i k i k i k i k i k
i k i k i k
u u r u u u
r u u u
2
tr
h
The Crank-Nicolson Method
12
The Crank-Nicolson Method
Next, collect similar terms and bring– All unknowns to the left, and– All knowns to the right
1, 1 , 1 1, 1
, 1, , 1,
2 1
2 2
i k i k i k
i k i k i k i k
ru r u ru
u r u u u
The Crank-Nicolson Method
13
The Crank-Nicolson Method
We now have a new finite-difference equation:
1, 1 , 1 1, 1 , 1, , 1,2 1 2 2i k i k i k i k i k i k i kru r u ru u r u u u
Unknowns Knowns
The Crank-Nicolson Method
14
The Crank-Nicolson Method
As we did in Laboratory 1, we could then write nx – 2 equations
1, 1 2, 1 3, 1 2, 1, 2, 3,2 1 2 2k k k k k k kru r u ru u r u u u
2, 1 3, 1 4, 1 3, 2, 3, 4,2 1 2 2k k k k k k kru r u ru u r u u u
3, 1 4, 1 5, 1 4, 3, 4, 5,2 1 2 2k k k k k k kru r u ru u r u u u
2, 1 1, 1 , 1 1, 2, 1, ,2 1 2 2x x x x x x xn k n k n k n k n k n k n kru r u ru u r u u u
3, 1 2, 1 1, 1 2, 3, 2, 1,2 1 2 2x x x x x x xn k n k n k n k n k n k n kru r u ru u r u u u
....nx – 2
Unknowns Knowns
The Crank-Nicolson Method
15
The Crank-Nicolson Method
Again, there appear to be nx unknowns; however, the boundary conditions provide two of those values:
1, 1 2, 1 3, 1 2, 1, 2, 3,2 1 2 2k k k k k k kru r u ru u r u u u
2, 1 3, 1 4, 1 3, 2, 3, 4,2 1 2 2k k k k k k kru r u ru u r u u u
3, 1 4, 1 5, 1 4, 3, 4, 5,2 1 2 2k k k k k k kru r u ru u r u u u
2, 1 1, 1 , 1 1, 2, 1, ,2 1 2 2x x x x x x xn k n k n k n k n k n k n kru r u ru u r u u u
3, 1 2, 1 1, 1 2, 3, 2, 1,2 1 2 2x x x x x x xn k n k n k n k n k n k n kru r u ru u r u u u
....
Unknowns Knowns
The Crank-Nicolson Method
16
The Crank-Nicolson Method
That is: u1, k + 1 = abndry(tk + 1)
un , k + 1 = bbndry(tk + 1)x
1, 1 2, 1 3, 1 2, 1, 2, 3,2 1 2 2k k k k k k kru r u ru u r u u u
2, 1 3, 1 4, 1 3, 2, 3, 4,2 1 2 2k k k k k k kru r u ru u r u u u
3, 1 4, 1 5, 1 4, 3, 4, 5,2 1 2 2k k k k k k kru r u ru u r u u u
2, 1 1, 1 , 1 1, 2, 1, ,2 1 2 2x x x x x x xn k n k n k n k n k n k n kru r u ru u r u u u
3, 1 2, 1 1, 1 2, 3, 2, 1,2 1 2 2x x x x x x xn k n k n k n k n k n k n kru r u ru u r u u u
....
Unknowns Knowns
Bring them to the right-hand side....
The Crank-Nicolson Method
17
The Crank-Nicolson Method
We now have nx – 2 linear equations and nx – 2 unknowns
2, 1 3, 1 2, 1, 2, 3, bndry 12 1 2 2k k k k k k kr u ru u r u u u ra t
2, 1 1, 1 1, 2, 1, , bndry 12 1 2 2x x x x x xn k n k n k n k n k n k kru r u u r u u u rb t
2, 1 3, 1 4, 1 3, 2, 3, 4,2 1 2 2k k k k k k kru r u ru u r u u u
3, 1 4, 1 5, 1 4, 3, 4, 5,2 1 2 2k k k k k k kru r u ru u r u u u
3, 1 2, 1 1, 1 2, 3, 2, 1,2 1 2 2x x x x x x xn k n k n k n k n k n k n kru r u ru u r u u u
....
Unknowns Knowns
The Crank-Nicolson Method
18
The Crank-Nicolson Method
This can be written in the form Mx = b:
2, 1
3, 1
4, 1
2, 1
1, 1
2 1
2 1
2 1
2 1
2 1
x
x
k
k
k
n k
n k
r r ur r r u
r r u
ru
r r ru
r r
2, 1, 2, 3, bndry 1
3, 2, 3, 4,
4, 3, 4, 5,
2, 3, 2, 1,
1, 2, 1, , bndry 1
2 2
2 2
2 2
2 2
2 2
x x x x
x x x x
k k k k k
k k k k
k k k k
n k n k n k n k
n k n k n k n k k
u r u u u ra t
u r u u u
u r u u u
u r u u u
u r u u u rb t
Unknow
ns
Knowns
The Crank-Nicolson Method
The Crank-Nicolson Method
Note the structure of b:
2, 1, 2, 3, bndry 1
3, 2, 3, 4,
4, 3, 4, 5,
2, 3, 2, 1,
1, 2, 1, , bndry 1
2 2
2 2
2 2
2 2
2 2
x x x x
x x x x
k k k k
k k k k
k k k k
n k n k n k n k
n k n k n k n k
u r u u u ra t
u r u u u
u r u u u
u r u u u
u r u u u rb t
2, 1, 2, 3, bndry 1
3, 2, 3, 4,
4, 3, 4, 5,
2, 3, 2, 1,
bndry 11, 2, 1, ,
2
2
22
2
2x x x x
x x x x
k k k k k
k k k k
k k k k
n k n k n k n k
kn k n k n k n k
u u u u ra tu u u u
u u u ur
u u u u
rb tu u u u
diff
19
The Crank-Nicolson Method
20
Approximating the Solution
Thus, given the initial state at time t1, we create a system of equations with k = 1 to solve for u2, 2 through un – 1, 2x
The Crank-Nicolson Method
21
Approximating the Solution
We will simultaneously solve for these values and assign them to our solution matrix U
The Crank-Nicolson Method
22
Approximating the Solution
Given the initial state at time t2, we will create the system of equations with k = 2 to solve for u2, 3 through un – 1, 3x
The Crank-Nicolson Method
23
Approximating the Solution
Again, having solved for the values u2, 3 through un – 1, 3, we assign those entries to our matrix U
x
The Crank-Nicolson Method
24
Approximating the Solution
In general, at time tk, we will create the system of equations with k and then solve for u2, k + 1 through un – 1, k + 1x
The Crank-Nicolson Method
25
Approximating the Solution
Doing this, we will fill in the balance of the matrix
The Crank-Nicolson Method
26
The diffusion1d Function
The signature will be function [x_out, t_out, U_out] = ... crank_nicolson1d( kappa, x_rng, nx, t_rng, nt, u_init,
u_bndry )
wherekappa the diffusivity coefficient
x_rng the space range [a, b]
nx the number of points into which we will divide [a, b]
t_rng the time interval [t0, tfinal]
nt the number of points into which we will divide [t0, tfinal]
u_init a function handle giving the initial state uinit:[a, b] → R
u_bndry a function handle giving the two boundary conditions
ubndry:[t0, tfinal] → R2 where
bndrybndry
bndry
a tu t
b t
The Crank-Nicolson Method
27
Step 1: Error Checking
Unlike the previous method we used which was subject to a catastrophic error if
the Crank-Nicolson method is unconditionally stable– There are no values which will cause the divergence we saw
using the previous technique
Never-the-less, if , there may be decaying osillations
20.5
t
h
20.5
t
h
The Crank-Nicolson Method
28
Step 1: Error Checking
Rather than throwing an exception, issue a warning:
warning( 'MATLAB:questionable_argument', ... 'the arguments of %d and %d are sub-
optimal', ... a, b )
This warning will be seen by the user; however,it will not terminate the execution of the function
The Crank-Nicolson Method
29
Step 2: Initialization
It would still be useful to initialize the matrix U and then use the values as appropriate
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
The Crank-Nicolson Method
30
Step 3: Solving
As with the previous case, we will find solutions for the interior points for t2 through t
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
t
The Crank-Nicolson Method
nt
31
Step 3: Solving
For each time step from 1 to nt – 1, we will, however, have to perform the following:
3a. Set up the system of linear equations
3b. Solve the system of linear equations, and
3c. Assign the values to the next column of the solution matrix
The Crank-Nicolson Method
32
Useful Matlab Commands
There are no new additional Matlab commands for the Crank-Nicolson method that you have not already been introduced to in Laboratories 1 and 2
The Crank-Nicolson Method
33
Examples
Consider the following example:– The initial temperature of the bar is 1 oC– The bar is placed in contact with two barriers at –1 oC and 2 oC
function [u] = u3a_init( x ) u = x*0 + 1;end
function [u] = u3a_bndry( t ) u = [t*0 - 1; t*0 + 2];end
The Crank-Nicolson Method
34
Examples
[xs, ts, Us] = crank_nicolson1d( 1.5, [0 1], 6, [0 1], 21, @u3a_init, @u3a_bndry );
mesh( ts, xs, Us )
21.875
t
h
The Crank-Nicolson Method
35
Examples
[xs, ts, Us] = crank_nicolson1d( 1.5, [0 1], 41, [0 1], 11, @u3a_init, @u3a_bndry );
mesh( ts, xs, Us )
2240
t
h
Note the transient oscillations
The Crank-Nicolson Method
36
Examples
[xs, ts, Us] = crank_nicolson1d( 1.5, [0 1], 41, [0 1], 41, @u3a_init, @u3a_bndry );
mesh( ts, xs, Us )
Note the transient oscillations
260
t
h
The Crank-Nicolson Method
37
Examples
[xs, ts, Us] = crank_nicolson1d( 1.5, [0 1], 21, [0 1], 301, @u3a_init, @u3a_bndry );
mesh( ts, xs, Us )
22
t
h
The Crank-Nicolson Method
38
Examples
Consider an alternate example:– The initial temperature of the bar is 0 oC– The bar is placed in contact with two barriers at 1 oC and 4 oC
function [u] = u3b_init( x ) u = x*0;end
function [u] = u3b_bndry( t ) u = [t*0 + 1; t*0 + 4];end
The Crank-Nicolson Method
39
Examples
[x3b, t3b, U3b] = crank_nicolson1d( 0.25, [0 1], 11, [0 1], 11, @u3b_init, @u3b_bndry );
mesh( t3b, x3b, U3b );frames3b = animate( U3b );frames2gif( frames3b, 'plot3b.i.gif' ); 2
2.5t
h
The Crank-Nicolson Method
40
Examples
[x3b, t3b, U3b] = crank_nicolson1d( 0.25, [0 1], 41, [0 1], 161, @u3b_init, @u3b_bndry );
mesh( t3b, x3b, U3b );frames3b = animate( U3b );frames2gif( frames3b, 'plot3b.ii.gif' ); 2
2.5t
h
The Crank-Nicolson Method
41
Insulated Boundaries
We have looked at situations where we have known temperatures or concentrations at each end of the bar; however, what happens if one end of the bar is insulated?
The Crank-Nicolson Method
42
Terminology
Up to this point, we have discussed boundary values where the value of the function is specified– These are termed Dirichlet boundary condition
Alternatively, one can specify the value of the derivative at the boundary points– These are termed Neumann boundary conditions
Specifically, we will focus on insulated boundary conditions where the derivative at the boundaries are zero
The Crank-Nicolson Method
43
Insulated Boundaries
Suppose you have a metal bar in contact with a body at 100 oC– If the bar is insulated, over time, the entire length of the bar will
be at 100 oC
100 oC0 oC
The Crank-Nicolson Method
44
Insulated Boundaries
At time t = 0, one end of the bar is brought in contact with a heat sink at 0 oC; the other end is insulated
100 oC0 oC
The Crank-Nicolson Method
45
Insulated Boundaries
Over time, the bar continues to cool
100 oC0 oC
The Crank-Nicolson Method
46
Insulated Boundaries
The cooling process will be much slower
100 oC0 oC
The Crank-Nicolson Method
47
Insulated Boundaries
Even the furthest end, however, will begin to cool after some time
42 oC0 oC
The Crank-Nicolson Method
48
Insulated Boundaries
Until, ultimately, the entire bar is at 0 oC
0 oC0 oC
The Crank-Nicolson Method
49
Insulated Boundaries
The mathematical property of an insulated boundary is that there is no transfer w.r.t. space of the property (temperature or concentration) across that boundary:
or 1, 0ku a t
x
1, 0ku b tx
The Crank-Nicolson Method
50
Insulated Boundaries
We could use the O(h) approximations:
However, as other approximations of the 2nd derivative are O(h2), we will use:
, ,,
u a h t u a tu a t
x h
The Crank-Nicolson Method
, ,,
u b t u b h tu b t
x h
3 , 4 , 2 ,,
22 , 4 , 3 ,
,2
u a t u a h t u a h tu a t
x hu b h t u b h t u b t
u b tx h
51
Insulated Boundaries
Thus, our approximations of the insulated boundary conditions
are
1 1 12 , 4 , 3 ,0
2k k ku a h t u a h t u a t
h
1 1 13 , 4 , 2 ,0
2k k ku b t u b h t u b h t
h
The Crank-Nicolson Method
1, 0ku a tx
1, 0ku b tx
52
Insulated Boundaries
First multiply by 2h:
Next, substitute
Finally, solve for u1,k + 1 and un ,k + 1 :
1 1 12 , 4 , 3 , 0k k ku a h t u a h t u a t
1 1 13 , 4 , 2 , 0k k ku b t u b h t u b h t
3, 1 2, 1 1, 14 3 0k k ku u u
, 1 1, 1 2, 13 4 0x x xn k n k n ku u u
1, 1 2, 1 3, 1
4 1
3 3k k ku u u , 1 1, 1 2, 1
4 1
3 3x x xn k n k n ku u u
x
The Crank-Nicolson Method
53
Insulated Boundaries
The first and last linear equations we prepared use both u1, k + 1 and un , k + 1, respectively
1, 1 2, 1 3, 1 2, 1, 2, 3,2 1 2 2k k k k k k kru r u ru u r u u u
2, 1 1, 1 , 1 1, 2, 1, ,2 1 2 2x x x x x x xn n k n k n k n k n k n kru r u ru u r u u u
x
The Crank-Nicolson Method
54
Insulated Boundaries
Applying our substitutions:
2, 1 3, 1 2, 1 3, 1 2, 1, 2, 3,
4 12 1 2 2
3 3k k k k k k k kr u u r u ru u r u u u
2, 1 1, 1 1, 1 2, 1 1, 2, 1, ,
4 12 1 2 2
3 3x x x x x x x xn n k n k n k n k n k n k n kru r u r u u u r u u u
1, 1 2, 1 3, 1
4 1
3 3k k ku u u
, 1 1, 1 2, 1
4 1
3 3x x xn k n k n ku u u
The Crank-Nicolson Method
55
Insulated Boundaries
Thus, the first and last equations simplify to:
2, 1 3, 1 2, 1, 2, 3,
2 22 2 2
3 3k k k k k kr u ru u r u u u
2, 1 1, 1 1, 2, 1, ,
2 22 2 2
3 3x x x x x xn n k n k n k n k n kru r u u r u u u
The Crank-Nicolson Method
56
Insulated Boundaries
Recall our original system Mx = b
2, 1, 2, 3, bndry 1
3, 2, 3, 4,
4, 3, 4, 5,
intr
2, 3, 2, 1,
1,
2 22 1
2 1 2 2
2 22 1
2 22 1
2 1 2
x x x x
x x
k k k k k
k k k k
k k k k
n k n k n k n k
n k n
u r u u u ra tr r
r r r u r u u u
u r u u ur r
ru r u u ur r r
r r u r u
u
2, 1, , bndry 12x xk n k n k ku u rb t
The Crank-Nicolson Method
57
2, 1, 2, 3, bndry 1
3, 2, 3, 4,
4, 3, 4, 5,
intr
2, 3, 2, 1,
1,
2 22 1
2 1 2 2
2 22 1
2 22 1
2 1 2
x x x x
x x
k k k k k
k k k k
k k k k
n k n k n k n k
n k n
u r u u u ra tr r
r r r u r u u u
u r u u ur r
ru r u u ur r r
r r u r u
u
2, 1, , bndry 12x xk n k n k ku u rb t
Insulated Boundaries
Two changes are required if the left boundary is insulated:– m1,1 and m1,2 are modified
– The original change to b1 is no longer necessary2
23
r2
3r
The Crank-Nicolson Method
58
2, 1, 2, 3, bndry 1
3, 2, 3, 4,
4, 3, 4, 5,
intr
2, 3, 2, 1,
1,
2 22 1
2 1 2 2
2 22 1
2 22 1
2 1 2
x x x x
x x
k k k k k
k k k k
k k k k
n k n k n k n k
n k n
u r u u u ra tr r
r r r u r u u u
u r u u ur r
ru r u u ur r r
r r u r u
u
2, 1, , bndry 12x xk n k n k ku u rb t
Insulated Boundaries
Two changes are required if the right boundary is insulated:– mn – 2, n – 2 and mn – 2, n – 3 are modified
– The original change to bn – 2 is no longer necessaryx
x x
22
3r
2
3r
x x
The Crank-Nicolson Method
59
Insulated Boundaries
How do we indicate an insulated boundary?– One of the best ideas I have found is to have the boundary
function return NaN– Recall that NaN is the result of floating-point operations such as
0/0: >> 0/0ans = NaN
– It is also appropriate: an insulated boundary has an undefined temperature
The Crank-Nicolson Method
60
Insulated Boundaries
For example, the following would implement an insulated boundary at the left-hand end point:
function u = u3c_bndry(t) u = [0*t + NaN; 0*t + 2];end
The Crank-Nicolson Method
61
Insulated Boundaries
Problem: IEEE 754 standard requires that NaN ≠ NaN:>> NaN == NaNans = 0
>> NaN ~= NaNans = 1
Solution: use the isnan command:>> isnan( NaN )ans = 1
The Crank-Nicolson Method
62
Insulated Boundaries
Suppose that our system begins as follows:>> [x3c, t3c, U3c] = crank_nicolson1d( 1.5, [0 2], 6, [0 1], 9, @u3c_init,
@u3c_bndry );
1 NaN NaN NaN NaN NaN NaN NaN NaN 1 0 0 0 0 0 0 0 01 0 0 0 0 0 0 0 01 0 0 0 0 0 0 0 01 0 0 0 0 0 0 0 01 2 2 2 2 2 2 2 2
21.1719
t
h
The Crank-Nicolson Method
63
Insulated Boundaries
Suppose that our system begins as follows:>> [x3c, t3c, U3c] = crank_nicolson1d( 1.5, [0 2], 6, [0 1], 9, @u3c_init,
@u3c_bndry );
The first system of equations yields the solution1.00701.02501.08581.2929
1 NaN NaN NaN NaN NaN NaN NaN NaN 1 0 0 0 0 0 0 0 01 0 0 0 0 0 0 0 01 0 0 0 0 0 0 0 01 0 0 0 0 0 0 0 01 2 2 2 2 2 2 2 2
The Crank-Nicolson Method
64
Insulated Boundaries
These values are assigned to the 2nd column:
The first system of equations yields the solution1.00701.02501.08581.2929
1 NaN NaN NaN NaN NaN NaN NaN NaN 1 1.0070 0 0 0 0 0 0 01 1.0250 0 0 0 0 0 0 01 1.0858 0 0 0 0 0 0 01 1.2929 0 0 0 0 0 0 01 2 2 2 2 2 2 2 2
The Crank-Nicolson Method
65
Insulated Boundaries
Now we reuse the formula:
The first system of equations yields the solution1.00701.02501.08581.2929
1 1.0010 NaN NaN NaN NaN NaN NaN NaN 1 1.0070 0 0 0 0 0 0 01 1.0250 0 0 0 0 0 0 01 1.0858 0 0 0 0 0 0 01 1.2929 0 0 0 0 0 0 01 2 2 2 2 2 2 2 2
1,2 2,2 3,2
4 1
3 3u u u
The Crank-Nicolson Method
66
Insulated Boundaries
Repeating the process...
The second system of equations yields the solution1.04041.10771.27351.6133
1 1.0010 NaN NaN NaN NaN NaN NaN NaN 1 1.0070 0 0 0 0 0 0 01 1.0250 0 0 0 0 0 0 01 1.0858 0 0 0 0 0 0 01 1.2929 0 0 0 0 0 0 01 2 2 2 2 2 2 2 2
The Crank-Nicolson Method
67
Insulated Boundaries
Copy the values:
The second system of equations yields the solution1.04041.10771.27351.6133
1 1.0010 NaN NaN NaN NaN NaN NaN NaN 1 1.0070 1.0404 0 0 0 0 0 01 1.0250 1.1077 0 0 0 0 0 01 1.0858 1.2735 0 0 0 0 0 01 1.2929 1.6133 0 0 0 0 0 01 2 2 2 2 2 2 2 2
The Crank-Nicolson Method
68
Insulated Boundaries
And use the formula:
The second system of equations yields the solution1.04041.10771.27351.6133
1 1.0010 1.0179 NaN NaN NaN NaN NaN NaN 1 1.0070 1.0404 0 0 0 0 0 01 1.0250 1.1077 0 0 0 0 0 01 1.0858 1.2735 0 0 0 0 0 01 1.2929 1.6133 0 0 0 0 0 01 2 2 2 2 2 2 2 2
1,2 2,2 3,2
4 1
3 3u u u
The Crank-Nicolson Method
69
Insulated Boundaries
If the insulated boundary condition is at the other end,we would use the formula:
to calculate the missing entry
, 1 1, 1 2, 1
4 1
3 3x x xn k n k n ku u u
The Crank-Nicolson Method
70
Step 3: Solving
We only need two small changes:– First, in the argument checking, nx ≥ 4
– Second, for each time step from 1 to nt – 1, we will, however, have to perform the following:
3a. Set up the system of linear equations and modify:– The vector if it is a Dirichlet boundary condition– The matrix if it is an insulated boundaries
3b. Solve the system, and
3c. Copy the values back to U in the next column– If the boundaries are insulated in that column, use the
appropriate formula to find the end points
The Crank-Nicolson Method
71
Examples
Ultimately, we get the image: [x3c, t3c, U3c] = crank_nicolson1d( 1.5, [0 2], 6, [0 1], 9, @u3c_init,
@u3c_bndry ); mesh( t3c, x3c, U3c )
The Crank-Nicolson Method
72
Examples
If we increase the resolution: [x3d, t3d, U3d] = crank_nicolson1d( 1.5, [0 2], 20, [0 1], 100, @u3c_init,
@u3c_bndry ); mesh( t3d, x3d, U3d )
The Crank-Nicolson Method
73
Examples
After three seconds, the temperature near the insulated boundary has increased significantly
[x3e, t3e, U3e] = crank_nicolson1d( 1.5, [0 2], 20, [0 3], 600, @u3c_init, @u3c_bndry );
mesh( t3e, x3e, U3e )frames = animate( U3e );frames2gif( frames, 'U3e.gif' );
The Crank-Nicolson Method
74
Summary
We have looked at the heat-conduction/diffusion equation– We developed the Crank-Nicolson method– You will implement the algorithm– Unlike the previous implementation, there are less
restrictions on
• Large values of this ratio may cause transient oscillations
– We defined insulated boundaries• Considered their approximation using Matlab
2
t
h
The Crank-Nicolson Method
75
References
[1] Glyn James, Modern Engineering Mathematics, 4th Ed., Prentice Hall, 2007, p.782.
[2] Glyn James, Advanced Modern Engineering Mathematics, 4th Ed., Prentice Hall, 2011, p.164.
The Crank-Nicolson Method