Download Introduction to Finite Difference Methods

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Introduction to Finite Di↵erence Methods
ME 448/548 Notes
Gerald Recktenwald
Portland State University
Department of Mechanical Engineering
[email protected]
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
Overview
1. Review of basic numerical analysis
2. Finite di↵erence notation
3. Finite di↵erence approximations
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 1
Preliminary Considerations
from Numerical Analysis
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 2
Important Ideas from Numerical Analysis
1. Symbolic versus numeric calculation
2. Numerical arithmetic and the floating point number line
3. Catastrophic cancellation errors
4. Machine precision and roundo↵
5. Truncation error
Note: Examples in these slides will use Matlab.
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 3
Symbolic versus Numeric Calculation
Commercial software for symbolic computation
•
•
•
•
DeriveTM
MACSYMATM
MapleTM
MathematicaTM
Symbolic calculations are exact. No rounding occurs because symbols and algebraic
relationships are manipulated without storing numerical values.
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 4
Symbolic versus Numeric Calculation
Example:
Evaluate f (✓) = 1
sin2 ✓
cos2 ✓ with Matlab
>> theta = 30*pi/180;
>> f = 1 - sin(theta)^2 - cos(theta)^2
f =
-1.1102e-16
f is close to, but not exactly equal to zero because of roundo↵.
Also note that f is a single value, not a formula.
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 5
Numerical Arithmetic
• Engineering analysis is usually done with real numbers
. Infinite in range
. Infinite in precision
• Computer architecture imposes limits on numerical values
. Finite range
. Finite precision
Finite precision is significantly more problematic than finite range.
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 6
Integer Arithmetic
Operation
Result
2+2=4
integer
9 ⇥ 7 = 63
integer
12
=4
3
29
=2
13
29
=0
1300
integer
exact result is not an integer
exact result is not an integer
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 7
Floating Point Arithmetic
Operation
Floating Point Value is . . .
2.0 + 2.0 = 4
exact
9.0 ⇥ 7.0 = 63
exact
12.0
=4
3.0
29
= 2.230769230769231
13
29
= 2.230769230769231 ⇥ 10
1300
exact
approximate
2
approximate
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 8
Floating Point Number Line
Compare floating point numbers to real numbers.
Real numbers
Floating point numbers
Range
Infinite: arbitrarily large and
arbitrarily small real numbers exist.
Finite: the number of bits allocated
to the exponent limit the magnitude
of floating point values.
Precision
Infinite: There is an infinite set
of real numbers between any two
real numbers.
Finite: there is a finite number
(perhaps zero) of floating point
values between any two floating
point values.
In other words: The floating point number line is a subset of the real number line.
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 9
Floating Point Number Line
denormal
overflow
under- underflow flow
usable range
–10+308
–realmax
–10-308
–realmin
0
usable range
10-308
realmin
overflow
10+308
realmax
zoom-in view
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 10
Catastrophic Cancellation Errors (1)
The errors in
c=a+b
will be large when a
and
c=a
b
b or a ⌧ b.
Consider c = a + b with
a = x.xxx . . . ⇥ 10
b = y.yyy . . . ⇥ 10
0
8
where x and y are decimal digits.
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 11
Catastrophic Cancellation Errors (1)
Evaluate c = a + b with a = x.xxx . . . ⇥ 100 and b = y.yyy . . . ⇥ 10
8
Assume for convenience of exposition that z = x + y < 10.
available precision
z
}|
{
x.xxx xxxx xxxx xxxx
0.000 0000 yyyy yyyy yyyy yyyy
x.xxx xxxx zzzz zzzz yyyy yyyy
| {z }
+
=
lost digits
The most significant digits of a are retained, but the least significant digits of b are lost
because of the mismatch in magnitude of a and b.
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 12
Catastrophic Cancellation Errors (2)
For subtraction: The error in
c=a
b
will be large when a ⇡ b.
Consider c = a
b with
a = x.xxxxxxxxxxx1ssssss
b = x.xxxxxxxxxxx0tttttt
where x, y , s and t are decimal digits. The digits sss . . . and ttt . . . are lost when a
and b are stored in double-precision, floating point format.
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 13
Catastrophic Cancellation Errors (3)
Evaluate a b in double precision floating point arithmetic when
a = x.xxx xxxx xxxx 1 and b = x.xxx xxxx xxxx 0
available precision
=
z
}|
{
x.xxx xxxx xxxx 1
x.xxx xxxx xxxx 0
0.000 0000 0000 1 uuuu uuuu uuuu
|
{z
}
unassigned digits
=
1.uuuu uuuu uuuu ⇥ 10
12
The result has only one significant digit. Values for the uuuu digits are not necessarily
zero. The absolute error in the result is small compared to either a or b. The relative
error in the result is large because ssssss tttttt 6= uuuuuu (except by chance).
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 14
Catastrophic Cancellation Errors (4)
Summary: Cancellation errors
• Occur in addition ↵ + or subtraction ↵
when ↵
or ↵ ⌧
• Occur in subtraction: ↵
when ↵ ⇡
• Are caused by a single operation (hence the term “catastrophic”) not a slow
accumulation of errors.
• Can often be minimized by algebraic rearrangement of the troublesome formula. (Cf.
improved quadratic formula.)
Note: roundo↵ errors that are not catastrophic can also cause problems.
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 15
Machine Precision
The magnitude of roundo↵ errors is quantified by machine precision "m.
There is a number, "m > 0, such that
1+
=1
whenever | | < "m.
In exact arithmetic 1 +
zero.
= 1 only when
= 0, so in exact arithmetic "m is identically
Matlab uses double precision (64 bit) arithmetic. The built-in variable eps stores the
value of "m.
16
eps = 2.2204 ⇥ 10
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 16
Implications for Routine Calculations
• Floating point comparisons should test for “close enough” instead of exact equality.
• Express “close” in terms of
or
absolute di↵erence, |x
relative di↵erence,
|x
y|
y|
|x|
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 17
Floating Point Comparison
Don’t ask, “is x equal to y ?”.
if x==y
...
end
%
Don’t do this
Instead ask, “are x and y ‘close enough’ in value?”
if abs(x-y) < tol
...
end
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 18
Practical Implications: Absolute and Relative Error (1)
“Close enough” can be measured with absolute di↵erence or relative di↵erence, or both.
Let ↵ = an exact or reference value, and ↵
b = the value we have computed
Absolute error
Relative error
Eabs(b
↵) = ↵
b
Erel(b
↵) =
↵
↵
b
↵
↵
b
↵
↵ref
Often we choose ↵ref = ↵ so that
Erel(b
↵) =
↵
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 19
Absolute and Relative Error (2)
Example:
Approximating sin(x) for small x
Since
sin(x) = x
x3
x5
+
3!
5!
...
we can approximate sin(x) with
sin(x) ⇡ x
for small enough |x| < 1
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 20
Absolute and Relative Error (3)
The absolute error in approximating sin(x) ⇡ x for small x is
Eabs = x
sin(x) =
x3
3!
x5
+ ...
5!
And the relative error is
Eabs =
x
sin(x)
x
=
sin(x)
sin(x)
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
1
page 21
Absolute and Relative Error (4)
Plot relative and absolute error in approximating sin(x) with x.
Error in approximating sin(x) with x
−3
Although the absolute error is
relatively flat around x = 0, the
relative error grows more quickly.
20
x 10
Absolute Error
Relative Error
15
The relative error grows quickly
because the absolute value of
sin(x) is small near x = 0.
Error
10
5
0
−5
−0.4
−0.3
−0.2
−0.1
0
x (radians)
0.1
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
0.2
0.3
page 22
Practical Implications: Iteration termination (1)
An iteration generates a sequence of scalar values xk , k = 1, 2, 3, . . ..
The sequence converges to a limit, ⇠ , if
|xk
where
⇠| < ,
for all k > N,
is a small.
In practice, the test is usually expressed as
|xk+1
xk | < ,
when k > N.
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 23
Iteration termination (2)
Absolute convergence criterion
Iterate until |x
xold| <
a
where
a
is the absolute convergence tolerance.
In Matlab:
x = ...
xold = ...
%
initialize
while abs(x-xold) > deltaa
xold = x;
update x
end
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 24
Iteration termination (3)
Relative convergence criterion
Iterate until
x
xold
<
xref
r
where
r
is the absolute convergence tolerance.
In Matlab:
x = ...
xold = ...
xref = ...
%
initialize
while abs((x-xold)/xref) > deltar
xold = x;
update x
end
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 25
Truncation Error: Distinct from Roundo↵ Error
Consider the series for sin(x)
sin(x) = x
x3
x5
+
3!
5!
···
For small x, only a few terms are needed to get a good approximation to sin(x).
The · · · terms are “truncated”
ftrue = fsum + truncation error
The size of the truncation error depends on x and the number of terms included in fsum.
For typical work we can only estimate the truncation error.
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 26
Truncation error in the series approximation of sin(x) (1)
function ssum = sinser(x,tol,n)
% sinser Evaluate the series representation of the sine function
%
% Input:
x
= argument of the sine function, i.e., compute sin(x)
%
tol = tolerance on accumulated sum.
%
n
= maximum number of terms.
%
% Output:
ssum = value of series sum after nterms or tolerance is met
term = x;
ssum = term;
%
Initialize series
fprintf(’Series approximation to sin(%f)\n\n k
fprintf(’%3d %11.3e %12.8f\n’,1,term,ssum);
for k=3:2:(2*n-1)
term = -term * x*x/(k*(k-1));
ssum = ssum + term;
fprintf(’%3d %11.3e %12.8f\n’,k,term,ssum);
if abs(term/ssum)<tol, break; end
end
term
ssum\n’,x);
%
Next term in the series
%
True at convergence
fprintf(’\nTruncation error after %d terms is %g\n\n’,(k+1)/2,abs(ssum-sin(x)));
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 27
Truncation error in the series approximation of sin(x) (2)
For small x, the series for sin(x) converges in a few terms
>> s = sinser(pi/6,5e-9,15);
Series approximation to sin(0.523599)
k
1
3
5
7
9
11
term
5.236e-001
-2.392e-002
3.280e-004
-2.141e-006
8.151e-009
-2.032e-011
ssum
0.52359878
0.49967418
0.50000213
0.49999999
0.50000000
0.50000000
Truncation error after 6 terms is 3.56382e-014
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 28
Truncation error in the series approximation of sin(x) (3)
The truncation error in the series is small relative to the true value of sin(⇡/6)
>> s = sinser(pi/6,5e-9,15);
.
.
.
>> err = (s-sin(pi/6))/sin(pi/6)
err =
-7.1276e-014
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 29
Truncation error in the series approximation of sin(x) (4)
For larger x, the series for sin(x) converges more slowly
>> s = sinser(15*pi/6,5e-9,15);
Series approximation to sin(7.853982)
k
1
3
5
.
.
.
25
27
29
term
7.854e+000
-8.075e+001
2.490e+002
.
.
.
1.537e-003
-1.350e-004
1.026e-005
ssum
7.85398163
-72.89153055
176.14792646
.
.
.
1.00012542
0.99999038
1.00000064
Truncation error after 15 terms is 6.42624e-007
More terms are needed to reach a converge tolerance of 5 ⇥ 10
9
.
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 30
Taylor Series: a tool for analyzing truncation error
For a sufficiently continuous function f (x) defined on the interval x 2 [a, b] we define
the nth order Taylor Series approximation Pn(x)
Pn(x) = f (x0)+(x
x0 )
df
dx
+
x=x0
(x
x 0 )2 d 2 f
2
dx2
+· · ·+
x=x0
(x
x 0 )n d n f
n!
dxn
x=x0
Then there exists ⇠ with x0  ⇠  x such that
f (x) = Pn(x) + Rn(x)
where
Rn(x) =
(x
x0)(n+1) d(n+1)f
(n + 1)!
dx(n+1)
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
x=⇠
page 31
Big “O” notation
Big “O ” notation
(x
f (x) = Pn(x) + O
or, for x
x0)(n+1)
(n + 1)!
!
x0 = h we say
⇣
⌘
(n+1)
f (x) = Pn(x) + O h
The Big O lumps multiplicative factors (like 1/(n + 1)!) into constants which are
(usually) ignored.
When working with Big O notation, we focus on the power of the term inside the
parenthesis.
Since h ! 0 as we refine the computations, larger powers are more beneficial.
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 32
Taylor Series Example
Consider the function
f (x) =
1
1
x
The Taylor Series approximations to f (x) of order 1, 2 and 3 are
P1(x)
=
P2(x)
=
P3(x)
=
1
1
x0
1
1
x0
1
1
x0
+
x
(1
x0
x 0 )2
+
x
(1
x0
(x
+
2
x0 )
(1
x 0 )2
x 0 )3
+
x
(1
x0
(x
+
x 0 )2
(1
x 0 )2
(x
+
x 0 )3
(1
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
x 0 )3
x 0 )4
page 33
Taylor Series: Pi(x) near x = 1.6
(5)
−0.5
Approximations to f(x) = 1/(1−x)
−1
−1.5
−2
−2.5
−3
−3.5
−4
exact
P1(x)
−4.5
P2(x)
P3(x)
−5
1.3
1.4
1.5
1.6
x
1.7
1.8
1.9
2
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 34
Roundo↵ and Truncation Errors (1)
Both roundo↵ and truncation errors occur in numerical computation.
Example:
Finite di↵erence approximation to f 0(x) = df /dx
0
f (x) =
f (x + h)
h
f (x)
h 00
f (x) + . . .
2
This approximation is said to be first order because the leading term in the truncation
error is linear in h. Dropping the truncation error terms we obtain
0
ffd(x) =
f (x + h)
h
f (x)
or
0
0
ffd(x) = f (x) + O(h)
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 35
Roundo↵ and Truncation Errors (2)
To study the roles of roundo↵ and truncation errors, compute the finite di↵erence1
approximation to f 0(x) when f (x) = ex.
0
The relative error in the ffd
(x) approximation to
d x
e is
dx
0
0
ffd
(x) f 0(x)
ffd
(x)
=
f 0(x)
ex
Erel =
ex
1 The finite di↵erence approximation is used to obtain numerical solutions to ordinary and partial di↵erentials equations where
f (x) and hence f 0 (x) is unknown.
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 36
Roundo↵ and Truncation Errors (3)
Evaluate Erel at x = 1 for a range of h.
0
Truncation error
dominates at large h.
10
Roundo↵ error in
f (x + h) f (h)
dominates as h ! 0.
10
−1
10
−2
Roundoff error
dominates
Truncation error
dominates
Relative error
−3
10
−4
10
−5
10
−6
10
−7
10
−8
10 −12
10
−10
10
−8
10
−6
10
Stepsize, h
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
−4
10
−2
10
0
10
page 37
Finite Di↵erence Approximations
to the Heat Equation
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 38
Model Problem: 1D Heat Equation
@u
@ 2u
= ↵ 2,
@t
@x
0xL
(1)
u(0, t) = 0,
u(L, t) = 0
(2)
u(x, 0) = u0(x)
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
(3)
page 39
Example: Place a hot pot on a table.
What is the transient temperature distribution in the table?
t<0
t>0
T
L
0
L
x
T
T
0
x
increasing time
L
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 40
Model Problem: 1D Heat Equation
Exact Solution
u(x, t) =
1
X
un(x, t) =
n=1
1
X
2
2
2
An exp( ↵n ⇡ t/L ) sin
n=1
2
An =
L
Z
L
u0(x) sin
0
✓
n⇡x
L
◆
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
dx.
✓
n⇡x
L
◆
(4)
(5)
page 41
Example: Decay of u0(x) = sin(⇡x/L)
We will use the following problem to test our finite-di↵erence solutions.
@u
@ 2u
= ↵ 2,
@t
@x
0  x  L,
t
0
(6.a)
(6.b)
u(0, t) = u(L, t) = 0
✓ ◆
⇡x
u(x, 0) = sin
.
L
(6.c)
This model problem has a simple solution that is convenient as a benchmark for numerical
schemes for the heat equation.
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 42
Example: Analytical solution to decay of u0(x) = sin(⇡x/L)
Substituting Equation (6.c) into Equation (5) gives
An =
2
L
Z
L
sin
0
✓
⇡x
L
◆
sin
✓
n⇡x
L
◆
dx.
The sine function is orthogonal in the following sense
Z
L
sin
0
✓
⇡x
L
◆
sin
✓
n⇡x
L
◆
dx =
(
L/2 if n = 1
0
otherwise
so that
A1 = 1,
An = 0,
n = 2, 3, . . .
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 43
Example: Analytical solution to decay of u0(x) = sin(⇡x/L)
Therefore, the analytical solution to the toy problem is
!
✓ ◆
⇡x
↵⇡ 2t
.
u(x, t) = exp
sin
L2
L
(7)
The solution is just an exponential decay of the initial condition for this problem and
problems with Dirichlet BC and no source term.
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 44
Example: Analytical solution to decay of a square pulse
u0(x) =
8
>
>
<0,
if 0  x < xl
uc
uc, if xl  x  xr
>
>
:0, if xr < x  L.
0
xl
xr
L
An infinite number of An terms are needed to match the initial condition.
✓
◆
Z xr
2
n⇡x
An =
uc sin
dx
L xl
L

✓
◆
2uc
n⇡xl
=
cos
n⇡
L
cos
✓
n⇡xr
L
◆
n = 0, 1, 2, . . .
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 45
Example: Analytical solution to decay of a square pulse
1.2
t = 1.0e−04
t = 1.0e−03
t = 1.0e−02
t = 5.0e−02
1
0.8
T(x,t)
0.6
0.4
0.2
0
−0.2
0
0.2
0.4
x
0.6
0.8
1
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 46
Numerical Model
Continuous
PDE for u(x,t)
Finite
difference
or
Finite volume
or
Finite element
or
Boundar element
or ...
Discrete
difference
equation
Algebraic
solution
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
uik approximation
to u(xi,tk)
page 47
Finite Di↵erence Mesh
The finite di↵erence solution is obtained at a finite set of points. These points are called
nodes and the network of these nodes is called a mesh or grid.
On a uniform mesh, the x-direction nodes are located at
xi = (i
1) x,
i = 1, 2, . . . , nx,
x=
L
nx
1
(8)
where nx is the total number of spatial nodes, including those on the boundary.
i=1
x=0
...
i–1
i
i–1
xi – 1
xi
xi + 1
∆x
...
i = nx
x=L
∆x
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 48
Finite Di↵erence Mesh
Analogous to the x-direction mesh, there are nodes at discrete times.
For uniform time steps
tk = (k
1) t,
k = 1, 2, . . . , nt,
t=
tmax
nt 1
(9)
where nt is the number of time steps.
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 49
Finite Di↵erence Mesh
The x and t direction nodes form a finite version of a semi-infinite strip
t
..
.
..
.
..
.
..
.
..
.
..
.
..
.
Interior node: u(x,t) is initially unknown.
Boundary node: u(0,t) and u(0,t) are known
or computable from additional data.
Initial condition: u(x,0) must be specified.
k+1
k
k 1
t=0, k=1
i=1
x=0
i 1 i i+1
nx
x=L
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 50
Nomenclature
For the one-dimensional mesh, we define
Symbol
Meaning
u(x, t)
Analytical solution (true solution).
u(xi, tk )
Analytical solution evaluated at x = xi, t = tk .
uki
Approximate numerical solution at x = xi, t = tk .
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 51
First order forward finite di↵erence
Use a Taylor series expansion about the point xi
u(xi + x) = u(xi) + x
@u
@x
+
xi
x2 @ 2 u
2 @x2
+
xi
x3 @ 3 u
3! @x3
xi
+ ···
where x is a small distance, and x2 and x3 are shorthand for ( x)2 and ( x)3.
Solve for @u/@x|x
@u
@x
i
=
xi
u(xi + x)
x
u(xi)
x @ 2u
2 @x2
xi
x2 @ 3 u
3! @x3
xi
+ ···
In the limit as x ! 0 the coefficients of the higher order derivative terms vanish, and
the first term on the right hand side becomes equal to the derivative on the left hand side.
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 52
First order forward finite di↵erence
Replace x with
@u
@x
xi, and substitute ui ⇡ u(xi)
x = xi+1
xi
⇡
ui+1
x @ 2u
2 @x2
ui
x
xi
x2 @ 3 u
3! @x3
xi
+ ···
(10)
The mean value theorem can be used to replace the higher order derivatives (exactly)
x @ 2u
2 @x2
xi
( x)2 @ 3u
+
3! @x3
xi
x @ 2u
+ ··· =
2 @x2
⇠
where xi  ⇠  xi+1. Thus
@u
@x
xi
⇡
ui+1
ui
x
+
x @ 2u
2 @x2
or
⇠
@u
@x
ui+1
xi
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
ui
x
⇡
x @ 2u
2 @x2
(11)
⇠
page 53
First order forward finite di↵erence
In general, ⇠ is not known. Since x is the only parameter under the user’s control that
determines the error, the truncation error is simply written
x @ 2u
2 @x2
⇠
= O( x)
This expression means that the left hand side is bounded by a product of an unknown
constant times x. Although the expression does not give us the exact magnitude of
( x/2) (@ 2u/@x2) ⇠ , it indicates how quickly that term approaches zero as x is
reduced.
Recall that the point of Big O notation is to look at the exponent of the truncation error
term.
We conclude that the error in the first order forward di↵erence formula is linear in the
mesh spacing.
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 54
First order forward finite di↵erence
Using big O notation, Equation (10) is written
@u
@x
=
xi
ui+1
ui
x
+ O( x).
(12)
Equation (12) is called the forward di↵erence formula for @u/@x|xi because it involves
nodes xi and xi+1.
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 55
First order backward finite di↵erence
Repeat the preceding analysis with a Taylor series expansion for u(xi
ui
1
= ui
x
@u
@x
+
xi
x2 @ 2 u
2 @x2
xi
( x)3 @ 3u
3! @x3
x)
xi
+ ···
(13)
Solve for @u/@x|x to get
i
@u
@x
=
xi
ui
ui
x
1
x @ 2u
+
2 @x2
( x)2 @ 3u
3! @x3
xi
xi
+ ···
or
@u
@x
=
xi
ui
ui
x
1
+ O( x).
(14)
This is called the backward di↵erence formula because it involves the values of u at xi
and xi 1.
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 56
First order central di↵erence
Combine Taylor Series expansions for ui+1 and ui
@u
@x
=
xi
ui+1 ui
2 x
1
1
to get
2
+ O( x )
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
(15)
page 57
Summary of first order di↵erence approximations
@u
@x
Forward:
Backward:
@u
@x
@u
@x
Central:
=
ui+1
x
xi
=
ui
xi
=
xi
ui
ui
x
1
+ O( x)
+ O( x)
ui+1 ui
2 x
1
+ O( x2)
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 58
Second Order Central Di↵erence
The Taylor series expansion for ui+1 is
ui+1 = ui +
x
@u
@x
+
xi
x2 @ 2 u
2 @x2
+
xi
( x)3 @ 3u
3! @x3
xi
+ ···
(16)
Adding Equation (13) and Equation (16) yields
ui+1 + ui
1
= 2ui +
x
2
@ 2u
@x2
+
xi
2 x4 @ 4 u
4! @x4
xi
+ ···
Notice that all odd derivatives cancel because of the alternating signs of terms on the
right hand side of Equation (13).
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
page 59
Second Order Central Di↵erence
Solving for
@ 2u
@x2
gives
xi
@ 2u
@x2
=
ui
2ui + ui+1
+
x2
1
xi
x2 @ 4 u
6 @x4
xi
+ ···
or
@ 2u
@x2
=
xi
ui
1
2ui + ui+1
2
+ O( x ).
2
x
ME 448/548: Introduction to Finite Di↵erence Approximation of the Heat Equation
(17)
page 60