0% found this document useful (0 votes)
259 views26 pages

FTCS Solution To The Heat Equation: ME 448/548 Notes

1) The document describes the finite difference time domain (FTCS) method for solving the heat equation numerically. 2) FTCS uses forward differences in time and central differences in space to approximate derivatives in the heat equation. This results in an explicit formula to calculate temperatures at each node independently at each time step. 3) The method is demonstrated through MATLAB code that implements the FTCS scheme and compares the numerical solution to an exact solution, showing good agreement. However, the FTCS method is only conditionally stable depending on the time and space step sizes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
259 views26 pages

FTCS Solution To The Heat Equation: ME 448/548 Notes

1) The document describes the finite difference time domain (FTCS) method for solving the heat equation numerically. 2) FTCS uses forward differences in time and central differences in space to approximate derivatives in the heat equation. This results in an explicit formula to calculate temperatures at each node independently at each time step. 3) The method is demonstrated through MATLAB code that implements the FTCS scheme and compares the numerical solution to an exact solution, showing good agreement. However, the FTCS method is only conditionally stable depending on the time and space step sizes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

FTCS Solution to the Heat Equation

ME 448/548 Notes

Gerald Recktenwald
Portland State University
Department of Mechanical Engineering
[email protected]

ME 448/548: FTCS Solution to the Heat Equation


Overview

1. Use the forward finite difference approximation to ∂u/∂t.

∂u uk+1
i − uki

∂t ∆t

2. Use the central difference approximation to ∂ 2u/∂x2 at time tk .



2
∂ u uki−1 − 2uki + uki+1
=
∂x2 ∆x2
xi

3. Solve for uk+1


i . The computational formula is explicit: each value of u k+1
i can be
updated independently.
4. FTCS is easy to implement, but is only conditionally stable
2

5. Truncation errors are O (∆x) and O(∆t).

ME 448/548: FTCS Solution to the Heat Equation page 1


Finite Difference Operators

Choose the forward difference to evaluate the time derivative at t = tk .

uk+1 − uki

∂u i
= + O(∆t) (1)
∂t tk ,xi ∆t

Approximate the spatial derivative with the central difference operator and take all nodal
values at time tk .
2
∂ u uki−1 − 2uki + uki+1 2
= + O(∆x ) (2)
∂x2 ∆x2

xi

ME 448/548: FTCS Solution to the Heat Equation page 2


FTCS Approximation to the Heat Equation

Substitute Equation (1) and Equation (2) into the heat equation
k k k
uk+1
i − u k
i u i−1 − 2u i + u i+1 2
=α + O(∆t) + O(∆x ) (3)
∆t ∆x2

Drop truncation error terms to get

uk+1
i − uki uki−1 − 2uki + uki+1
=α (4)
∆t ∆x2

ME 448/548: FTCS Solution to the Heat Equation page 3


FTCS Computational Molecule

FTCS scheme enables


explicit calculation of
t .. .. .. .. .. .. ..
. . . . . . . u at this node
k+1
k
k 1
Solution is known
for these nodes

t=0, k=1
i=1 i 1 i i+1 nx
x=0 x=L

ME 448/548: FTCS Solution to the Heat Equation page 4


FTCS Approximation to the Heat Equation

Solve Equation (4) for uk+1


i t .. .. .. .. .. .. ..
. . . . . . .
k+1 k k k k+1
ui = rui+1 + (1 − 2r)ui + rui−1 (5)
k
k 1
where r = α∆t/∆x2.
FTCS is an explicit scheme because it provides a
t=0, k=1
simple formula to update uk+1
i independently of the i=1 i 1 i i+1 nx
other nodal values at tk+1. x=0 x=L

ME 448/548: FTCS Solution to the Heat Equation page 5


demoFTCS Code
function errout = demoFTCS(nx,nt)

% ... Comments and processing of optional inputs skipped

% --- Assign physical and mesh parameters


alfa = 0.1; L = 1; tmax = 2; % Diffusion coefficient, domain length and max time
dx = L/(nx-1); dt = tmax/(nt-1);
r = alfa*dt/dx^2; r2 = 1 - 2*r;

% --- Assign IC and BC. u is initialized to a vector that includes BC


x = linspace(0,L,nx)’; u = sin(pi*x/L);

% --- Loop over time steps


for k=2:nt
uold = u; % prepare for next step
for i=2:nx-1
u(i) = r*uold(i-1) + r2*uold(i) + r*uold(i+1);
end
end

Remember that the formula for updating uk+1


i is
k+1 k k k
ui = rui+1 + (1 − 2r)ui + rui−1

ME 448/548: FTCS Solution to the Heat Equation page 6


Alternative formulation to the FTCS Algorithm

Equation (5) can be expressed as a matrix multiplication.


(k+1) (k)
u = Au (6)

where u(k+1) is the vector of u values at time step k + 1, u(k) is the vector of u values
at time step k, and A is the tridiagonal matrix
 
1 0 0 0 0 0
r (1 − 2r) r 0 0 0
 
0 r (1 − 2r) r 0 0
 
A= ... ... ... . (7)
0 0 0
(1 − 2r)
 
0 0 0 r r
0 0 0 0 0 1

The first and last rows of A are set to enforce the Dirichlet boundary conditions at x = 0
and x = L.

ME 448/548: FTCS Solution to the Heat Equation page 7


Run the demoFTCS code

0.14
FTCS
>> demoFTCS Exact
Error in FTCS solution = 0.002221 0.12

0.1

0.08
u

0.06

0.04

0.02

0
0 0.2 0.4 0.6 0.8 1
x

ME 448/548: FTCS Solution to the Heat Equation page 8


Unstable FTCS Solution
movieFTCS(15):
2 2 2
Initial Condition Initial Condition Initial Condition
FTCS solution FTCS solution FTCS solution
1.5 t = 0.41 1.5 t = 1.63 1.5 t = 1.96

1 1 1

0.5 0.5 0.5

0 0 0

−0.5 −0.5 −0.5

−1 −1 −1
0 0.2 0.4 0.6 0.8 1 0 0.2 0.4 0.6 0.8 1 0 0.2 0.4 0.6 0.8 1

2 2 2
Initial Condition Initial Condition Initial Condition
FTCS solution FTCS solution FTCS solution
1.5 t = 0.82 1.5 t = 1.88 1.5 t = 2.00

1 1 1

0.5 0.5 0.5

0 0 0

−0.5 −0.5 −0.5

−1 −1 −1
0 0.2 0.4 0.6 0.8 1 0 0.2 0.4 0.6 0.8 1 0 0.2 0.4 0.6 0.8 1

2 2 2
Initial Condition Initial Condition Initial Condition
FTCS solution FTCS solution FTCS solution
1.5 t = 1.22 1.5 t = 1.92 1.5 t = 2.04

1 1 1

0.5 0.5 0.5

0 0 0

−0.5 −0.5 −0.5

−1 −1 −1
0 0.2 0.4 0.6 0.8 1 0 0.2 0.4 0.6 0.8 1 0 0.2 0.4 0.6 0.8 1

ME 448/548: FTCS Solution to the Heat Equation page 9


Conditionally Stable FTCS Scheme

Observations:
• The FTCS solution appears to be stable at first
• Toward the end of the simulation time, oscillations grow exponentially
• Instability is not caused by truncation error
• Instability is fed by round-off errors, but not directly caused by round-off

We’ll use a simplified form of Fourier Stability Analysis


• Suppose that the initial condition is a small sine wave
• The correct solution is a decay of the sine wave
• Under what condition does the solution grow instead of decay?

Stability or instability is an intrinsic property of the scheme

FTCS is conditionally stable for solutions to the heat equation.

ME 448/548: FTCS Solution to the Heat Equation page 10


Stability Analysis

Suppose the initial condition looks like this


 
πx i−1
u0(x) = σ cos = σ(−1)
∆x

where σ is a suitably small value

The solution at time step k = 2 is


(2) 
ui = r u0(xi+1) + u0(xi−1) + (1 − 2r)u0(xi)
i i−2  i−1
= rσ (−1) + (−1) + (1 − 2r)σ(−1)
i+1 i−1  i−1
= −rσ (−1) + (−1) + (1 − 2r)σ(−1)

ME 448/548: FTCS Solution to the Heat Equation page 11


Stability Analysis
Since (−1)i+1 = (−1)i−1 for any i, the solution at k = 2 can be further simplified
(2) i−1 i−1
ui = −2rσ(−1) + (1 − 2r)σ(−1)
i−1
= (1 − 4r)σ(−1)
= (1 − 4r)u0(xi)

The pattern is
(2)
ui = (1 − 4r)u0(xi)
(3) (2) 2
ui = (1 − 4r)ui (xi) = (1 − 4r) u0(xi)
(4) (3) 3
ui = (1 − 4r)ui (xi) = (1 − 4r) u0(xi)

(2)
where ui is the numerical solution at xi and k = 2;
and where (1 − 4r)2 is the square of (1 − 4r)

ME 448/548: FTCS Solution to the Heat Equation page 12


Stability Analysis

The general pattern is


k k−1
ui = (1 − 4r) u0(xi).

where (1 − 4r)k−1 is (1 − 4r) raised to the k − 1 power.

Therefore, the solution grows when |1 − 4r| > 1.

Therefore, stability requires 1 − 4r < 1 and −(1 − 4r) < 1.

1
1 − 4r < 1 =⇒ −4r < 2 =⇒ r > − true for any r > 0.
2
The second case is
1
−(1 − 4r) < 1 =⇒ 4r < 2 =⇒ r <
2

ME 448/548: FTCS Solution to the Heat Equation page 13


Stability Analysis

The quick and dirty stability analysis shows that the FTCS scheme is stable only if

α∆t 1
r= < . (8)
∆x2 2

Recall: α is a parameter of the physical problem.

We must choose ∆t and ∆x so that Equation (8) is satisfied.

ME 448/548: FTCS Solution to the Heat Equation page 14


Working with the FTCS Stability Criterion

To increase accuracy, we want to decrease both ∆x and ∆t.

For a given ∆x, the stability limit for FTCS imposes an upper limit on ∆t

∆x2
∆t < (9)

1
Choosing ∆t and ∆x so that r < does not guarantee an accurate numerical solution.
2
1
r< only guarantees that the FTCS solution will not blow up.
2

ME 448/548: FTCS Solution to the Heat Equation page 15


Working with the FTCS Stability Criterion

When reducing ∆x and ∆t to


improve accuracy, follow a path
Unstable
log(∆t) (∆x)2
like A→C not A→B. B ∆t = 2α
A
Note that the axes have
logarithmic scales.
Stable
C

log(∆x)

ME 448/548: FTCS Solution to the Heat Equation page 16


Measuring the FTCS Truncation Error

The error reported by demoFTCS is defined as

1 k
E(nx, nt) = √ kui − u(xi, tk )k2 (10)
nx


The factor of 1/ nx converts kuki − u(xi, tk )k2 to an average error.

ME 448/548: FTCS Solution to the Heat Equation page 17


Measuring the FTCS Truncation Error

Designate the local error at x = xi and t = tk as


k k
ei = ui − u(xi, tk ). (11)

Define ēk as an RMS average error per node at time step tk


" nx
#1/2
k 1 X k 2
ē ≡ (ei ) (12)
nx i=1

Algebraic substitution shows that E(nx, nt) = ēk .

If the solution to the heat equation is smooth, then E(nx, nt) = O(∆t) + O(∆x2)

ME 448/548: FTCS Solution to the Heat Equation page 18


Measuring the truncation error

Systematically reduce ∆x and ∆t to determine whether truncation error prediction is


realized by the code.

We know the exact solution, so we use Equation(10).

This is a test of whether the code correctly implements FTCS.

Recall that for FTCS


2
E(nx, nt) = O(∆t) + O(∆x )

but stability requires ∆t = C∆x2, where C is a constant.

Therefore, a stable solution to FTCS should demonstrate


2
E(nx, nt) = O(∆x )

ME 448/548: FTCS Solution to the Heat Equation page 19


Measuring the truncation error

Suppose we don’t know the exponent of the truncation error for our code.

In other words, instead of


2
E(nx, nt) = O(∆x )
we have
p
E(nx, nt) = O(∆x )
where p is uknown
L
Since ∆x = we have
nx − 1

Lp
 
p
E(nx, nt) = O(∆x ) = O
(nx − 1)p

ME 448/548: FTCS Solution to the Heat Equation page 20


Measuring the truncation error

Simplify the preceding expression


p
E(nx, nt) = O(∆x )
Lp
 
=O
(nx − 1)p
 
1
∼O ignore multiplicative constants
(nx − 1)p
 
1
∼O leading powers dominate
npx

Therefore, we expect  
1
E(nx, nt) = O
npx

ME 448/548: FTCS Solution to the Heat Equation page 21


Measuring the truncation error

Measure E(nx, nt) on two difference meshes, nx,1 and nx,2.

With  
1
E(nx, nt) = O
npx
we form the ratio
npx,1 nx,1 p
 
E(nx,2, nt)
= p =
E(nx,1, nt) nx,2 nx,2

Solve for p
log (E(nx,2, nt)/E(nx,1, nt))
p= .
log (nx,1/nx,2)

ME 448/548: FTCS Solution to the Heat Equation page 22


convFTCS Code

function convFTCS
% convFTCS Convergence of FTCS on a series of finer spatial meshes

% --- Set constants to be consistent with demoFTCS


alfa = 0.1; L = 1; tmax=2; rsafe = 0.49999; % stable r<0.5

% --- Specify nx and compute nt consistent with stability limit


nx = [8 16 32 64 128 256]; dx = L./(nx-1);
nt = ceil( 1 + alfa*tmax*((nx-1).^2)/(rsafe*L^2) );

% --- Loop over mesh sizes, store error and compute order of scheme
fprintf(’\n nx nt error E(j)/E(j-1) p\n’);
er = NaN; p = 0;
for j=1:length(nx);
e(j) = demoFTCS(nx(j),nt(j));
if j>1
er = e(j)/e(j-1); p = log(er)/log(nx(j-1)/nx(j));
end
fprintf(’ %5d %5d %11.3e %8.4f %8.4f\n’,nx(j),nt(j),e(j),er,p);
end

% -- plotting code skipped ...

ME 448/548: FTCS Solution to the Heat Equation page 23


Running the convFTCS Code

−2
10
FTCS
>> convFTCS ideal

nx nt error E(j)/E(j-1) p −3
10
8 21 6.028e-03 NaN 0.0000
16 92 1.356e-03 0.2249 2.1524
32 386 3.262e-04 0.2406 2.0553

E(nx,ny)
−4
64 1589 7.972e-05 0.2444 2.0329 10
128 6453 1.970e-05 0.2471 2.0170
256 26012 4.895e-06 0.2485 2.0085
−5
10
The last column of text output shows
that demoFTCS has the right truncation
error behavior. −6
10 −3 −2 −1 0
10 10 10 10
x

ME 448/548: FTCS Solution to the Heat Equation page 24


Summary for the FTCS Scheme

• FTCS is easy to implement. The update formula is


k+1 k k k
ui = rui+1 + (1 − 2r)ui + rui−1

• The FTCS scheme is conditionally stable when

α∆t
r= < 1/2
∆x2
In two spatial dimensions with ∆y = ∆x, the stability condition is r < 1/4. In 3D
with ∆y = ∆z = ∆x, the stability condition is r < 1/8.
• There are much better schemes for solving the heat equation.
• FTCS is a toy used to introduce the numerical solution of PDEs

ME 448/548: FTCS Solution to the Heat Equation page 25

You might also like