Lecture 17 - Solving the heat equation using finite difference methods Chapter 13

advertisement
Chapter 13
Lecture 17 - Solving the heat
equation using finite
difference methods
13.1
Approximating the Derivatives of a Function
by Finite Differences
Recall that the derivative of a function was defined by taking the limit of a
difference quotient:
f (x + Δx) − f (x)
.
Δx→0
Δx
f (x) = lim
(13.1)
Now to use the computer to solve differential equations we go in the opposite
direction - we replace derivatives by appropriate difference quotients. If we
assume that the function can be differentiated many times then Taylor’s
Theorem is a very useful device in determining the appropriate difference
quotient to use. For example consider
f (x + Δx) = f (x) + Δxf (x) +
Δx2 Δx3 (3)
Δx4 (4)
f (x) +
f (x) +
f (x) + . (13.2)
..
2!
3!
4!
Re-arranging terms in (2) and dividing by Δx we obtain
Δx Δx2 (3)
f (x + Δx) − f (x)
= f (x) +
f (x) +
f (x) + . . . .
Δx
2
3!
85
Lecture 17 - Solving the heat equation using finite difference methods
If we take the limit Δx → 0 then we recover (1). But for our purposes it is
more useful to retain the approximation
f (x + Δx) − f (x)
Δx
Δx f (ξ)
2
= f (x) + O(Δx).
= f (x) +
(13.3)
Δx f (ξ) in (3) as a measure of the error involved when
2
we approximate f (x) by the difference quotient f (x + Δx) − f (x) /Δx.
Notice that this error depends on how large f is in the interval [x, x + Δx]
(i.e. on the smoothness of f ) and on the size of Δx. Since we like to focus
on that part of the error we can control we say that the error term is of the
order Δx – denoted by O(Δx). Technically a term or function E(Δx) is
O(Δx) if
We retain the term
E(Δx)
Δx
Δx→0
→
const.
Now the difference quotient (3) is not the only one that can be used to
approximate f (x). Indeed if we consider the expansion of f (x − Δx):
f (x − Δx) = f (x) − Δxf (x) +
Δx2 Δx3 (3)
Δx4 (4)
f (x) −
f (x) +
f (x) + . . (13.4)
..
2!
3!
4!
and we subtract (4) from (2) and divide by (2Δx) we obtain:
f (x + Δx) − f (x − Δx)
Δx2 (3)
= f (x) +
f (ξ).
2Δx
3!
(13.5)
We notice that the error term associated with this form of difference approximation is O(Δx2 ), which converges more rapidly to zero as Δx → 0.
In order to obtain an approximation to f (x) we add (2) to (4) which
upon re-arrangement and dividing by Δx2 leads to:
f (x + Δx) − 2f (x) + f (x − Δx)
1
= f (x) + Δx2 f (4) (ξ).
2
Δx
12
(13.6)
Due to the symmetry of the difference approximations (5) and (6) about the
expansion point x these are called central difference approximations. The
difference approximation (3) is known as a forward difference approximation.
We note that the central difference schemes (5) and (6) are second order
accurate while the forward difference scheme (3) is only O(Δx).
86
13.2. HEAT EQUATION SOLUTION BY FINITE DIFFERENCES
13.2
Heat Equation solution by Finite Differences
Consider the following initial-boundary value problem for the heat equation
∂2u
∂u
= α2 2 0 < x < 1, t > 0
∂t
∂x
BC: u(0, t) = 0 u(1, t) = 0
(13.8)
IC: u(x, 0) = f (x).
(13.9)
(13.7)
The basic idea is to replace the derivatives in the heat equation by difference quotients. We consider the relationships between u at (x, t) and its
neighbours a distance Δx apart and at a time Δt later.
Corresponding to the difference quotient approximations introduced in
Section 1, we consider the following partial difference approximations.
Forward Difference in Time:
u(x, t + Δt) = u(x, t) + Δt
∂u
Δt2 ∂ 2 u
(x, t) +
(x, t) + · · · .
∂t
2! ∂x2
After re-arrangement and division by Δt:
u(x, t + Δt) − u(x, t)
∂u
=
(u, t) + O(Δt).
Δt
∂t
(13.10)
Central Differences in Space:
∂u
(x, t) +
∂x
∂u
u(x − Δx, t) = u(x, t) − Δx (x, t) +
∂x
u(x + Δx, t) = u(x, t) + Δx
Δx2 ∂ 2 u
(u, t) +
2! ∂x2
Δx2 ∂ 2 u
(x, t) −
2! ∂x2
Δx3 ∂ 3 u
(x, t) +
3! ∂x3
Δx3 ∂ 3 u
(x, t) +
3! ∂x3
Δx4 ∂ 4 u
(x, t) + · · ·
4! ∂x2
Δx4 ∂ 4 u
(x, t) + · · · .
4! ∂x4
Adding and re-arranging:
u(x + Δx, t) − 2u(x, t) + u(x − Δx, t)
∂2u
=
(x, t) + O(Δx2 ). (13.11)
Δx2
∂x2
Substituting (2) and (3) into (1a) we obtain
u(x, t + Δt) − u(x, t)
2 u(x + Δx, t) − 2u(x, t) + u(x − Δx, t)
=α
+ O(Δt, Δx2 ).
Δt
Δx2
Re-arranging:
u(x, t + Δt) = u(x, t) + α
2
Δt
Δx2
{u(x + Δx, t) − 2u(x, t) + u(x − Δx, t)}
(13.12)
.
87
Lecture 17 - Solving the heat equation using finite difference methods
We subdivide the spatial interval [0, 1] into N + 1 equally spaced sample
points xn = nΔx. The time interval [0, T ] is subdivided into M + 1 equal
time levels tk = kΔt. At each of these space-time sample points we introduce
approximations:
u(xn , tk ) ukn .
uk+1
0
tk+1
6
Δt
tk ?
u
u
uk0
uk+1
n
uk+1
N
uk+1
n
u
ukn−1
=
ukn
+α
2
u
T
T
u
ukn
-
u
T
T
T
Tu
ukn+1
u
ukN −1
ukN
Δx
Δt
Δx2
ukn+1 − 2ukn + ukn−1
↑ This is implemented in the spread sheets Heat0 and Heat.
88
u
13.2. HEAT EQUATION SOLUTION BY FINITE DIFFERENCES
Implementing Derivative Boundary Conditions:
Assume that the boundary conditions (1b) are changed to
BC: u(0, t) = 0,
∂u
(1, t) = 0.
∂x
∂u
Consider a central difference approximation to
(1, t), where xN = N Δx =
∂x
1,
u(xN + Δx, t) − u(xN − Δx, t)
= 0.
Δx
Re-arranging we obtain:
u(xN + Δx, t) = u(xN − Δx, t) (∗)
Since xN = 1 we observe that xN +Δx is outside the domain we introduce
an extra column uN +1 into which we copy the values uN −1 . In the column
xN we implement the same difference approximation for the Heat Equation,
namely:
= ukN + α2 (
uk+1
N
Δt
)(ukN +1 − 2ukN + ukN −1 )
Δx2
(∗∗)
↑ This is implemented in the spread sheet Heat0f.
while ukN +1 = ukN −1 (see (*) ) since column ukN −1 is copied to column ukN +1 .
Note that this BC could be implemented another way without introducing
the additional column, by eliminating uN +1 from (∗) and (∗∗):
Δt k
k+1
k
2
k
−
u
u
uN = uN + 2α
N −1
N .
Δx2
If this latter equation is implemented at xN there is no need to introduce
an extra column UN +1 or to implement the difference equation given in (**)
as the the derivative boundary condition is taken care of automatically.
Some EXERCISES and Observations: – Heat Equation
1. Change the Δt in cell D1 from 0.001 to 0.05 and you will observe what
is known as a numerical instability. Now change Δt to 0.00625, which
is known as the stability boundary and observe what happens. Now
let Δt = 0.006 and observe the abrupt change in the solution - it is
much closer to what we would expect.
89
Lecture 17 - Solving the heat equation using finite difference methods
2. The instability noted in 1. above is not the only source of error in the
numerical approximation. Although numerical instability is evident for
a parameter choice that is unstable, the other type of error is present
in almost every type of numerical approximation scheme. This class of
error results from discarding the O(Δx2 ) and O(Δt) terms in (2) and
(3) when we replace derivatives in (1a) by difference quotients. This
error is known as the truncation error. To determine the truncation
error change the spread sheet to implement the initial condition
2x
0 < x < 1/2
f (x) =
.
2(1 − x) 1/2 ≤ x < 1
Now code up the Fourier Series (in another spread sheet) that is derived on page 21 of the notes and compare the numerical solution to
the ‘exact’ Fourier Series solution with 50 terms. The difference between the two is mainly due to the truncation error since the round-off
error is about 10−12 and does not grow if stable parameters are used.
3. Implement derivative boundary conditions on both endpoints x = 0 and
x = 1. Check the numerical solution against the problem solved in
HW1 #3.
90
Download