Docsity

Finite-Difference Approximations-Skripta-Paralelno programiranje-Informatika, Rezime od Paralelno programiranje

Toggle sidebar
bg1
Finite-Difference Approximations
to the Heat Equation
Gerald W. Recktenwald∗
January 21, 2004
Abstract
This article provides a practical overview of numerical solutions to
the heat equation using the finite difference method. The forward time,
centered space (FTCS), the backward time, centered space (BTCS), and
Crank-Nicolson schemes are developed, and applied to a simple problem
involving the one-dimensional heat equation. Complete, working Mat-
lab codes for each scheme are presented. The results of running the
codes on finer (one-dimensional) meshes, and with smaller time steps is
demonstrated. These sample calculations show that the schemes real-
ize theoretical predictions of how their truncation errors depend on mesh
spacing and time step. The Matlab codes are straightforward and al-
low the reader to see the differences in implementation between explicit
method (FTCS) and implicit methods (BTCS and Crank-Nicolson). The
codes also allow the reader to experiment with the stability limit of the
FTCS scheme.
1 The Heat Equation
The one dimensional heat equation is
∂φ
∂t =α∂2φ
∂x2,0≤x≤L, t ≥0 (1)
where φ=φ(x, t) is the dependent variable, and αis a constant coefficient.
Equation (1) is a model of transient heat conduction in a slab of material with
thickness L. The domain of the solution is a semi-infinite strip of width Lthat
continues indefinitely in time. The material property αis the thermal diffusivity.
In a practical computation, the solution is obtained only for a finite time, say
tmax.
Solution to Equation (1) requires specification of boundary conditions at
x=0andx=L, and initial conditions at t= 0. Simple boundary and initial
conditions are
φ(0,t)=φ0,φ(L, t)=φLφ(x, 0) = f0(x).(2)
Other boundary conditions, e.g. gradient (Neumann) or mixed conditions, can
be specified. To keep the presentation as simple as possible, only the conditions
in (2) are considered in this article.
∗Associate Professor, Mechanical Engineering Department Portland State University, Port-
land, Oregon, [email protected]
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Delimični pregled teksta

Preuzmite Finite-Difference Approximations-Skripta-Paralelno programiranje-Informatika i više Rezime u PDF od Paralelno programiranje samo na Docsity!

Finite-Difference Approximations

to the Heat Equation

Gerald W. Recktenwald∗

January 21, 2004

Abstract This article provides a practical overview of numerical solutions to the heat equation using the finite difference method. The forward time, centered space (FTCS), the backward time, centered space (BTCS), and Crank-Nicolson schemes are developed, and applied to a simple problem involving the one-dimensional heat equation. Complete, working Mat- lab codes for each scheme are presented. The results of running the codes on finer (one-dimensional) meshes, and with smaller time steps is demonstrated. These sample calculations show that the schemes real- ize theoretical predictions of how their truncation errors depend on mesh spacing and time step. The Matlab codes are straightforward and al- low the reader to see the differences in implementation between explicit method (FTCS) and implicit methods (BTCS and Crank-Nicolson). The codes also allow the reader to experiment with the stability limit of the FTCS scheme.

1 The Heat Equation

The one dimensional heat equation is

∂φ ∂t

= α

∂^2 φ ∂x^2

, 0 ≤ x ≤ L, t ≥ 0 (1)

where φ = φ(x, t) is the dependent variable, and α is a constant coefficient. Equation (1) is a model of transient heat conduction in a slab of material with thickness L. The domain of the solution is a semi-infinite strip of width L that continues indefinitely in time. The material property α is the thermal diffusivity. In a practical computation, the solution is obtained only for a finite time, say tmax. Solution to Equation (1) requires specification of boundary conditions at x = 0 and x = L, and initial conditions at t = 0. Simple boundary and initial conditions are

φ(0, t) = φ 0 , φ(L, t) = φL φ(x, 0) = f 0 (x). (2)

Other boundary conditions, e.g. gradient (Neumann) or mixed conditions, can be specified. To keep the presentation as simple as possible, only the conditions in (2) are considered in this article. ∗Associate Professor, Mechanical Engineering Department Portland State University, Port- land, Oregon, [email protected]

2 FINITE DIFFERENCE METHOD 2

2 Finite Difference Method

The finite difference method is one of several techniques for obtaining numerical solutions to Equation (1). In all numerical solutions the continuous partial differential equation (PDE) is replaced with a discrete approximation. In this context the word “discrete” means that the numerical solution is known only at a finite number of points in the physical domain. The number of those points can be selected by the user of the numerical method. In general, increasing the number of points not only increases the resolution (i.e., detail), but also the accuracy of the numerical solution. The discrete approximation results in a set of algebraic equations that are evaluated (or solved) for the values of the discrete unknowns. Figure 1 is a schematic representation of the numerical solution. The mesh is the set of locations where the discrete solution is computed. These points are called nodes, and if one were to draw lines between adjacent nodes in the domain the resulting image would resemble a net or mesh. Two key parameters of the mesh are ∆x, the local distance between adjacent points in space, and ∆t, the local distance between adjacent time steps. For the simple examples considered in this article ∆x and ∆t are uniform throughout the mesh. In Section 2.1 the mesh is defined. The core idea of the finite-difference method is to replace continuous deriva- tives with so-called difference formulas that involve only the discrete values associated with positions on the mesh. In Section 2.2 a handful of difference formulas are developed. Applying the finite-difference method to a differential equation involves re- placing all derivatives with difference formulas. In the heat equation there are derivatives with respect to time, and derivatives with respect to space. Us- ing different combinations of mesh points in the difference formulas results in different schemes. In the limit as the mesh spacing (∆x and ∆t) go to zero, the numerical solution obtained with any useful^1 scheme will approach the true solution to the original differential equation. However, the rate at which the numerical solution approaches the true solution varies with the scheme. In ad- dition, there are some practically useful schemes that can fail to yield a solution for bad combinations of ∆x and ∆t. Three different schemes for the solution to Equation (1) are developed in Section 3. The numerical solution of the heat equation is discussed in many textbooks. Ames [1], Morton and Mayers [3], and Cooper [2] provide a more mathematical development of finite difference methods. See Cooper [2] for modern introduc- tion to the theory of partial differential equations along with a brief coverage of numerical methods. Fletcher [4], Golub and Ortega [5], and Hoffman [6] take a more applied approach that also introduces implementation issues. Fletcher provides Fortran code for several methods.

2.1 The Discrete Mesh

The finite difference method obtains an approximate solution for φ(x, t) at a finite set of x and t. For the codes developed in this article the discrete x are

(^1) There are plausible schemes that do not exhibit this important property of converging to the true solution. Refer to discussions of consistency in references [1, 2].

2 FINITE DIFFERENCE METHOD 4

t

1 i –1 i i +1 N

m +

m

m –

x =0 x = L

t =

Figure 2: Mesh on a semi-infinite strip used for solution to the one-dimensional heat equation. The solid squares indicate the location of the (known) initial values. The open squares indicate the location of the (known) boundary values. The open circles indicate the position of the interior points where the finite difference approximation is computed.

where the quantities on the right hand side are defined on the finite difference mesh. Approximations to the governing differential equation are obtained by replacing all continuous derivatives by discrete formulas such as those in Equa- tion (3). The relationship between the continuous (exact) solution and the discrete approximation is shown in Figure 1. Note that computing φmi from the finite difference model is a distinct step from translating the continuous problem to the discrete problem. Finite difference formulas are first developed with the dependent variable φ as a function of only one independent variable, x, i.e. φ = φ(x). The resulting formulas are then used to approximate derivatives with respect to either space or time. By initially working with φ = φ(x), the notation is simplified without any loss of generality in the result.

2.3 First Order Forward Difference

Consider a Taylor series expansion φ(x) about the point xi

φ(xi + δx) = φ(xi) + δx

∂φ ∂x

xi

δx^2 2

∂^2 φ ∂x^2

xi

δx^3 3!

∂^3 φ ∂x^3

xi

where δx is a change in x relative to xi. Let δx = ∆x in Equation (4), i.e., consider the value of φ at the location of the xi+1 mesh line

φ(xi + ∆x) = φ(xi) + ∆x

∂φ ∂x

xi

∆x^2 2

∂^2 φ ∂x^2

xi

∆x^3 3!

∂^3 φ ∂x^3

xi

Solve for (∂φ/∂x)xi

∂φ ∂x

xi

φ(xi + ∆x) − φ(xi) ∆x

∆x 2

∂^2 φ ∂x^2

xi

∆x^2 3!

∂^3 φ ∂x^3

xi

2 FINITE DIFFERENCE METHOD 5

Notice that the powers of ∆x multiplying the partial derivatives on the right hand side have been reduced by one. Substitute the approximate solution for the exact solution, i.e., use φi ≈ φ(xi) and φi+1 ≈ φ(xi + ∆x).

∂φ ∂x

xi

φi+1 − φi ∆x

∆x 2

∂^2 φ ∂x^2

xi

∆x^2 3!

∂^3 φ ∂x^3

xi

The mean value theorem can be used to replace the higher order derivatives (exactly) ∆x^2 2

∂^2 φ ∂x^2

xi

(∆x)^3 3!

∂^3 φ ∂x^3

xi

∆x^2 2

∂^2 φ ∂x^2

ξ

where xi ≤ ξ ≤ xi+1. Thus

∂φ ∂x

xi

φi+1 − φi ∆x

∆x^2 2

∂^2 φ ∂x^2

ξ

or ∂φ ∂x

xi

φi+1 − φi ∆x

∆x^2 2

∂^2 φ ∂x^2

ξ

The term on the right hand side of Equation (6) is called the truncation error of the finite difference approximation. It is the error that results from truncating the series in Equation (5). In general, ξ is not known. Furthermore, since the function φ(x, t) is also unknown, ∂^2 φ/∂x^2 cannot be computed. Although the exact magnitude of the truncation error cannot be known (unless the true solution φ(x, t) is available in analytical form), the “big O” notation can be used to express the dependence of the truncation error on the mesh spacing. Note that the right hand side of Equation (6) contain the mesh parameter ∆x, which is chosen by the person using the finite difference simulation. Since this is the only parameter under the user’s control that determines the error, the truncation error is simply written

∆x^2 2

∂^2 φ ∂x^2

ξ

= O(∆x^2 )

The equals sign in this expression is true in the order of magnitude sense. In other words the “= O(∆x^2 )” on the right hand side of the expression is not a strict equality. Rather, the expression means that the left hand side is a product of an unknown constant and ∆x^2. Although the expression does not give us the exact magnitude of (∆x^2 )/2((∂^2 φ/∂x^2 )xi )ξ , it tells us how quickly that term approaches zero as ∆x is reduced. Using big O notation, Equation (5) can be written

∂φ ∂x

xi

φi+1 − φi ∆x

  • O(∆x) (7)

Equation (7) is called the forward difference formula for (∂φ/∂x)xi because it involves nodes xi and xi+1. The forward difference approximation has a truncation error that is O(∆x). The size of the truncation error is (mostly) under our control because we can choose the mesh size ∆x. The part of the truncation error that is not under our control is |∂φ/∂x|ξ.

3 SCHEMES FOR THE HEAT EQUATION 7

the truncation error for the central difference approximation goes to zero much faster than the truncation error in Equation (7) or Equation (8). There is a complication with Equation (11) because it does not include the value for φi. This may cause problems when the central difference approximation is included in an approximation to a differential equation.

2.6 Second Order Central Difference

Finite difference approximations to higher order derivatives can be obtained with the additional manipulations of the Taylor Series expansion about φ(xi). Adding Equation (9) and Equation (10) yields

φi+1 + φi− 1 = 2φi + (δx)^2

∂^2 φ ∂x^2

xi

2(δx)^4 4!

∂^4 φ ∂x^4

xi

Solving for (∂^2 φ/∂x^2 )xi gives

∂^2 φ ∂x^2

xi

φi+1 − 2 φi + φi+ ∆x^2

(δx)^2 12

∂^4 φ ∂x^4

xi

Or, using order notation,

∂^2 φ ∂x^2

xi

φi+1 − 2 φi + φi+ ∆x^2

  • O(∆x^2 ) (12)

This is also called the central difference approximation, but (obviously) it is the approximation to the second derivative, whereas Equation (11) is the central difference approximation to the first derivative.

3 Schemes for the Heat Equation

The finite difference approximations developed in the preceding section are now assembled into a discrete approximation to Equation (1). Both the time and space derivatives are replaced by finite differences. Doing so requires specifica- tion both the time and spatial locations of the φ values in the finite difference formulas. Therefore, we need to introduce superscript m to designate the time step of the discrete solution. Though this seems like only is a bookkeeping issue, we shall soon see that choosing the time step at which the spatial deriva- tives are evaluated will have a large impact on the performance and ease of implementation of the finite difference model.

3.1 Forward Time, Centered Space

Approximate the time derivative in Equation (1) with a forward difference

∂φ ∂t

tm+1,xi

φm i +1− φmi ∆t

  • O(∆t) (13)

Note that terms on the right hand side only involve φ at x = xi.

3 SCHEMES FOR THE HEAT EQUATION 8

Use the central difference approximation to (∂^2 φ/∂x^2 )xi and evaluate all terms at time m.

∂^2 φ ∂x^2

xi

φmi− 1 − 2 φmi + φmi+ ∆x^2

  • O(∆x^2 ). (14)

Substitute Equation (13) into the left hand side of Equation (1); substitute Equation (14) into the right hand side of Equation (1); and collect the truncation error terms to get

φm i +1− φmi ∆t

= α

φmi− 1 − 2 φmi + φmi+ ∆x^2

  • O(∆t) + O(∆x^2 ) (15)

The temporal errors and the spatial errors have different orders. Also notice that we can explicitly solve for φm i +1in terms of the other values of φ. Drop the truncation error terms from Equation (15) and solve for φm i +1to get

φm i +1= φmi +

α∆t ∆x^2 (φmi+1 − 2 φmi + φmi− 1 ) (16)

Equation (16) is called the Forward Time, Centered Space or FTCS approxi- mation to the heat equation. A slight improvement in computational efficiency can be obtained with a small rearrangement of Equation (16)

φm i +1= rφmi+1 + (1 − 2 r)φmi + rφmi− 1 (17)

where r = α∆t/∆x^2. The FTCS scheme is easy to implement because the values of φm i +1can be updated independently of each other. The entire solution is contained in two loops: an outer loop over all time steps, and an inner loop over all interior nodes. The code fragment in Listing 1 shows how easy it is to implement the FTCS scheme^3. Equation (16) can be represented by the computational molecule or stencil in the left third of Figure 3. Notice that the value of φm i +1does not depend on φm i−+1 1 or φm i+1+1 for the FTCS scheme. Thus, this scheme behaves more like a the solution to a hyperbolic differential equation than a parabolic differential equation. The solutions to Equation (1) subject to the initial and boundary conditions in Equation (2) are all bounded, decaying functions. In other words, the magni- tude of the solution will decrease from the initial condition to a constant. The FTCS can yield unstable solutions that oscillate and grow if ∆t is too large. Stable solutions with the FTCS scheme are only obtained if

r =

α∆t ∆x^2

(^3) The inner for loop could be vectorized by replacing it with u(2:n-1) = ruold(1:n-2) + r2uold(2:nx-1) + r*uold(3:nx)

Furthermore, since the right hand side of this expression is evaluated in its entirety before assigning it to the left hand side, the uold variable can be eliminated completely. Although these code optimizations will reduce the execution time, the less efficient code makes the intent of the loop more clear.

3 SCHEMES FOR THE HEAT EQUATION 10

i –1 i i +

m +

m i –1 i i +

m +

m i –1 i i +

m +

m

Forward Time, Centered Space

Backward Time, Centered Space Crank-Nicholson

Figure 3: Computational molecules for finite-difference approximations to the heat equation.

Substitute Equation (19) into the left hand side of Equation (1); substitute Equation (14) into the right hand side of Equation (1); and collect the truncation error terms to get

φmi − φm i−^1 ∆t = α

φmi− 1 − 2 φmi + φmi+ ∆x^2

  • O(∆t) + O(∆x^2 ) (20)

The truncation errors in this approximation have the same order of magnitude as the truncation errors in Equation (15). Unlike Equation (15), however, with Equation (20) cannot be rearranged to obtain a simple algebraic formula for computing for φmi in terms of its neighbors φmi+1, φmi− 1 , and φm i −^1. Developing a similar equation for φmi+1 (or simply adding 1 to each spatial subscript in Equation (20)) shows that φmi+1 depends on φmi+2 and φmi. Thus, Equation (20) is one equation in a system of equations for the values of φ at the internal nodes of the spatial mesh (i = 2, 3 ,... , N − 1). To see the system of equations more clearly, drop the truncation error terms from Equation (20) and rearrange the resulting equation to get

−

α ∆x^2

φmi− 1 +

∆t

2 α ∆x^2

φmi −

α ∆x^2

φmi+1 =

∆t

φm i −^1 (21)

The system of equations can be represented in matrix form as        

b 1 c 1 0 0 0 0 a 2 b 2 c 2 0 0 0 0 a 3 b 3 c 3 0 0 0 0

0 0 0 aN − 1 bN − 1 cN − 1 0 0 0 0 aN bN

φ 1 φ 2 φ 3 .. . φN − 1 φN

d 1 d 2 d 3 .. . dN − 1 dN

where the coefficients of the interior nodes are

ai = −α/∆x^2 , i = 2, 3 ,... , N − 1 bi = (1/∆t) + (2α/∆x^2 ), ci = −α/∆x^2 , di = (1/∆t)φm i −^1.

For the Dirichlet boundary conditions in Equation (2)

b 1 = 1, c 1 = 0, d 1 = φ 0 aN = 0, bN = 1, dn = φL

3 SCHEMES FOR THE HEAT EQUATION 11

Implementation of the BTCS scheme requires solving a system of equations at each time step. In addition to the complication of developing the code, the computational effort per time step for the BTCS scheme is greater than the computational effort per time step of the FTCS scheme^4. The BTCS scheme has one huge advantage over the FTCS scheme: it is unconditional stable (for solutions to the heat equation). The BTCS scheme is just as accurate as the FTCS scheme. Therefore, with some extra effort, the BTCS scheme yields a computational model that is robust to choices of ∆t and ∆x. This advantage is not always overwhelming, however, and the FTCS scheme is still useful for some problems.

3.3 Crank-Nicolson

The FTCS and BTCS schemes have a temporal truncation error of O(∆t). When time-accurate solutions are important, the Crank-Nicolson scheme has significant advantages. The Crank-Nicolson scheme is not significantly more difficult to implement than the BTCS scheme, and it has a temporal truncation error that is O(∆t^2 ). The Crank-Nicolson scheme is implicit, like BTCS, it is also unconditional stable [1, 7, 8]. The left hand side of the heat equation is approximated with the backward time difference used in the BTCS scheme, i.e. Equation (19). The right hand side of the heat equation is approximated with the average of the central dif- ference scheme evaluated at the current and the previous time step. Thus, Equation (1) is approximated with

φmi − φm i−^1 ∆t

[

φmi− 1 − 2 φmi + φmi+ ∆x^2

φm i−− 1 1 − 2 φm i −^1 + φm i+1−^1 ∆x^2

]

Notice that values of φ from time step m and time step m − 1 appear on the right hand side. Equation (23) is used to predict the values of φ at time m, so all values of φ at time m − 1 are assumed to be known. Rearranging Equation (23) so that values of φ at time m are on the left, and values of φ at time m − 1 are on the right gives.

α 2∆x^2

φmi− 1 +

∆t

α ∆x^2

φmi − α 2∆x^2

φmi+1 =

1 ∆t

φm i −^1 + − α 2∆x^2

φm i−− 1 1 +

∆t

α ∆x^2

φm i −^1 − α 2∆x^2

φm i+1−^1 (24)

The Crank-Nicolson scheme is implicit, and as a result a system of equations for the φ must be solved at each time step. The system of equations is identical

(^4) For transient problems in one space dimension, the computation cost difference is not too important. For transient problems with two or three space dimensions, however, the computational effort per time step for BTCS is much greater than the computational effort per time step of FTCS. Nonetheless, the superior stability of BTCS usually provides an overall computational advantage.

4 IMPLEMENTATION AND VERIFICATION 13

(^00) 0.2 0.4 0.6 0.8 1

x

u

FTCS Exact

Figure 4: FTCS solution to the heat equation at t = 0.5 obtained with r = 2. The solution seems to be stable, but only because the duration of the simulation is not long enough for the instability to become apparent.

dt, dx, r = 5.556e-002 5.263e-002 2.

heatCN

Norm of error = 1.883e-003 at t = 0. dt, dx, r = 5.556e-002 5.263e-002 2.

Note that the L 2 norm of the error for the FTCS scheme and BTCS scheme are comparable: 0.024 and 0.027. The error of the Crank-Nicolson scheme is 0 .0018, which is more than an order of magnitude smaller than either the FTCS and BTCS schemes. In § 4.4 below, the variation of the errors with ∆x and ∆t is explored.

4.3 Stability

The heat equation is a model of diffusive systems. For all problems with constant boundary values, the solutions of the heat equation decay from an initial state to a non-varying steady state condition. The transient behavior of these solutions are smooth and bounded: the solution does not develop local or global maxima that are outside the range of the initial data. In section 3.1 it was asserted that the FTCS scheme can exhibit instability. An unstable numerical solution is one in which the values of φ at the nodes may oscillate or the magnitude grow outside the bounds of the initial and bound- ary conditions. According to Equation (18) the solution in Figure 4 should be unstable because the value of r = 2 for this simulation, an r value that is signif- icantly higher than the stability limit of 0.5. The FTCS scheme is unstable for r > 0 .5. By increasing the length of the simulation time to 1.0 (and correspond- ingly increasing nt to maintain the same value of ∆t), the unstable behavior is made apparent. In the next Matlab session, the values of nt and tmax are

4 IMPLEMENTATION AND VERIFICATION 14

(^00) 0.2 0.4 0.6 0.8 1

1

x

T(x,t)

t = 0. t = 0. t = 0. t = 0. t = 1.

Figure 5: FTCS solution to the heat equation at t = 1 obtained with r = 2. The instability in the solution is now obvious.

both increased by a factor of two, and the solution from the heatFTCS is saved in the x, t, and T variables. function

[err,x,t,Phi] = heatFTCS(20,20,0.1,1,1);

Norm of error = 1.069e-001 at t = 1. dt, dx, r = 5.263e-002 5.263e-002 1.

The error at the end of the simulation has increased significantly. The showTheat function can be used to plot the T fields at selected time steps.

showTheat(x,t([1 5 10 15 20]),Phi(:,[1 5 10 15 20]) )

The expression t([1 5 10 15 20]) selects the values of ti for i = 1, 5 , 10 , 15 , 20. The expression Phi(:,[1 5 10 15 20]) selects columns 1, 5, 10, 15, and 20 from the Phi matrix. The plot created by showTheat is shown in Figure 5. Note that the solution appears smooth as late as t = 0.74. However, by t = 1 significant oscillation in the solution is evidence of instability in the FTCS scheme. In contrast to the FTCS scheme, the BTCS and Crank-Nicholson schemes are unconditionally stable. Repeating the preceding calculations with the BTCS scheme gives this text output

[err,x,t,Phi] = heatBTCS(20,20,0.1,1,1);

Norm of error = 3.134e-002 at t = 1. dt, dx, r = 5.263e-002 5.263e-002 1.

To view curves of φ(x, t) use the showTheat command

showTheat(x,t([1 5 10 15 20]),Phi(:,[1 5 10 15 20]) )

which produces the plot in Figure 6. Repeating the calculations with the Crank- Nicolson scheme would give qualitatively similar results, but with much greater absolute accuracy (smaller errors).

REFERENCES 16

Kx∆x^2  Kt∆t 1 and Kx∆x^2  Kt∆t 2 then

TE 2 TE 1

∆x = constant

Kt∆t 2 Kt∆t 1

∆t 2 ∆t 1

Given the numerical solution φmi , i = 1,... , N at time step m, one scalar measure of the truncation error as

e = ‖φmi − φ(xi, tm)‖ 2. (28)

One could also use the L 1 or L∞ norms. The value of e is used to verify that the FTCS and BTCS solutions have truncation errors that decrease as O(∆t) + O(∆x^2 ). In other words, e can be used as TE in Equations (25), (26), or (27).

4.5 Truncation Error Measurements

Figure 7 shows how the truncation error of the FTCS and BTCS codes varies when ∆t is reduced and ∆x is fixed. The solutions were obtained for α = 0.1, L = 1, tmax = 0.5. The dashed line shows e ∼ ∆t, which is the theoretically predicted variation of the truncation error with ∆t for the FTCS and BTCS schemes. The BTCS scheme can obtain solutions for much larger ∆t than the FTCS scheme because the BTCS scheme is unconditionally stable. For ∆x = 3. 448 × 10 −^2 and α = 0.1, the stability limit for the FTCS scheme is ∆t = 5. 94 × 10 −^3. As ∆t → 0, the truncation errors for the FTCS and BTCS schemes approach constant values. Further reduction in ∆t do not reduce the error because the contribution of the spatial error is fixed (when ∆x is fixed). In other words, as ∆t → 0, Equation (27) does not hold because the constant Kx∆x terms in the numerator and denominator of Equation (26) are not negligible. Thus, as ∆t → 0 for finite ∆x, the TE of BTCS and FTCS approach constants. For intermediate ∆t, Equation (27) holds. Figure 8 shows that the dependence of the truncation error for both schemes is nearly identical except at very small ∆x. As ∆x becomes very small, the Kt∆t terms in the numerator and denominator of Equation (26) become important.

References

[1] William F. Ames. Numerical Methods for Partial Differential Equations. Academic Press, Inc., Boston, third edition, 1992.

[2] Jeffery Cooper. Introductin to Partial Differential Equations with Matlab. Birkh¨auser, Boston, 1998.

[3] K.W. Morton and D.F. Mayers. Numerical Solution of Partial Differen- tial Equations: An Introduction. Cambridge University Press, Cambridge, England, 1994.

[4] Clive A.J. Fletcher. Computational Techniquess for Fluid Dynamics. Springer-Verlag, Berlin, 1988.

[5] Gene Golub and James M. Ortega. Scientific Computing: An Introduction with Parallel Computing. Academic Press, Inc., Boston, 1993.

REFERENCES 17

[6] Joe D. Hoffman. Numerical Methods for Engineers and Scientists. McGraw- Hill, New York, 1992.

[7] R. L. Burden and J. D. Faires. Numerical Analysis. Brooks/Cole Publishing Co., New York, sixth edition, 1997.

[8] Eugene Isaacson and Herbert Bishop Keller. Analysis of Numerical Methods. Dover, New York, 1994.

[9] Erwin Kreyszig. Advanced Engineering Mathematics. Wiley, New York, seventh edition, 1993.

REFERENCES 19

Appendix A: Order Notation

The formal definition of O( ) is as follows. We say that some function f (s) is O(ψ(s)) for s ∈ Ω if |f (s)| ≤ C|ψ(s)| ∀ s ∈ Ω

Suppose for concreteness that we have an approximation with a truncation error that is O(∆x^2 ). By this we mean that the upper limit for the truncation error is C∆x^2 where C is an unknown constant. The value of C does not need to be known for us to make good use of the truncation error estimate. For the particular case where the truncation error is O(∆x^2 ) we have

Error for ∆x 2 Error for ∆x 1

C|(∆x 2 )^2 | C|(∆x 1 )^2 |

[

∆x 2 ∆x 1

] 2

So as long as we work with the ratio of errors, the value of the constant does not matter.

Appendix B: Exact Solution

The general solution of Equation (1) with the boundary and initial conditions

φ(0, t) = φ(L, t) = 0, φ(x, 0) = f 0 (x) (29)

is (See e.g., Kreyszig [9, §11.5].)

φ(x, t) =

∑^ ∞

n=

bn sin

( (^) nπx L

exp

αn^2 π^2 t L^2

where the bn are obtained from

bn =

L
∫ L

0

f 0 (x) sin

( (^) nπx L

dx (31)

Example: f 0 (x) = sin (πx/L). Compute

bn =

L
∫ L

0

sin

( (^) πx L

sin

( (^) nπx L

dx

but (^) ∫ L

0

sin

( (^) πx L

sin

( (^) nπx L

dx =

L/ 2 if n = 1 0 otherwise

Therefore,

b 1 = 1, bn = 0, n = 2, 3 ,...

The exact solution to Equation (1) subject to initial and bound conditions in Equation (29), and with f 0 (x) = sin ((πx)/(L)) is

φ(x, t) = sin

( (^) πx L

exp

απ^2 t L^2

REFERENCES 20

Appendix C: Solving a Tridiagonal System

The system of equations for the BTCS scheme can be represented in matrix form as Av = d where

A =

b 1 c 1 0 0 0 0 a 2 b 2 c 2 0 0 0 0 a 3 b 3 c 3 0 0 0 0

0 0 0 aN − 1 bN − 1 cN − 1 0 0 0 0 aN bN

, v =

φ 1 φ 2 φ 3 .. . φN − 1 φN

, d =

d 1 d 2 d 3 .. . dN − 1 dN

This system can be efficiently solved using LU factorization with backward substitution. Since the coefficient matrix is known to by positive definite (and symmetric in the case of conduction or diffusion without convection), we can use LU factorization without pivoting. If the coefficient matrix is A, we wish to find L and U such that A = LU. It turns out that L and U have the form

L =

e 1 a 2 e 2 a 3 e 3

.. .

an− 1 en− 1 an en

U =

1 f 1 1 f 2 1 f 3

.. .

1 fn− 1 1

Evaluating each nonzero term in the product LU and setting it equal to the corresponding entry in A gives

e 1 = b 1 e 1 f 1 = c 1

a 2 = a 2 a 2 f 1 + e 2 = b 2 e 2 f 2 = c 2

ai = ai aifi− 1 + ei = bi eifi = ci .. .

an = an anfn− 1 + en = bn