7/20/2017
1
Lecture 10 Slide 1
EE 5337
Computational Electromagnetics (CEM)
Lecture #10
Finite‐Difference Method
These notes may contain copyrighted material obtained under fair use rules. Distribution of these materials is strictly prohibited
InstructorDr. Raymond Rumpf(915) 747‐[email protected]
Outline
• Finite‐Difference Approximations
• Finite‐Difference Method
• Numerical Boundary Conditions
• Matrix Operators
Lecture 10 Slide 2
7/20/2017
2
Lecture 10 Slide 3
Finite Difference Approximations
Lecture 10 Slide 4
The Basic Finite‐Difference Approximation
1.5 2 1df f f
dx x
1f2f
df
dx
x
second‐order accuratefirst‐order derivative
This is the only finite‐difference approximation we will use in this course!
7/20/2017
3
Lecture 10 Slide 5
Types of Finite‐Difference Approximations
Backward Finite‐Difference
1.5 2 1df f f
dx x
Central Finite‐Difference
1 2 1df f f
dx x
Forward Finite‐Difference
2 2 1df f f
dx x
Lecture 10 Slide 6
The Generalized Finite‐Difference
n
ni
ii
d fa
xf
d i i
i
f aL f
The derivative of any order of a function at any position can be approximated as a linear sum of known points of that function.
In fact, any linear operation on the function can be approximated as a linear sum of known points of that function.
7/20/2017
4
Lecture 10 Slide 7
Finite‐Difference “Atoms”
Any finite‐difference approximation can be summarized graphically as an “atom.”
1 1
2i i
i
f fdf x
dx
if 1if 1if
2
1 12 2
2i i ii
f f fdf x
dx
if 1if 1if
,i jf
1,i jf 1,i jf
, 1i jf
, 1i jf
1, , 1, , 1 , , 122 2
2 2i j i j i j i j i j i ji
f f f f f ff x
1
21
2
2
1
2
1
2
2
2
1
2
1
21
21
24
Lecture 10 Slide 8
Finite‐DifferenceMethod
7/20/2017
5
Lecture 10 Slide 9
Overview of Our FDM
1. Identify and write the governing equation(s).
2
2a x f x b x f x c x f x g x
x x
2. Write the matrix form of this equation going term‐by‐term.
3. Put matrix equation in the standard form of [L][f] = [g].
2
2a x f x b x f x c x f x g x
x x
2x xA D f B D f C f g
2 where x xL f g L A D B D C 4. Solve [L][f] = [g].
1f L g
L = A*D2X + y*B*DX + C;
Lecture 10 Slide 10
What’s the Catch?
2x xL A D B D C
How do we construct these matrices? What do they mean?
A
2xD
B xD
C
7/20/2017
6
Lecture 10 Slide 11
Functions Vs. Operations (1 of 2)
2
2f x f x f x ga b x x
xxx c
x
FunctionsThe only time functions appear in a differential equation is as the unknown or as the excitation.
unknown
excitation
f x
g x
OperationsEverything else in a differential equation is something that operates on a function.
2
2
, , point-by-point
multiplication on
, calculates derivatives of
scales entire
a x b x c x
f x
f xx x
f x
Lecture 10 Slide 12
Functions Vs. Operations (2 of 2)
2x xA D B D Cf f f g
FunctionsFunctions are stored as column vectors.
OperationsOperations are always stored in square matrices. Any linear operation can be put into matrix form.
1 1
2 2
M M
f g
f gf g
f g
11 12 1
21 22 2
1 2
M
M
M M MM
l l l
l l lL
l l l
7/20/2017
7
Lecture 10 Slide 13
Interpretation of Matrices
11 12 13 1a x a y a z b
21 22 23 2a x a y a z b
31 32 33 3a x a y a z b
11 12 13 1
21 22 23 2
31 32 33 3
a a a x b
a a a y b
a a a z b
11 12 13
21 22 23
31 32 33
a a a
a a a
a a a
Equation for x
Equation for y
Equation for z
EQUATION FOR… RELATION TO…
11 12 13
21 22 23
31 32 33
a a a
a a a
a a a
From a purely mathematical perspective, this interpretation does not make sense. This interpretation will be highly useful and insightful because of how we derive the equations.
Lecture 10 Slide 14
Representing Functions on a Grid
A grid is constructed by dividing space into discrete
cells
Example physical
(continuous) 2D function
Function is known only at discrete points
Representation of what is
actually stored in memory
7/20/2017
8
Lecture 10 Slide 15
Grid Cells
y
x
A function value is assigned to a specific point within the grid cell.
Whole Grid
A Single Grid Cell
, grid resolution parametersx y
Lecture 10 Slide 16
Functions are Put Into Column Vectors
2‐D Systems
1f 5f 9f 13f
2f 6f 10f 14f
3f 7f 11f 15f
4f 8f 12f 16f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
f
f
f
f
f
f
f
f
f
f
f
f
f
f
f
f
f
1
2
3
4
5
f
f
f
f
f
f
1‐D Systems
1f2f 3f 4f 5f
7/20/2017
9
Lecture 10 Slide 17
Putting Functions into Column Vectors
1f 5f 9f 13f
2f 6f 10f 14f
3f 7f 11f 15f
4f 8f 12f 16f
1f
2f
3f
4f
5f
6f
7f
8f
9f
10f
11f
12f
13f
14f
15f
16f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
f
f
f
f
f
f
f
f
f
f
f
f
f
f
f
f
f
MATLAB ‘reshape’ commandf = F(:);F = reshape(f,Nx,Ny);
Lecture 10 Slide 18
Locating Nodes in Column Vectors
1D Grids
Node located at nx m = nx
2D Grids
Node located at nx,ny m = (ny – 1)*Nx + nx
3D Grids
Node located at nx,ny,nz m = (nz – 1)*Nx*Ny + (ny – 1)*Nx + nx
7/20/2017
10
Lecture 10 Slide 19
The Finite‐Difference Method
df xa x f x g x
dx
1 1
2
f k f ka k f k g k
x
L f g
1f L g
Conventional FDM
Tedious, but not difficult.
Very difficult and tedious.
Easy.
xD f a f g
x
L f g
L D a
Improved
FDM
Almost effortless.
Very easy and clean
Easy.
Most time consuming step.
Differential Equation to Solve
Lecture 10 Slide 20
Conventional FDM (1 of 3)
Step 1 – We start with a differential equation that we wish to solve.2
2
d f dfa bf c
dx dx
Step 2 – We approximate the derivatives with finite differences.
2
1 2 1 1 1
2
f k f k f k f k f ka k b k f k c k
Step 3 – The equation is expanded and we collect common terms
2 2 2
2 2 2
1 2 11 1 1 1
2 2
1 2 11 1
2 2
a k a kf k f k f k f k f k b k f k c k
a k a kf k b k f k f k c k
IMPORTANT RULE: Every term in a finite‐difference equation must exist at the same point.
7/20/2017
11
Lecture 10 Slide 21
Conventional FDM (2 of 3)
Step 4 – The final equation is used to populate a matrix equation.
222
1 11
2 2
21b
a kkf k f k f k k
kc
a
1 1
2 2
3 3
4 4
5 5
1 1
f c
f c
f c
f c
f c
f N c N
f N c N
Filling in the matrix like this can be a very difficult and tedious task for more complicated differential equations or for systems of differential equations.
Lecture 10 Slide 22
Conventional FDM (3 of 3)
Step 5 – The matrix equation is solved for the unknown function f(x)
L f c
Lf c
1
1
f L c
f L c
7/20/2017
12
Lecture 10 Slide 23
Improved FDM
We want a very easy way to immediately write differential equations in matrix form. Starting with the same differential equation…
2
2
f fa bf c
x x
We will develop a procedure by which this will be directly written in matrix form without having to explicitly handle any finite‐differences.
2 1x x D f AD f Bf = c
2 1, , , and x xD A D Bare square matrices that perform linear operations on the vector f.
2
2f a f bf c
x x
2 1x x
L
L
D AD B f = c
1f L c
Lecture 10 Slide 24
Locating Nodes in Matrices
1D m or n = nx2D m or n = (ny–1)*Nx + nx3D m or n = (nz–1)*Nx*Ny + (ny–1)*Nx + nx
Row mEquation for node (nx,ny,nz)
Column n
Relatio
n to
node (n
x,ny,nz)
7/20/2017
13
Lecture 10 Slide 25
NumericalBoundary Conditions
2,1 1,1 1,2 1,11,12 2
3,1 2,1 1,1 2,2 2,
0,1 1,0
2,0
4,1
12,12 2
3,1 2,1 3,2 3,13,12 2
2,2 1,2 1,3 1,2 1,11,22 2
3,2 2,2 1
3,0
0,2
,2 2,3 2,2 2
2
2 2
2 2
2 2
2 2
2 2
f f f fg
x y
f f f f fg
x y
f f f fg
x
f f
y
f f f f fg
x y
f f f f f
f
f
f
x
f
f
,1
2,22
3,2 2,2 3,3 3,2 3,13,22 2
2,3 1,3 1,3 1,21,32 2
3,3 2,3 1,3 2,3 2,22,32 2
3,3 2,3 3,3 3,23,32
4,2
0,3 1,4
2,4
4,3 3
2
,4
2 2
2 2
2 2
2 2
gy
f f f f fg
x y
f f f fg
x y
f f f f fg
x y
f f f fg
x
f
y
f f
f
f f
0,1f
0,2f
0,3f
4,1f
4,2f
4,3f
1,0f 2,0f 3,0f
1,4f 2,4f 3,4f
Lecture 10 Slide 26
The Problem at the Boundaries
Suppose it is desired to solve the following differential equation on a 33 grid.
1, , 1, , 1 , , 1,2 2
2 2 i j i j i j i j i j i j
i j
f f f f f fg
x y
The terms in red exist outside of the grid.
How this is handled is called a boundary condition.
2 2
2 2
, ,,
f x y f x yg x y
x y
7/20/2017
14
Lecture 10 Slide 27
Dirichlet Boundary Conditions (1 of 2)
The simplest boundary condition is to assume that all values of f(x,y)outside of the grid are zero.
2,1 1,1 1,2 1,11,12 2
3,1 2,1 1,1 2,2 2,12,12 2
3,1 2,1 3,2 3,13,12 2
2,2 1,2 1,3 1,2 1,11,22 2
3,2 2,2 1,2 2,3 2,2 2,12,22 2
3,2
02 2
2 2
2 2
2 2
2 2
2
0
0
0 0
0
0
f f f fg
x y
f f f f fg
x y
f f f fg
x y
f f f f fg
x y
f f f f f fg
x y
f
2,2 3,3 3,2 3,1
3,22 2
2,3 1,3 1,3 1,21,32 2
3,3 2,3 1,3 2,3 2,22,32 2
3,3 2,3 3,3 3,23,32 2
0 0
0
0 0
2
2 2
2 2
2 2
f f f fg
x y
f f f fg
x y
f f f f fg
x y
f f f fg
x y
0,1f
0,2f
0,3f
4,1f
4,2f
4,3f
1,0f 2,0f 3,0f
1,4f 2,4f 3,4f
Lecture 10 Slide 28
Dirichlet Boundary Conditions (2 of 2)
2
1
2
1
1
1
, , 0
2
, 0 ,
2
, , 0
2
, 0 ,
2
x
y
x
y
i i
x
i N i
x
i i
y
i i N
y
N
N
f y f x y
x
f y f x y
x
x
x
yf x f x y
y
f yx f x y
y
Dirichlet boundary conditions assume function values from outside of the grid are zero.
22 1
2 2
21
2 2
22 1
21
2
21
2 2
1, , 2 , 0
, 0 2 , ,
, , 2 , 0
, 0 2 , ,
x x
y y
x
y
i i i
x
i N i N i
x
i i i
y
i i N i
N
N
y
N
f y f x y f x y
x
f y f x y f x y
x
f x f x y f x y
y
f x f x y f x y
y
x
x
y
y
7/20/2017
15
Lecture 10 Slide 29
Periodic Boundary Conditions (1 of 2)
If the function f(x,y) is periodic, then the values from outside of the grid can be mapped to a value from inside the grid at the other side.
3, 1, ,3 ,10, 4, ,0 ,4, , , and j j i ij j i if f ff f f ff
2,1 1,1 1,2 1,11,12 2
3,1 2,1 1,1 2,2 2,
3,1 1,3
2,3
1,1
12,12 2
3,1 2,1 3,2 3,13,12 2
2,2 1,2 1,3 1,2 1,11,22 2
3,2 2,2 1
3,3
3,2
,2 2,3 2,2 2
2
2 2
2 2
2 2
2 2
2 2
f f f fg
x y
f f f f fg
x y
f f f fg
x
f f
y
f f f f fg
x y
f f f f f
f
f
f
x
f
f
,1
2,22
3,2 2,2 3,3 3,2 3,13,22 2
2,3 1,3 1,3 1,21,32 2
3,3 2,3 1,3 2,3 2,22,32 2
3,3 2,3 3,3 3,23,32
1,2
3,3 1,1
2,1
1,3 3
2
,1
2 2
2 2
2 2
2 2
gy
f f f f fg
x y
f f f fg
x y
f f f f fg
x y
f f f fg
x
f
y
f f
f
f f
0,1f
0,2f
0,3f
4,1f
4,2f
4,3f
1,0f 2,0f 3,0f
1,4f 2,4f 3,4f
Lecture 10 Slide 30
Periodic Boundary Conditions (2 of 2)
1
1
1
1
2
1
2
1
, ,,
2
, , ,
2
, ,,
2
, , ,
2
y
x
y
x
x
y
N
N
i ii
x
i i N i
x
i ii
y
i i i
N
N N
y
f x y f yf y
x
f y f y f x y
x
f x y f xf x
y
f x
xx
x x
yy
f x f xy y y
y
Periodic boundary conditions assume function values from outside of the grid can be taken from the opposite side of the grid.
22 1
2 2
21
2 2
22 1
2 2
21
2
1
1
1
1
2
, 2 , ,,
, , 2 , ,
, 2 , ,,
, , 2 , ,
x x
y y
x
x
y
y
Ni i ii
x
i i N i N i
x
i i ii
y
i i i N i N
y
N
N
N
f x y f x y f yf y
x
f y f y f x y f x y
x
f x y f x y f xf x
y
f x f x f x
xx
x x
y f x y
y
yy
y y
7/20/2017
16
Lecture 10 Slide 31
Neumann Boundary Conditions
We use Neumann boundary conditions for non‐periodic functions or functions that are not zero at the boundary. Here the function continues linearly outside of the grid.
Spatially variant grating that is not periodic and not zero at the boundaries.
Here is a 1D function with Neumann boundaries
Lecture 10 Slide 32
Neumann BC’s for 1D Function
The finite‐difference approximation for a 1D function is
1 1
2i i
i x
f fdf
dx
At i=1, we have a problem…
2 0
1 2i x
f fdf
dx
This term doesn’t exist!
2 1
1i x
f fdf
dx
At i=Nx, we have another problem…
1 1
2x x
x
N N
i N x
f fdf
dx
This term doesn’t exist! 1x x
x
N N
i N x
f fdf
dx
7/20/2017
17
Lecture 10 Slide 33
What About the 2nd‐Order Derivatives for the Neumann Boundary Condition?
In order for the function to continue in a straight line, the second‐order derivate should be set to zero at the boundary.
IMPORTANT: This is NOT Dirichlet boundary conditions. A Dirichlet BC sets the function itself to zero outside of the grid, not the derivative. Here, the 2nd‐order derivative is set to zero.
2 22 1 0
2 2 2
1 1
0xi i
f f fd f d f
dx dx
2 21 1
2 2 2 0x x x
x x
N N N
xi N i N
f f fd f d f
dx dx
Lecture 10 Slide 34
Neumann Boundary Conditions for 2D Functions
1 2 1
1
1 2 1
1
, , ,
, , ,
, , ,
, , ,
x x x
y y y
N N
i i i
x
i i i
x
i i i
y
N
Ni Ni
y
N i
f y f y f y
x
f y f y f y
x
f x f x f x
y
f x
x x x
x x x
y y y
y f x f x
y
y y
Neumann boundary conditions are used when a function should be continuous at the boundary. That is, the first‐order derivative is continuous and the second‐order derivative is zero.
2
2
2
2
2
2
2
2
1
1
,0
,0
,0
,0
x
y
i
i
i
N
Ni
f y
x
f y
x
f x
y
x
x
yf
y
x
y
7/20/2017
18
Lecture 10 Slide 35
Matrix Operators
Lecture 10 Slide 36
Origin of Matrix Operators
We start with a governing equation.
2
2
,,
d f x yg x y
dx
We construct a grid to store the functions.
2,1 1,11,12
3,1 2,1 1,12,12
3,1 2,13,12
2,2 1,21,22
3,2 2,2 1,22,22
3,2 2,23,22
2,3 1,31,32
3,3 2,3 1,32,32
3,3 2,33,32
2
2
2
2
2
2
2
2
2
0
0
0
0
0
0
f fg
xf f f
gxf f
gx
f fg
xf f f
gxf f
gx
f fg
xf f f
gxf f
gx
We approximate the governing equation with finite‐differences and then write the finite‐difference equation at each point the grid.
1,1 1,1
2,1 2,1
3,1 3,1
1,2
2,22
3,2
1,3
2,3
3,3
2 1 0 0 0 0 0 0 0
1 2 1 0 0 0 0 0 0
0 1 2 0 0 0 0 0
0 0 2 1 0 0 0 01
0 0 0 1 2 1 0 0 0
0 0 0 0 1 2 0 0
0 0 0 0 0 2 1 0
0 0 0 0 0 0 1 2 1
0 0 0 0 0 0 0 1
0
0
0
2
0
f g
f g
f g
f
fx
f
f
f
f
1,2
2,2
3,2
1,3
2,3
3,3
g
g
g
g
g
g
We collect the large set of equations into a single matrix equation.
This matrix calculates the derivative of f(x,y)and puts the answer in g(x,y). This is a matrix operator.
2xD
7/20/2017
19
Lecture 10 Slide 37
Other Matrix Operators
A square matrix can always be constructed to perform any linear operation on a function that is stored in a column vector.
1
2
3 ?
x
x
N
f
fd
f x fdx
f
D
D f
FFT f x Ff
f x dx f
g x f x Gf
h x f x Hf
Lecture 10 Slide 38
Point‐by‐Point Multiplication (1 of 2)
Since we are storing our “functions” in vector form, how do we perform a point‐by‐point multiplication using a square matrix?
f xb x Bf
1x 2x 3x 4x 5x 6x
1f
2f3f 4f 5f 6f
1b2b 3b 4b 5b 6b
1 11 1
2 2
3 3
4 4
2 2
3 3
4
5 5
4
5 5
6 66 6
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
f f
f f
f
b b
b b
b b
b b
f
f f
f f
f f
b b
b b
f fB B
?
7/20/2017
20
Lecture 10 Slide 39
Point‐by‐Point Multiplication (2 of 2)
Since we are storing our “functions” in vector form, how do we perform a point‐by‐point multiplication using a square matrix?
f xb x Bf
1x 2x 3x 4x 5x 6x
1f
2f3f 4f 5f 6f
1b2b 3b 4b 5b 6b
1 11 1
2 2
3 3
4 4
2 2
3 3
4
5 5
4
5 5
6 66 6
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
f f
f f
f
b b
b b
b b
b b
f
f f
f f
f f
b b
b b
f fB B
Lecture 10 Slide 40
First‐Order Partial Derivative (1 of 2)
How do we construct a square matrix so that when it premultiplies a vector, we get a vector containing the first‐order partial derivative?
1 xf xx
D f
1x 2x 3x 4x 5x 6x
1f
2f3f 4f 5f 6f
1 1
2 0
3 1
4
1
2
23
4
6
7
5
6
5 3
4
5
2
2
2
2
2
0 1 0 0 0 0
1 0 1 0 0 0
0 1 0 1 0 01
0 0 1 0 1 02
0 0 0 1 0 1
0 0 0 0 1 0 2
x
x
x
x
x
x
x
x
x
f f
f f
f f
f f
f
f
f
f
f
f f
f ff
D fDf
?
7/20/2017
21
Lecture 10 Slide 41
First‐Order Partial Derivative (2 of 2)
How do we construct a square matrix so that when it premultiplies a vector, we get a vector containing the first‐order partial derivative?
1 xf xx
D f
1x 2x 3x 4x 5x 6x
1f
2f3f 4f 5f 6f
1 1
2 0
3 1
4
1
2
23
4
6
7
5
6
5 3
4
5
2
2
2
2
2
0 1 0 0 0 0
1 0 1 0 0 0
0 1 0 1 0 01
0 0 1 0 1 02
0 0 0 1 0 1
0 0 0 0 1 0 2
x
x
x
x
x
x
x
x
x
f f
f f
f f
f f
f
f
f
f
f
f f
f ff
D fDf
Lecture 10 Slide 42
Second‐Order Partial Derivative (1 of 2)
How do we construct a square matrix so that when it premultiplies a vector, we get a vector containing the second‐order partial derivative?
2
2
2 xx
f x
D f
1x 2x 3x 4x 5x 6x
1f
2f3f 4f 5f 6f
2
22 1 0
23 2 1
24 3 2
25 4 3
26 5 4
27 6
1
2
3
4
5
2
6 5
2 1 0 0 0 0
1 2 1 0 0 0
0 1 2 1 0 01
0 0 1 2 1 0
0 0 0 1 2 1
0 0 0 0
2
2
2
2
2
2 21
x
x
x
xx
x
x
x
f f f
f f f
f f f
f f f
f f f
f
f
f
f
f
f
f f f
fD
2x
fD
?
7/20/2017
22
Lecture 10 Slide 43
Second‐Order Partial Derivative (2 of 2)
How do we construct a square matrix so that when it premultiplies a vector, we get a vector containing the second‐order partial derivative?
2
2
2 xx
f x
D f
1x 2x 3x 4x 5x 6x
1f
2f3f 4f 5f 6f
2
22 1 0
23 2 1
24 3 2
25 4 3
26 5 4
27 6
1
2
3
4
5
2
6 5
2 1 0 0 0 0
1 2 1 0 0 0
0 1 2 1 0 01
0 0 1 2 1 0
0 0 0 1 2 1
0 0 0 0
2
2
2
2
2
2 21
x
x
x
xx
x
x
x
f f f
f f f
f f f
f f f
f f f
f
f
f
f
f
f
f f f
fD
2x
fD
Lecture 10 Slide 44
What About 2D Grids (1 of 2)?
Two‐dimensional grids are a little more difficult
2
2
2 , xx
f x y
D f
1f 2f 3f
4f 5f 6f
7f 8f 9f
x
y
2
22 1 ???
23 2 1
2??? 3 2
25 4 ?
1
??
6 52
2
3
4
5
6
7
8
9
2
2
0 2
2 1
1
0 2
2 1
1 2
2
2
11
1 2 1
1 2
2 1
1
0
0
1 2
1 2
x
x
x
x
x
x
f
f
f
f
f
f
f f f
f f f
f f f
f f
f
f
f f
f
f
f
fD
2
24
2??? 6 5
28 7 ???
29 8 7
2??? 9 8
2
2
2
2
x
x
x
x
x
x
f f f
f f f
f f f
f f f
fD
7/20/2017
23
Lecture 10 Slide 45
What About 2D Grids (2 of 2)?
Two‐dimensional grids are a little more difficult
2
2
2 , yy
f x y
D f
2
24 1 ???
25
1
2
3
4
2 ???
6
7
2
8
9
3
5
6
2 1
2 1
2 1
1 2 11
1 2 1
1 2 1
1 2
1 2
1
20 0
20 0 0
20 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0
0 0 2
y
y
y
y
f
f
f
f
f
f
f
f
f
f f f
f f f
f f f
D f
2
2???
27 4 1
28 5 2
29 6 3
2??? 7 4
2??? 8 5
2??? 9 6
2
2
2
2
2
2
y
y
y
y
y
y
y
y
f f f
f f f
f f f
f f f
f f f
f f f
fD
1f 2f 3f
4f 5f 6f
7f 8f 9f
x
y
Lecture 10 Slide 46
Derivative Operators with Dirichlet Boundary Conditions
2
2
0 0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 01
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0 0
2 1
1 2 1
1 2
2 1
1 2 1
1 2
2
0
0
0
1
1 2 1
1
0
2
xx
D
2
2
2 1
2 1
2 1
1 2 1
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 01
0 0 0 0 0 0
0 0 0 0 0 0
1 2 1
1 2 1
1 2
1 2
1
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 20 0 0
yy
D
Both of these matrices only have numbers along three of their diagonals.
This is called tridiagonal.
This suggests a fast way to construct these matrices.
Dx(2) has some corrections shown in
blue along two of its diagonals.
These matrices contain mostly zeros.
These are called a sparse matrices.
See MATLAB sparse() command.
Also see MATLAB spdiags() command.
7/20/2017
24
Lecture 10 Slide 47
Why Do We Need Separate Matrix Operators for First‐ and Second‐Order Derivatives?
We know that,2 2
2 2
x x x y y y
Can we just calculate D(2) from D(1)?
? ?2 1 1 2 1 1 x x x y y y D D D D D D
Yes, but this does not make efficient use of the grid. For a 5‐point, 1D grid, we have
1 1 2
2 2
1 0 1 0 0 2 1 0 0 0
0 2 0 1 0 1 2 1 0 01 1
1 0 2 0 1 0 1 2 1 02
0 1 0 2 0 0 0 1 2 1
0 0 1 0 1 0 0 0 1 2
x x xxx
D D D
This is not as accurate because it calculates the derivative with poorer grid resolution than is available.
This matrix operator makes optimal use of the available grid resolution.
Lecture 10 Slide 48
2D Derivative Operators for 1D Grids
When Nx=1 and Ny>10 0
0
0 0
x
D
When Nx>1 and Ny=1
is standard for 1D grid
zero matrixx
y
D
D Z
zero matrix
is standard for 1D gridx
y
D Z
D
0 0
0
0 0
y
D
7/20/2017
25
Lecture 10 Slide 49
USE SPARSE MATRICES!!!!!!!
WARNING !!The derivative operators will be EXTREMELY large matrices.
For a small grid that is just 100200 points:
Total Number of Points: 20,000Size of Derivate Operators: 20,000 20,000Total Elements in Matrices: 400,000,000Memory to Store One Full Matrix: 6 GbMemory to Store One Sparse Matrix: 1 Mb
NEVER AT ANY POINT should you use FULL MATRICES in the
finite‐difference method. Not even for intermediate steps. NEVER!