4/19/2018
1
Topic 6b –Finite‐Difference Approximations
EE 4386/5301 Computational Methods in EE
Course InstructorDr. Raymond C. RumpfOffice: A‐337Phone: (915) 747‐6958E‐Mail: [email protected]
Outline
• What are finite‐difference approximations?
• Polynomial technique
– Description of the method
– Examples
– Implementation in MATLAB
– Examples using MATLAB
2Topic 6b ‐‐ Finite‐difference approximations
4/19/2018
2
What are Finite‐Difference Approximations?
3Topic 6b ‐‐ Finite‐difference approximations
What are Finite‐Difference Approximations? (1 of 3)
Slide 4Topic 6b ‐‐ Finite‐difference approximations
Very often in science and engineering we must calculate a derivative.
When we are processing data from measurements or simulations, there may not be an analytical equation to work with symbolically.
Typically, we only know the function at discrete points.
1x 2x 3x
1f
2f3f
4/19/2018
3
What are Finite‐Difference Approximations? (2 of 3)
Slide 5Topic 6b ‐‐ Finite‐difference approximations
Suppose we wish to numerically calculate the first‐order derivative at x2.
The first‐order derivative is slope. We can estimate the slope as riserunusing information from surrounding points.
3 12
rise
run 2
f ff x
x
1x 2x 3x
1f
2f3f
x
run = 2x
rise
= f 3
–f 1
What are Finite‐Difference Approximations? (3 of 3)
Slide 6Topic 6b ‐‐ Finite‐difference approximations
We can estimate the derivative at the midpoint between data points.
1x 2x 3x
1f
2f3f
2 11.5
f ff x
x
3 22.5
f ff x
x
1.5f
2.5f
The second‐order derivative is the slope of the slope.
2.5 1.52
3 2 2 1
3 2 12
2
f x f xf x
xf f f fx x
xf f f
x
4/19/2018
4
Our First Two Finite‐Difference Approximations
Topic 6b ‐‐ Finite‐difference approximations Slide 7
3 12 2
f ff x
x
We already derived two finite‐difference approximations
3 2 12 2
2f f ff x
x
General Concept of Finite‐Difference Approximations (1 of 2)
Topic 6b ‐‐ Finite‐difference approximations Slide 8
2 2 2 at ,f x y 1 1 1 at ,f x y
3 3 3 at ,f x y
4 4 4 at ,f x y
5 5 5 at ,f x y
6 6 6 at ,f x y
7 7 7 at ,f x y
or at ,a ai i
i ia a
f fx y
x y
Suppose we wish to estimate the function f(x) or one of its derivatives at location (xi,yi).
1 1 2 2 3 3 4 4 5 5 6 6 7 7 or a ai ia a
f fa f a f a f a f a f a f a f
x y
It is always possible to estimate this from a linear sum of the function values at surrounding points.
4/19/2018
5
General Concept of Finite‐Difference Approximations (2 of 2)
Topic 6b ‐‐ Finite‐difference approximations Slide 9
The trick is, how do we calculate the coefficients an?
These are a function of the positions of the points.
1 2 3 4 5 6 7, , , , , ,a a a a a a a 1 1,x y
7 7,x y 6 6,x y
2 2,x y 3 3,x y 4 4,x y
5 5,x y
Same coefficients
Different coefficients
Different coefficients
Finite‐difference coefficients depend only on the relative position of the points. They do not depend on the absolute positions.
Types of Finite‐Differences
Topic 6b ‐‐ Finite‐difference approximations Slide 10
2 11df f f
dx x
2f1f
xForward Finite‐Difference
Reaches ahead to use data in the forward direction.
1.5 2 1df f f
dx x
2f1f
xCentral Finite‐Difference
Reaches symmetrically to use data in both directions for highest accuracy.
2 12df f f
dx x
2f1f
xBackward Finite‐Difference
Reaches behind to use data in the backward direction.
4/19/2018
6
Continuum of Finite‐Difference Approximations (1 of 2)
Topic 6b ‐‐ Finite‐difference approximations Slide 11
Continuum of Finite‐Difference Approximations (2 of 2)
Topic 6b ‐‐ Finite‐difference approximations Slide 12
4/19/2018
7
Two Key Considerations
Topic 6b ‐‐ Finite‐difference approximations Slide 13
1. The position of the points from which the finite‐difference approximation is calculated. More closely spaced points improves accuracy, but typically leads to more computations.
2. The location of the point where the finite‐difference is being evaluated. We typically want to be as centered as possible for best accuracy.
Polynomial Technique for Deriving Finite‐Difference
Approximations
14Topic 6b ‐‐ Finite‐difference approximations
4/19/2018
8
Concept of Using Polynomials
Slide 15
We can fit an Nth order polynomial given N+1 points.
20 1 2
NNf x a a x a x a x
After we have performed the curve fit, we can interpolate the function or any of its derivatives from the polynomial.
2 3 40 1 2 3 4
2 3 11 2 3 4
2 22 3 4
33 4
2 3 4
2 6 12 1
6 24 1 2
NN
NN
NN
NN
f x a a x a x a x a x a x
f x a a x a x a x Na x
f x a a x a x N N a x
f x a a x N N N a x
Topic 6b ‐‐ Finite‐difference approximations
Easiest Point for Evaluating f(x)
Slide 16
Recall the equations we will use to evaluate f(x) or one of its derivatives:
2 3 40 1 2 3 4
2 3 11 2 3 4
2 22 3 4
2 3 4
2 6 12 1
NN
NN
NN
f x a a x a x a x a x a x
f x a a x a x a x Na x
f x a a x a x N N a x
0
1
2
3
0
0
0 2
0 6
f a
f a
f a
f a
These are most easily evaluated at x = 0 because the above equations reduce to
Topic 6b ‐‐ Finite‐difference approximations
4/19/2018
9
How to Make Any Point Easy
Slide 17
Now suppose we wish to evaluate f(x) or one of its derivatives at the general point x = xfd.
To do this, we shift our x‐axis by xfd before fitting the polynomial.
Recall that the finite‐difference coefficients depend only on the relative position of the points. An offset will not affect their values.
Now we write our polynomial at each shifted point.
21 0 1 1 2 1 1
22 0 1 2 2 2 2
21 0 1 1 2 1 1
NN
NN
NN N N N N
f x a a x a x a x
f x a a x a x a x
f x a a x a x a x
fdn nx x x
In our shifted coordinate system, our finite‐difference is being evaluated at .0x
0 1 2 30 0 0 2 0 6 f a f a f a f a
Topic 6b ‐‐ Finite‐difference approximations
Four‐Step Procedure to Derive Finite‐Difference Approximations
Slide 18
Step 1 – Identify set of points x1, x2, …, to xN from which to derive a finite‐difference approximation.
Topic 6b ‐‐ Finite‐difference approximations
20 1 2
NNf x a a x a x a x
Step 3 – Fit shifted points to a polynomial.fdi ix x x
Step 4 – Write finite‐difference approximation directly from one of the derivatives of the polynomial.
0
1
2
0
0
0 2
f a
f a
f a
Step 2 – Shift coordinates so that corresponds to where you wish to approximate the function or one of its derivatives.
0x
4/19/2018
10
Step 1 – Choose x Coordinates
Identify the x‐coordinates of the points from which you wish to approximate a derivate.
Store these in a column
1
2
1N
x
xx
x
19Topic 6b ‐‐ Finite‐difference approximations
Step 2 – Shift x‐Axis
Shift the function across the x‐axis until corresponds to the point where you wish to approximate the derivative.
20
fd 0a a
a a
d df x x f x
dx dx
Topic 6b ‐‐ Finite‐difference approximations
0x
Subtract xfd from the column vector [x] to shift coordinates.
1 fd 1
2 fd 2fd
1 fd 1N N
x x x
x x xx x x
x x x
4/19/2018
11
Step 3 – Fit Points to Polynomial (1 of 3)
Use the column vector to build matrix .
0 1
1 fd 1 fd1 1
2 2 2 fd 2 fd
1 1 1 fd 1 fd
11
1 1
1 1
N
NN
NN
N NN N N N
X x x x
x x x xx x
x x x x x x
x x x x x x
21Topic 6b ‐‐ Finite‐difference approximations
x
Insert 1’s instead of . 0x
X
Step 3 – Fit Points to Polynomial (2 of 3)
22Topic 6b ‐‐ Finite‐difference approximations
11 12 1, 1
1 21 22 2, 1
1,1 1,2 1, 1
N
N
N N N N
y y y
y y yY X
y y y
X Invert the matrix .
4/19/2018
12
Step 3 – Fit Points to Polynomial (3 of 3)
23Topic 6b ‐‐ Finite‐difference approximations
0 11 1 12 2 13 3 1, 1 1
1 21 1 22 2 23 3 2, 1 1
2 31 1 32 2 33 3 3, 1 1
1,1 1 1,2 2 1,3 3 1, 1 1
N N
N N
N N
N N N N N N
a y f y f y f y f
a y f y f y f y f
a y f y f y f y f
a y f y f y f y f
Calculate the polynomial coefficients.
11 12 13 1, 10 1
21 22 23 2, 11 21
31 32 33 3, 12 3
1,1 1,2 1,3 1, 1 1
N
N
N
N N N N NN N
y y y ya f
y y y ya f
y y y ya X f Y f a f
y y y ya f
At this point, f1 to fN+1 will be symbolic.
Step 4 – Write Finite‐Difference Approximation
0 11 1 12 2 13 3 1, 1 1
1 21 1 22 2 23 3 2, 1 1
2 31 1 32 2 33 3 3, 1 1
1,1 1 1,2 2 1,3 3 1, 1 1
N N
N N
N N
N N N N N N
a y f y f y f y f
a y f y f y f y f
a y f y f y f y f
a y f y f y f y f
24
0
1
2
22
0
0
0 2
f x a
df x a
dx
df x a
dx
Topic 6b ‐‐ Finite‐difference approximations
Recall how we interpolate the function or one of it’s derivatives given our polynomial…
11 1 12 2 13 3 1, 1 1
21 1 22 2 23 3 2, 1 1
2
1,1 1 1,2 2 1,3 3 1, 1 12
0
0
0 2 2 2 2
N N
N N
N N N N N
f x y f y f y f y f
df x y f y f y f y f
dx
df x y f y f y f y f
dx
The rows of areY
essentially our finite‐difference coefficients.
4/19/2018
13
Examples UsingPolynomial Technique
25Topic 6b ‐‐ Finite‐difference approximations
Example #1
26
Derive first‐order and second‐order finite‐difference approximations that span across three points. The approximations should be evaluated at the midpoint.
0
h
x
h
2
0 1 2
2
1
1 0 0
1
h h
X x x x
h h
1
2 2 2
0 1 0
1 2 0 1 2
1 2 1 1 2
Y X h h
h h h
0 1 2 3 20 1
1 31 2 1 1 2 3
2 2 22 3
22
0 1 0 0 1 0
1 2 0 1 2 1 2 0 1 2 2
1 2 1 1 21 2
a f f f fa f
f fa h h f a h f f h f
ha h h h f
a h f
2 2 1 2 31 2 3 2
21 1 2
2
f f fh f h f
h
fd 0 2
fd 3 11
2fd 1 2 3
22 2
2
22
f x a f
df x f fa
dx h
d f x f f fa
dx h
Topic 6b ‐‐ Finite‐difference approximations
4/19/2018
14
Example #2
27
Derive first‐order and second‐order finite‐difference approximations that span across three points. The approximations should be evaluated at the first point.
0
2
x h
h
0 1 2 2
2
1 0 0
1
1 2 2
X x x x h h
h h
1
2 2 2
1 0 0
3 2 2 1 2
1 2 1 1 2
Y X h h h
h h h
0 1 2 3 1
0 1
1 2 31 2 1 1 2 3
2 2 22 3
2
1 0 0 1 0 0
1.5 2 0.53 2 2 1 2 3 2 2 1 2
1 2 1 1 21
a f f f fa f
f f fa h h h f a h f h f h f
ha fh h h
a
2 2 2 1 2 31 2 3 2
22 1 1 2
2
f f fh f h f h f
h
fd 0 1
fd 1 2 31
2fd 1 2 3
22 2
1.5 2 0.5
22
f x a f
df x f f fa
dx h
d f x f f fa
dx h
Topic 6b ‐‐ Finite‐difference approximations
Example #3 – Higher Order Accuracy (1 of 2)
28
Let’s evaluate some derivatives at the midpoint of four discrete points.
3 2
2
2
3 2
h
hx
h
h
2 3
2 3
0 1 2 3
2 3
2 3
3 9 271
2 4 8
12 4 8
12 4 8
3 9 271
2 4 8
h h h
h h h
X x x x xh h h
h h h
1
2 2 2 2
3 3 3 3
1 9 9 1
16 16 16 161 9 9 1
24 8 8 241 1 1 1
4 4 4 41 1 1 1
6 2 2 6
h h h hY X
h h h h
h h h h
Topic 6b ‐‐ Finite‐difference approximations
4/19/2018
15
Example #3 – Higher Order Accuracy (2 of 2)
29
The coefficients are then
0 1
1 2
2 32 2 2 2
3 4
3 3 3 3
1 9 9 1
16 16 16 161 9 9 1
24 8 8 241 1 1 1
4 4 4 41 1 1 1
6 2 2 6
Y
a f
a fh h h ha f
h h h ha f
h h h h
0 1 2 3 4
1 1 2 3 4
2 1 2 3 42 2 2 2
3 1 2 3 43 3 3 3
1 9 9 1
16 16 16 161 9 9 1
24 8 8 241 1 1 1
4 4 4 41 1 1 1
6 2 2 6
a f f f f
a f f f fh h h h
a f f f fh h h h
a f f f fh h h h
1 2 3 42.5 0
2.5 1 2 3 41
22.5 1 2 3 4
2 22
9 9
16
27 27
24
22
f f f ff x a
df x f f f fa
dx x
d f x f f f fa
dx x
Topic 6b ‐‐ Finite‐difference approximations
Implementing the Polynomial Technique
Using MATLAB
30Topic 6b ‐‐ Finite‐difference approximations
4/19/2018
16
General Form of the Polynomial Fit
Slide 31
We have so far derived finite‐difference approximations symbolically.What if we want 6th‐order accurate finite‐differences? This is unreasonable to do symbolically.
Recall our matrix equation representing the polynomial written at each discrete point. It always had the following form where the w’s were just numerical constants. The h’s were symbolic.
21 012 13 1
22 122 23 2
21 1,2 1,3 1,
1
1
1
NN
NN
NN NN N N N
f aw h w h w h
f aw h w h w h
f aw h w h w h
Topic 6b ‐‐ Finite‐difference approximations
Factor Out Symbolic Term h
Slide 32
Now, we are able to separate the w terms from the h terms.
12 13 11 0
22 23 22 12
1,2 1,3 1,1
11
1
1
N
N
N N N NN NN
w w wf ah
w w wf ah
w w wf ah
Topic 6b ‐‐ Finite‐difference approximations
We were able to put numbers to all of these coefficients. This is a fully numerical matrix. It does not contain any symbolic variables.
These are our symbolic variables.
Hint: when 1W X h
So we build [W] by building and pretending h = 1.X
4/19/2018
17
Solve Matrix Equation for [a]
Slide 33
Solving our matrix equation for [a] gives1
1
12 13 10 1
22 23 21 22
1,2 1,3 1, 1
11 12 13 10
21 22 231
2
11
1
1
1
1
1
1
N
N
N N N NN NN
N
N
N
w w wa fh
w w wa fh
w w wa fh
v v v va hv v v va
h
a
h
1
2 2
1,1 1,2 1,3 1, 1
N
N N N N N N
f
f
v v v v f
Topic 6b ‐‐ Finite‐difference approximations
1V W
The key aspect here is that [W] will be completely numerical so it is easily inverted using MATLAB. This accommodates large matrices and avoids symbolic manipulation.
Incorporate Symbolic h Again
Slide 34
Next we reincorporate symbolic h by multiplying our matrices.
11 12 13 10 1
21 22 23 21 2
2
1,1 1,2 1,3 1, 1
11 12 13 1
0
21 22 231
1
1
1
1
1 1 1 1
1 1 1 1
N
N
N N N N NN N
N
N
N
v v v va fhv v v va f
hv v v va f
h
v v v va
v v va h h h h
a
1
22
11,1 1,2 1,3 1,
1 1 1 1
N
NN N N N NN N N N
fv f
fv v v v
h h h h
Topic 6b ‐‐ Finite‐difference approximations
4/19/2018
18
Extract Polynomial Coefficients
Slide 35
Next, we read off the polynomial coefficients from our matrix equation.
11 12 13 1
0 1
21 22 23 21 2
11,1 1,2 1,3 1,
1 1 1 1
1 1 1 1
1 1 1 1
N
N
N NN N N N NN N N N
v v v va f
v v v va fh h h h
a fv v v v
h h h h
Topic 6b ‐‐ Finite‐difference approximations
0 11 1 12 2 1 1
21 1 22 2 2 11
1,1 1 1,2 2 1, 1
N N
N N
N N N N NN N
a v f v f v f
v f v f v fa
h
v f v f v fa
h
Write Finite‐Difference Approximations
Slide 36
Last, we write our finite‐difference approximations from the polynomial coefficients.
Topic 6b ‐‐ Finite‐difference approximations
0 11 1 12 2 1 1
21 1 22 2 2 11
231 1 32 2 3 1
22 22 2
N N
N N
N N
f a v f v f v f
v f v f v fdfa
dx h
v f v f v fd fa
dx h
Staring at these equations long enough, we realize that the vijcoefficients can be determined completely numerically. We just have to remember to divide by h and perhaps multiply the finite‐difference expression by a constant.
4/19/2018
19
MATLAB Examples
37Topic 6b ‐‐ Finite‐difference approximations
Example #4 – 6th Order Accurate Finite‐Differences (1 of 2)
Topic 6b ‐‐ Finite‐difference approximations Slide 38
1. Here we need seven points to calculate seven polynomial coefficients.
3 2 0 2 3T
x h h h h h h
2. To build the [W] matrix, think h = 1 for now.
ˆ 3 2 1 0 1 2 3T
x
0 1 2 3 4 5 6
1 -3 9 -27 81 -243 729
1 -2 4 -8 16 -32 64
1 -1 1 -1 1 -1 1ˆ ˆ ˆ ˆ ˆ ˆ ˆ 1 W x x x x x x x
0 0 0 0 0 0
1 1 1 1 1 1 1
1 2 4 8 16 32 64
1 3 9 27 81 243 729
4/19/2018
20
Example #4 – 6th Order Accurate Finite‐Differences (2 of 2)
Topic 6b ‐‐ Finite‐difference approximations Slide 39
3. Invert [W].
1
-0.0
0.0056 -0.0750 0.7500
167 0.1500 -0.750
0 0 0 1.
0 -0.0000 0.7500 -0.1500 0.01
0000 0 0
67
-
0
V W 0.0208 -0.1667 0.2708 0.0000 -0.2708 0.1667 -0.0208
-0.0069 0.0833 -0.2708 0.3889 -0.2708 0.083
1.3611 0.7500 -0.0750 0.
3 -0.0069
-0.0042 0.0167
0056
-0.0208 -0.0000 0.0208 -0.0167 0.0042
0.0014 -0.0083 0.0208 -0.0278 0.0208 -0.0083 0.0014
4. Write the finite‐difference approximations, remembering to incorporate the symbolic h’s back in.
1 2 3 4 5 6 70
1 2 3 4 5 6 71
21 2 3 4 5 6 7
22 2
0.0167 0.15 0.75 0 0.75 0.15 0.01
0.0056 0.0750 0.7500 1.3611 0.7500 0.075
1
2 2 2 2 2 2 22
0 0.
6
0056
0 0 0 0
7
1 0 0f f f f f f ff a
f f f f f f ffa
x h
f f f f f f ffa
x h
Example #5 – Nonuniform Grid(1 of 3)
Topic 6b ‐‐ Finite‐difference approximations Slide 40
x1
= 1.1
x2
= 1.6
x3
= 2.6
x4
= 3.2
x5
= 4.7
x6
= 5.6
x7
= 6.0
x8
= 6.5
x fd
= 4
.4
Derive the finite‐difference equations for first‐and second‐order derivatives at the point xfd.
1. Choose range of points.
2.6 3.2 4.7 5.6T
x
4/19/2018
21
Example #5 – Nonuniform Grid(2 of 3)
Topic 6b ‐‐ Finite‐difference approximations Slide 41
2. Shift x axis.
fd
2.6 3.2 4.7 5.6 4.4
1.8 1.2 0.3 1.2
T
T
x x x
3. Build matrix.
0 1 2 3
1 1.8 3.24 5.832
1 1.2 1.44 1.728ˆ ˆ ˆ ˆ1 0.3 0.09 0.027
1 1.2 1.44 1.728
X x x x x
X
Note: We are not calling this the [W] matrix because we did not have to factor out a symbolic h term.
Example #5 – Nonuniform Grid(3 of 3)
Topic 6b ‐‐ Finite‐difference approximations Slide 42
4. Invert matrix.
1 0.3810 -1.0833 0.5079 0.194
0.0794 0.1389 -0.6349 0
-0.1143
.416
-0.2
646 0.4630 -0.3527
0.3000 0.9143
0.15
-0
7
4
000
4
3
.1
Y X
5. Write the finite‐difference approximations directly from the rows of .
0 1 2 3 4
1 1 2 3 4
2
2 1 2 3 42
0.3810 1.0833 0.5079
0.1
0.0794 0.1389 0
0.1944
.6349 0.2 2 2 2
143 0.3 0.914
4167
0
2
3 .1f a f f f f
fa f f f f
x
fa f f f f
x
Note: The symbolic variable h did not appear in
so it does not need to be incorporated here. x
X
Y