the finite‐difference method · differential equation to solve conventional fdm (1 of 3) slide 20...

25
8/24/2019 1 Advanced Computation: Computational Electromagnetics The Finite‐Difference Method Outline Finite‐Difference Approximations Finite‐Difference Method Numerical Boundary Conditions Matrix Operators Slide 2 1 2

Upload: others

Post on 11-Oct-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Finite‐Difference Method · Differential Equation to Solve Conventional FDM (1 of 3) Slide 20 Step 1 –Start with a differential equation to be solved. 2 2 df df abfc dx dx

8/24/2019

1

Advanced Computation:

Computational Electromagnetics

The Finite‐Difference Method

Outline

• Finite‐Difference Approximations

• Finite‐Difference Method

• Numerical Boundary Conditions

• Matrix Operators

Slide 2

1

2

Page 2: The Finite‐Difference Method · Differential Equation to Solve Conventional FDM (1 of 3) Slide 20 Step 1 –Start with a differential equation to be solved. 2 2 df df abfc dx dx

8/24/2019

2

Slide 3

Finite Difference Approximations

The Basic Finite‐Difference Approximation

Slide 4

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!

3

4

Page 3: The Finite‐Difference Method · Differential Equation to Solve Conventional FDM (1 of 3) Slide 20 Step 1 –Start with a differential equation to be solved. 2 2 df df abfc dx dx

8/24/2019

3

Types of Finite‐Difference Approximations

Slide 5

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

The Generalized Finite‐Difference

Slide 6

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. 

5

6

Page 4: The Finite‐Difference Method · Differential Equation to Solve Conventional FDM (1 of 3) Slide 20 Step 1 –Start with a differential equation to be solved. 2 2 df df abfc dx dx

8/24/2019

4

Finite‐Difference “Atoms”

Slide 7

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

Slide 8

Finite‐Difference Method

7

8

Page 5: The Finite‐Difference Method · Differential Equation to Solve Conventional FDM (1 of 3) Slide 20 Step 1 –Start with a differential equation to be solved. 2 2 df df abfc dx dx

8/24/2019

5

Overview of Our Approach to FDM

Slide 9

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;

What’s the Catch?

Slide 10

2x xL A D B D C

How are these matrices constructed?  What is their meaning?

A

2xD

B xD

C

9

10

Page 6: The Finite‐Difference Method · Differential Equation to Solve Conventional FDM (1 of 3) Slide 20 Step 1 –Start with a differential equation to be solved. 2 2 df df abfc dx dx

8/24/2019

6

Functions Vs. Operations (1 of 2)

Slide 11

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

Functions Vs. Operations (2 of 2)

Slide 12

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

11

12

Page 7: The Finite‐Difference Method · Differential Equation to Solve Conventional FDM (1 of 3) Slide 20 Step 1 –Start with a differential equation to be solved. 2 2 df df abfc dx dx

8/24/2019

7

Interpretation of Matrices

Slide 13

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.

Representing Functions on a Grid

Slide 14

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

13

14

Page 8: The Finite‐Difference Method · Differential Equation to Solve Conventional FDM (1 of 3) Slide 20 Step 1 –Start with a differential equation to be solved. 2 2 df df abfc dx dx

8/24/2019

8

Grid Cells

Slide 15

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

Functions are Put Into Column Vectors

Slide 16

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

15

16

Page 9: The Finite‐Difference Method · Differential Equation to Solve Conventional FDM (1 of 3) Slide 20 Step 1 –Start with a differential equation to be solved. 2 2 df df abfc dx dx

8/24/2019

9

Putting Functions into Column Vectors

Slide 17

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);

Locating Points in Column Vectors

Slide 18

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

17

18

Page 10: The Finite‐Difference Method · Differential Equation to Solve Conventional FDM (1 of 3) Slide 20 Step 1 –Start with a differential equation to be solved. 2 2 df df abfc dx dx

8/24/2019

10

The Finite‐Difference Method

Slide 19

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

Conventional FDM (1 of 3)

Slide 20

Step 1 – Start with a differential equation to be solved.2

2

d f dfa bf c

dx dx

Step 2 – 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 – Expand equation and 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.

19

20

Page 11: The Finite‐Difference Method · Differential Equation to Solve Conventional FDM (1 of 3) Slide 20 Step 1 –Start with a differential equation to be solved. 2 2 df df abfc dx dx

8/24/2019

11

Conventional FDM (2 of 3)

Slide 21

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.

Conventional FDM (3 of 3)

Slide 22

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

21

22

Page 12: The Finite‐Difference Method · Differential Equation to Solve Conventional FDM (1 of 3) Slide 20 Step 1 –Start with a differential equation to be solved. 2 2 df df abfc dx dx

8/24/2019

12

Improved FDM

Slide 23

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

Locating Points in Matrices

Slide 24

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)

23

24

Page 13: The Finite‐Difference Method · Differential Equation to Solve Conventional FDM (1 of 3) Slide 20 Step 1 –Start with a differential equation to be solved. 2 2 df df abfc dx dx

8/24/2019

13

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

The Problem at the Boundaries

Slide 26

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

25

26

Page 14: The Finite‐Difference Method · Differential Equation to Solve Conventional FDM (1 of 3) Slide 20 Step 1 –Start with a differential equation to be solved. 2 2 df df abfc dx dx

8/24/2019

14

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

Dirichlet Boundary Conditions (1 of 2)

Slide 27

The simplest boundary condition is to assume that all values of 𝑓 𝑥, 𝑦 outside of the grid are zero.

Dirichlet Boundary Conditions (2 of 2)

Slide 28

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

27

28

Page 15: The Finite‐Difference Method · Differential Equation to Solve Conventional FDM (1 of 3) Slide 20 Step 1 –Start with a differential equation to be solved. 2 2 df df abfc dx dx

8/24/2019

15

Periodic Boundary Conditions (1 of 2)

Slide 29

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

Periodic Boundary Conditions (2 of 2)

Slide 30

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

29

30

Page 16: The Finite‐Difference Method · Differential Equation to Solve Conventional FDM (1 of 3) Slide 20 Step 1 –Start with a differential equation to be solved. 2 2 df df abfc dx dx

8/24/2019

16

Neumann Boundary Conditions

Slide 31

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

Neumann BC’s for 1D Function

Slide 32

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

31

32

Page 17: The Finite‐Difference Method · Differential Equation to Solve Conventional FDM (1 of 3) Slide 20 Step 1 –Start with a differential equation to be solved. 2 2 df df abfc dx dx

8/24/2019

17

What About the 2nd‐Order Derivatives for the Neumann Boundary Condition?

Slide 33

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

Neumann Boundary Conditions for 2D Functions

Slide 34

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

33

34

Page 18: The Finite‐Difference Method · Differential Equation to Solve Conventional FDM (1 of 3) Slide 20 Step 1 –Start with a differential equation to be solved. 2 2 df df abfc dx dx

8/24/2019

18

Slide 35

Matrix Operators

Origin of Matrix Operators

Slide 36

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

35

36

Page 19: The Finite‐Difference Method · Differential Equation to Solve Conventional FDM (1 of 3) Slide 20 Step 1 –Start with a differential equation to be solved. 2 2 df df abfc dx dx

8/24/2019

19

Other Matrix Operators

Slide 37

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

Point‐by‐Point Multiplication (1 of 2)

Slide 38

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

?

37

38

Page 20: The Finite‐Difference Method · Differential Equation to Solve Conventional FDM (1 of 3) Slide 20 Step 1 –Start with a differential equation to be solved. 2 2 df df abfc dx dx

8/24/2019

20

Point‐by‐Point Multiplication (2 of 2)

Slide 39

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

First‐Order Partial Derivative (1 of 2)

Slide 40

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

?

39

40

Page 21: The Finite‐Difference Method · Differential Equation to Solve Conventional FDM (1 of 3) Slide 20 Step 1 –Start with a differential equation to be solved. 2 2 df df abfc dx dx

8/24/2019

21

First‐Order Partial Derivative (2 of 2)

Slide 41

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

How do we construct a square matrix so that when it premultiplies a vector, we get a vector containing the first‐order partial derivative?

Second‐Order Partial Derivative (1 of 2)

Slide 42

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

?

41

42

Page 22: The Finite‐Difference Method · Differential Equation to Solve Conventional FDM (1 of 3) Slide 20 Step 1 –Start with a differential equation to be solved. 2 2 df df abfc dx dx

8/24/2019

22

Second‐Order Partial Derivative (2 of 2)

Slide 43

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

How do we construct a square matrix so that when it premultiplies a vector, we get a vector containing the second‐order partial derivative?

What About 2D Grids (1 of 2)?

Slide 44

Two‐dimensional grids are a little more difficult

2

2

2 , xx

f x y

D f1f 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

43

44

Page 23: The Finite‐Difference Method · Differential Equation to Solve Conventional FDM (1 of 3) Slide 20 Step 1 –Start with a differential equation to be solved. 2 2 df df abfc dx dx

8/24/2019

23

What About 2D Grids (2 of 2)?

Slide 45

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

Two‐dimensional grids are a little more difficult

2

2

2 , xx

f x y

D f1f 2f 3f

4f 5f 6f

7f 8f 9f

x

y

Derivative Operators with Dirichlet Boundary Conditions

Slide 46

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.

45

46

Page 24: The Finite‐Difference Method · Differential Equation to Solve Conventional FDM (1 of 3) Slide 20 Step 1 –Start with a differential equation to be solved. 2 2 df df abfc dx dx

8/24/2019

24

Why Do We Need Separate Matrix Operators for First‐ and Second‐Order Derivatives?

Slide 47

It is known that2 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

1 0 1 0 0

0 2 0 1 01

1 0 2 0 12

0 1 0 2 0

0 0 1 0 1

x x

x

D D

This is not as accurate because it calculates the derivative with poorer grid resolution than is available.

2

2

2 1 0 0 0

1 2 1 0 01

0 1 2 1 0

0 0 1 2 1

0 0 0 1 2

xx

D

This matrix operator makes optimal use of the available grid resolution.

2D Derivative Operators for 1D Grids

Slide 48

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

47

48

Page 25: The Finite‐Difference Method · Differential Equation to Solve Conventional FDM (1 of 3) Slide 20 Step 1 –Start with a differential equation to be solved. 2 2 df df abfc dx dx

8/24/2019

25

USE SPARSE MATRICES!!!!!!!

Slide 49

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!

49