one‐dimensional finite‐difference method · the finite‐difference method slide 4 the...
TRANSCRIPT
-
9/22/2019
1
Computational Science:Computational Methods in Engineering
One‐DimensionalFinite‐Difference Method
Outline
• Introduction & Problem Setup•Conventional Finite‐Difference Method• Improved Finite‐Difference Method•Matrix Operators• Incorporating Boundary Conditions• Solving ODE’s
2
1
2
-
9/22/2019
2
Slide 3
Introduction & Problem Setup
The Finite‐Difference Method
Slide 4
The finite‐difference method is a way of obtaining a numerical solution to differential equations. It does not give a symbolic solution.
2
2 0 0 10
0 1, 10 5
d y dy y xdx dxy y
Governing Equation
A y bMatrix Equation
1y A bNumerical Solution
3
4
-
9/22/2019
3
Functions are Discrete
Slide 5
To obtain a numerical solution using the finite‐difference method, functions are stored as arrays of discrete points.
The function can be resolved more accurately using more points, but the solution will be more computationally intensive to obtain. This is a fundamental tradeoff.
Discrete Functions are Stored as Column Vectors
Slide 6
Discrete functions are stored as a 1D array of numbers in a column vector.
2
2
2
2
0
1 0
d y dy ydx dx
d d ydx dx
0A y y
5
6
-
9/22/2019
4
Slide 7
ConventionalFinite‐Difference Method
Step 1 – Identify Governing Equation & Boundary Values
Slide 8
2
2 0 0 10d y dy y xdx dx
Governing Equation
0 110 5y
y
Boundary Values
7
8
-
9/22/2019
5
0y x
Step 2 – Approximate Derivatives with Finite‐Differences (1 of 3)
Slide 9
2
2 0dydx
ddx
yy
First, let the function be discrete.
This allows us to approximate the derivatives with finite‐differences.
2
2y x x y x y x xx
y x y x xx
Step 2 – Approximate Derivatives with Finite‐Differences (2 of 3)
Slide 10
22
0y x x y x y x x y x y x x
y xx x
It is critical to ensure that each term in the finite‐difference equation exists at the same point.
Exists at x Exists at x - x/2
Exists at x
Exists at x
This is not a healthy or stable formulation because not all of the terms exist at the same point.
9
10
-
9/22/2019
6
Step 2 – Approximate Derivatives with Finite‐Differences (3 of 3)
Slide 11
2
2
2
0
20
2
d y dy ydx dx
y x x y x y x x y x x y x xy x
x x
This is the correct finite‐difference equation.
All terms exist at x.
Step 3 – Write Finite‐Difference Equation Using Array Indices
Slide 12
2
2
2
1 1 1 12
0
20
2
2 02
i i i i ii
d y dy ydx dx
y x x y x y x x y x x y x xy x
x x
y y y y y yx x
11
12
-
9/22/2019
7
Step 4 – Rearrange Finite‐Difference Equation
Slide 13
1 1 1 12
1 1 1 12 2 2
1 12 2 2
2 02
1 2 1 1 1 02 2
1 1 2 1 11 02 2
i i i i ii
i i i i i i
i i i
y y y y y yx x
y y y y y yx x x x x
y y yx x x x x
The finite‐difference equation is rearranged so as to collect the y terms.
Step 5 – Setup Grid
Slide 14
Let’s solve this problem using 21 points.
x1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
x=
0
x=
10
The grid spacing x is then
10 0 0.51 21 1
b ax xxN
x
13
14
-
9/22/2019
8
Step 6 – Revise Finite‐Difference Equation
Slide 15
1 12 2 2
1 12 2 2
1 1
1 1 2 1 11 02 2
1 1 2 1 11 00.5 2 0.5 0.5 0.5 2 0.5
5 7 3 0
i i i
i i i
i i i
y y yx x x x x
y y y
y y y
Substituting x = 0.5 into our finite‐difference equation gives
Step 7 – Write Finite‐Difference Equation at Each Point on Grid
Slide 16
x1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
1 15 7 3 0i i iy y y
These terms exist outside of the grid.
15
16
-
9/22/2019
9
y 1=
1
y 21
= 5
Step 8 – Incorporate Boundary Values
Slide 17
x1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
1 15 7 3 0i i iy y y
Step 8 – Write Set of Equations as a Single Matrix Equation
Slide 18
A y b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
00000000
0
1
000
yyyyyyyyyyyyyyyyyyyyy
5
0000000
17
18
-
9/22/2019
10
Step 9 – Solve Matrix Equation
Slide 19
11
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
10000000000
yyyyyyyyyyyyyyyyyyyyy
0000000005
1.00002.04973.11603.85453.80052.4436-0.6324-5.5482-11.8919-18.5007-23.3484-23.6453-16.25821.473030.533968.7909109.6222141.1336146.6082106.86305.0000
Step 10 – Plot the Result
Slide 20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
yyyyyyyyyyyyyyyyyyyyy
1.00002.04973.11603.85453.80052.4436-0.6324-5.5482-11.8919-18.5007-23.3484-23.6453-16.25821.473030.533968.7909109.6222141.1336146.6082106.86305.0000
19
20
-
9/22/2019
11
Slide 21
ImprovedFinite‐Difference Method
Step 1 – Identify Governing Equation & Boundary Values
Slide 22
2
2 0 0 10d y dy y xdx dx
Governing Equation
0 110 5y
y
Boundary Values
21
22
-
9/22/2019
12
Step 2 – Write Equation in Matrix Form Going Term‐by‐Term
Slide 23
2
2 0d dy x y x y xdx dx
2D D 0x xy y y
Step 3 – Factor Out [y] To Put in Standard Form
Slide 24
2D D 0x x I y
2D D 0x xy y y
2
0 standard form
D Dx x
A y
A I
A = DX2 - DX + I;
23
24
-
9/22/2019
13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
0000000000000
yyyyyyyyyyyyyyyyyyyyy
00000000
Step 4 – Incorporate Boundary Values (1 of 4)
Slide 25
Start with direct matrix equation.
0A y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
0000000000000
yyyyyyyyyyyyyyyyyyyyy
00000000
Step 4 – Incorporate Boundary Values (2 of 4)
Slide 26
Zero out rows in [A]that correspond to the boundary points. For this case, these are the rows for y1 and y21.
25
26
-
9/22/2019
14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
0000000000000
yyyyyyyyyyyyyyyyyyyyy
00000000
Step 4 – Incorporate Boundary Values (3 of 4)
Slide 27
Insert 1’s in the diagonal position of the rows in [A]where zeros were inserted.
Step 4 – Incorporate Boundary Values (4 of 4)
Slide 28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
00000000
0
1
000
yyyyyyyyyyyyyyyyyyyyy
5
0000000
Insert boundary values in column vector [b].
27
28
-
9/22/2019
15
Step 5 – Solve Matrix Equation
Slide 29
11
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
10000000000
yyyyyyyyyyyyyyyyyyyyy
0000000005
1.00002.04973.11603.85453.80052.4436-0.6324-5.5482-11.8919-18.5007-23.3484-23.6453-16.25821.473030.533968.7909109.6222141.1336146.6082106.86305.0000
Step 6 – Plot the Result
Slide 30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
yyyyyyyyyyyyyyyyyyyyy
1.00002.04973.11603.85453.80052.4436-0.6324-5.5482-11.8919-18.5007-23.3484-23.6453-16.25821.473030.533968.7909109.6222141.1336146.6082106.86305.0000
29
30
-
9/22/2019
16
Slide 31
Matrix Operators
Functions Vs. Operations (1 of 2)
Slide 32
2
2 f x f x f x ga b x xxxx c
x
FunctionsFunctions only appear in a differential equation as the unknown or as the excitation.
unknownexcitation
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
31
32
-
9/22/2019
17
Functions Vs. Operations (2 of 2)
Slide 33
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 gf g
f g
f g
11 12 1
21 22 2
1 2
M
M
M M MM
l l ll l l
L
l l l
Point‐by‐Point Multiplication (1 of 2)
Slide 34
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 B f1x 2x 3x 4x 5x 6x
1f2f
3f 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 00 0 0 0 00 0 0 0 00 0 0 0 00 0 0 0 00 0 0 0 0
f ff ff
b bb b
b bb b
ff ff ff f
b bb b
f fB B
?
33
34
-
9/22/2019
18
Point‐by‐Point Multiplication (2 of 2)
Slide 35
1x 2x 3x 4x 5x 6x
1f2f
3f 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 00 0 0 0 00 0 0 0 00 0 0 0 00 0 0 0 00 0 0 0 0
f ff ff
b bb b
b bb b
ff ff ff f
b bb b
f fB B
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 B f
First‐Order Partial Derivative (1 of 2)
Slide 36
How can a square matrix be constructed so that when it premultiplies a vector it gives a vector containing the first‐order partial derivative?
xf Dxx f
1x 2x 3x 4x 5x 6x
1f2f
3f 4f 5f 6f
1 2 0
3 1
4
2
3
4
5
2
5 3
6 4
76 5
22222
0 1 0 0 0 01 0 1 0 0 0
0 1 0 1 0 010 0 1 0 1 020 0 0 1 0
21
0 0 0 0 1 0x x
x
D
x
x
x
x
x
f
x
D f
f ff ff ff
fffff
ff f
f f f
?
35
36
-
9/22/2019
19
First‐Order Partial Derivative (2 of 2)
Slide 37
1x 2x 3x 4x 5x 6x
1f2f
3f 4f 5f 6f
1 2 0
3 1
4
2
3
4
5
2
5 3
6 4
76 5
22222
0 1 0 0 0 01 0 1 0 0 0
0 1 0 1 0 010 0 1 0 1 020 0 0 1 0
21
0 0 0 0 1 0x x
x
D
x
x
x
x
x
f
x
D f
f ff ff ff
fffff
ff f
f f f
xf Dxx f
How can a square matrix be constructed so that when it premultiplies a vector it gives a vector containing the first‐order partial derivative?
Second‐Order Partial Derivative (1 of 2)
Slide 38
How can a square matrix be constructed so that when it premultiplies a vector it gives a vector containing the second‐order partial derivative?
2
22 xDf fx
x
1x 2x 3x 4x 5x 6x
1f2f
3f 4f 5f 6f
2
22 1 0
23 2 1
24 3 2
25 4 3
26 5
2
42
1
2
3
4
5
7 6 56
2 1 0 0 0 01 2 1 0 0 00 1 2 1 0 010 0 1 2 1 00 0 0 1 2 10 0 0 0 1 2
222222
x
x
D f
x
x
x
x
x
x
ffffff
f f ff f ff f ff f ff f ff f f
2xD f
?
37
38
-
9/22/2019
20
Second‐Order Partial Derivative (2 of 2)
Slide 39
1x 2x 3x 4x 5x 6x
1f2f
3f 4f 5f 6f
2
22 1 0
23 2 1
24 3 2
25 4 3
26 5
2
42
1
2
3
4
5
7 6 56
2 1 0 0 0 01 2 1 0 0 00 1 2 1 0 010 0 1 2 1 00 0 0 1 2 10 0 0 0 1 2
222222
x
x
D f
x
x
x
x
x
x
ffffff
f f ff f ff f ff f ff f ff f f
2xD f
2
22 xDf fx
x
How can a square matrix be constructed so that when it premultiplies a vector it gives a vector containing the second‐order partial derivative?
Why Do We Need Separate Derivative Matrices for First‐ and Second‐Order Derivatives?
Slide 40
It is known that,2
2x x x
Can we just calculate D(2) from D(1)? ?
2 1 1x x xD 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 00 2 0 1 0 1 2 1 0 0
1 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 derivate matrix makes optimal use of the available grid resolution.
39
40
-
9/22/2019
21
USE SPARSE MATRICES!!!!!!!
Slide 41
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!
Placing Diagonals into Sparse Matrices in MATLAB
42
M = 6;Z = sparse(M,M);d = ones(M,1);A = spdiags(d,0,Z);
M = 6;Z = sparse(M,M);d = ones(M,1);A = spdiags(-d,-1,Z);A = spdiags(+d,+1,A);
A =[ 0 1 0 0 0 0 ][ -1 0 1 0 0 0 ][ 0 -1 0 1 0 0 ][ 0 0 -1 0 1 0 ][ 0 0 0 -1 0 1 ][ 0 0 0 0 -1 0 ]
A =[ 1 0 0 0 0 0 ][ 0 1 0 0 0 0 ][ 0 0 1 0 0 0 ][ 0 0 0 1 0 0 ][ 0 0 0 0 1 0 ][ 0 0 0 0 0 1 ]
41
42
-
9/22/2019
22
Slide 43
Incorporating Boundary Conditions
Dirichlet Boundary Conditions(1 of 2)
44
x1x 2x 4x 5x 6x 7x3x1f 2f 4f 5f 6f 7f3f
The simplest boundary condition is to assume all function values outside of the grid are zero.
27 6 7
22
2 0d f f fdx x
21 1 2
2220d f f f
dx x
21 1
22
2i i i id f f f fdx x
43
44
-
9/22/2019
23
Dirichlet Boundary Conditions(2 of 2)
45
x
7x
6x
4x
3x
2x
1x
5x
x
21 1 2
2220d f f f
dx x
27 6 7
22
2 0d f f fdx x
21 2 32
222f f fd f
dx x
23 2 3 4
22
2d f f f fdx x
23 4 54
22
2f f fd fdx x
25 4 5 6
22
2d f f f fdx x
26 5 6 7
22
2d f f f fdx x
2
3
42
5
7
6
12 1 0 0 0 0 01 2 1 0 0 0 00 1 2 1 0 0 0
1 0 0 1 2 1 0 00 0 0 1 2 1 00 0 0 0 1 2 10 0 0 0 0 1 2
fff
x ff
f
f
2xD
Periodic Boundary Conditions (1 of 2)
46
x1x 2x 4x 5x 6x 7x3x1f 2f 4f 5f 6f 7f3f
If the problem is periodic (i.e. keeps repeating), then the value outside of the grid is the same as the value at the opposite side of the grid.
27 6 7
21
2
2d f f fdx x
f
21 21
27
2
2f f fd fdx x
21 1
22
2i i i id f f f fdx x
45
46
-
9/22/2019
24
Periodic Boundary Conditions(2 of 2)
47
x
7x
6x
4x
3x
2x
1x
5x
x
21 21
27
2
2f f fd fdx x
27 6 7
21
2
2d f f fdx x
f
21 2 32
222f f fd f
dx x
23 2 3 4
22
2d f f f fdx x
23 4 54
22
2f f fd fdx x
25 4 5 6
22
2d f f f fdx x
26 5 6 7
22
2d f f f fdx x
2
7
3
42
5
6
12 1 0 0 0 01 2 1 0 0 0 00 1 2 1 0 0 0
1 0 0 1 2 1 0 00 0 0 1 2 1 00 0 0 0 1 2 1
0 0 0 0 1 2
1
1
fff
x ff
f
f
2xD
Neuman Boundary Conditions(1 of 3)
48
x1x 2x 4x 5x 6x 7x3x1f 2f 4f 5f 6f 7f3f
The Neuman boundary condition allows functions to continue linearly off of the grid as if to infinity.
27 7 6 7
2 0df f f d fdx x dx
21 2 1 1
2 0df f f d fdx x dx
21 1 1 1
22
2 2
i i i i i i idf f f d f f f fdx x dx x
47
48
-
9/22/2019
25
Neuman Boundary Conditions(2 of 3)
49
x
7x
6x
4x
3x
2x
1x
5x
x
1 2 12 22
df f fdx x
7 7 62 22
df f fdx x
3 12
2f fdf
dx x
3 4 2
2df f fdx x
5 34
2f fdf
dx x
5 6 4
2df f fdx x
6 7 5
2df f fdx x
1
7
2
3
4
5
6
2 2 0 0 0 0 0
0 0 0 0 0 2 2
1 0 1 0 0 0 00 1 0 1 0 0 0
1 0 0 1 0 1 0 02
0 0 0 1 0 1 00 0 0 0 1 0 1
fff
xff
f
f
xD
Neuman Boundary Conditions(3 of 3)
50
x
7x
6x
4x
3x
2x
1x
5x
x
21
2 0d fdx
27
2 0d fdx
21 2 32
222f f fd f
dx x
23 2 3 4
22
2d f f f fdx x
23 4 54
22
2f f fd fdx x
25 4 5 6
22
2d f f f fdx x
26 5 6 7
22
2d f f f fdx x
2
3
42
1
5
6
7
1 2 1 0 0 0 00 1 2 1 0 0 0
1 0 0 1 2 1 0 00 0 0 1 2 1 00 0 0 0 1 2 1
0 0 0 0 0 0 0
0 0 0 0 0 0 0
ff
f
f
ff
x f
2xD
49
50
-
9/22/2019
26
High‐Order Boundary Conditions (1 of 2)
51
x1x 2x 4x 5x 6x 7x3x1f 2f 4f 5f 6f 7f3f
Here we estimate the derivative at the boundaries using special finite‐difference equations derived specifically for these points.
27 4 5 6 7
2 2
4 5 2d f f f f fdx h
21 2 3 41
2 2
2 5 4f f f fd fdx h
21 1
2 2
2i i i id f f f fdx h
High‐Order Boundary Conditions (2 of 2)
52
x
7x
6x
4x
3x
2x
1x
5x
x
21 2 3 41
22
2 5 4f f f fd fdx x
27 4 5 6 7
224 5 2d f f f f f
dx x
21 2 32
222f f fd f
dx x
23 2 3 4
22
2d f f f fdx x
23 4 54
22
2f f fd fdx x
25 4 5 6
22
2d f f f fdx x
26 5 6 7
22
2d f f f fdx x
4
7
2
6
1
2
3
5
2 5 4 1 0 0 0
0 0 0 1 4 5
1 2 1 0 0 0 00 1 2 1 0 0 0
1 0 0 1 2 1 0 00 0 0 1 2 1 00 0 0 0 1 2
21
fff
x ff
f
f
2xD
51
52
-
9/22/2019
27
Slide 53
Solving OrdinaryDifferential Equations
Boundary Values
Slide 54
Many differential equations give boundary values, or initial conditions, that must be incorporated into the matrix equation in order to obtain a numerical solution.
For example, a problem may be stated as…
2
2 5 6 0 0 2
0 2 2 0.2
d f x df xf x x
dx dx
f f
Boundary values
53
54
-
9/22/2019
28
Why are Boundary Values Needed?
Slide 55
To solve the differential equation, it is first converted to matrix form.
2
2 5 6 0d df x f x f xdx dx
2 5 6 0x xD f D f f
Next, this is rearranged into standard form 𝐀𝐱 𝐛. 20 5 6x xA f A D D I
This is not solvable.
1 0 0f A The boundary values have to be incorproated in order to obtain a nontrivial solution. When this is done, the matrix equation will becomes
A f b
Incorporating Boundary Values
Slide 56
It is possible to incorporate the boundary values into the matrix operators that we used to build [A]. However, it is usually easier to incorporate these directly into the final matrix equation.
The steps are:
1. Build direct matrix equation [A][f]=[0].2. For each row in [A] that corresponds to a boundary point:
1. Replace entire row with 0’s.2. Insert a ‘1’ into the diagonal, or pivot, position.3. Place the boundary value in the same row of [b].
55
56
-
9/22/2019
29
Solve Matrix Equation
Slide 57
This matrix equation is now in the form A f b
This is solved for [f] as follows. 1f A b
For small to moderate size problems, this can be solved directly using LU decomposition, or just backward division in MATLAB.
f = A\b; DO NOT USE f = inv(A)*bFor large problems, iterative methods are preferred, but the conditioning of [A] becomes important and a solution is not guaranteed.
Example (1 of 10)
Slide 58
2
2 5 6 0 0 2
0 2 2 0.2
d f x df xf x x
dx dx
f f
Solve the following ordinary differential equation.
% DEFINE BOUNDARY VALUESxa = 0;xb = 2;fa = 2;fb = 0.2;
57
58
-
9/22/2019
30
Example (2 of 10)
Slide 59
Step 1 – Formulate the matrix equation [A][f] = [0].
2
2 5 6 0d df x f x f xdx dx
2 5 6 0x xD f D f f
2
0
5 6x x
A f
A D D I
2 5 6 0x xD D I f
Example (3 of 10)
Slide 60
Step 2 – Calculate grid parameters N and x.
Choose N = 11. This is just a first guess!
Calculate x.
2 0 0.21 11 1
b axN
% GRID PARAMETERSNx = 11;dx = (xb - xa)/(Nx - 1);
59
60
-
9/22/2019
31
Example (4 of 10)
Slide 61
Step 3 – Build Matrix Operators , , .2xD I xD
2
50 25 0 0 0 0 0 0 0 0 025 50 25 0 0 0 0 0 0 0 00 25 50 25 0 0 0 0 0 0 00 0 25 50 25 0 0 0 0 0 00 0 0 25 50 25 0 0 0 0 00 0 0 0 25 50 25 0 0 0 00 0 0 0 0 25 50 25 0 0 00 0 0 0 0 0 25 50 25 0 00 0 0 0 0 0 0 25 50 25 00 0 0 0 0 0 0 0 25 50 250 0 0 0 0 0 0 0 0 25 50
xD
0 2.5 0 0 0 0 0 0 0 0 02.5 0 2.5 0 0 0 0 0 0 0 00 2.5 0 2.5 0 0 0 0 0 0 00 0 2.5 0 2.5 0 0 0 0 0 00 0 0 2.5 0 2.5 0 0 0 0 00 0 0 0 2.5 0 2.5 0 0 0 00 0 0 0 0 2.5 0 2.5 0 0 00 0 0 0 0 0 2.5 0 2.5 0 00 0 0 0 0 0 0 2.5 0 2.5 00 0 0 0 0 0 0 0 2.5 0 2.50 0 0 0 0 0 0 0 0 2.5 0
xD
1 0 0 0 0 0 0 0 0 0 00 1 0 0 0 0 0 0 0 0 00 0 1 0 0 0 0 0 0 0 00 0 0 1 0 0 0 0 0 0 00 0 0 0 1 0 0 0 0 0 00 0 0 0 0 1 0 0 0 0 00 0 0 0 0 0 1 0 0 0 00 0 0 0 0 0 0 1 0 0 00 0 0 0 0 0 0 0 1 0 00 0 0 0 0 0 0 0 0 1 00 0 0 0 0 0 0 0 0 0 1
I
% BUILD MATRIX OPERATORSI = speye(Nx,Nx);[DX,DX2] = fdder1d(Nx,dx);
Example (5 of 10)
Slide 62
Step 4 – Calculate [A] and [b].
2
44 37.5 0 0 0 0 0 0 0 0 012.5 44 37.5 0 0 0 0 0 0 0 0
0 12.5 44 37.5 0 0 0 0 0 0 00 0 12.5 44 37.5 0 0 0 0 0 00 0 0 12.5 44 37.5 0 0 0 0 0
5 6 0 0 0 0 12.5 44 37.5 0 0 0 00 0 0 0 0 12.5 44 37.5 0 0 00 0 0 0 0 0 12.5 44 37.5 0 00 0 0 0 0 0 0 12.5 44 37.5 00 0 0 0 0 0 0 0 12.5 44
x xA D D I
37.5
0 0 0 0 0 0 0 0 0 12.5 44
00000000000
b
% CALCULATE [A] AND [b]A = DX2 + 5*DX + 6*I;b = sparse(Nx,1);
61
62
-
9/22/2019
32
Example (6 of 10)
Slide 63
Step 5 – Incorporate Boundary Values into [A] and [b].
2
0 0 0 0 0 0 0 0 012.5 44 37.5 0 0 0 0 0 0 0 0
0 12.5 44 37.5 0 0 0 0 0 0 00 0 12.5 44 37.5 0 0 0 0 0 00 0 0 12.5 44 37.5 0 0 0 0 0
5 6 0 0 0 0 12.5 44 37.5 0 0 0 00 0 0 0 0 12.5 44 37.5 0 0 00 0 0 0 0 0 12.5 44 37.5 0 00 0 0 0 0 0 0 12.5 44 37.5 00 0 0 0 0 0 0 0 12.
1
5
0
44 37.50
x xA D D I
0 0 0 0 0 0 0 0 0 1
0000000
2
.2
00
0
b
% INCORPORATE BOUNDARY VALUESA([1 Nx],:) = 0;A(1,1) = 1;A(Nx,Nx) = 1;b([1 Nx]) = [ fa fb ];
Example (7 of 10)
Slide 64
Step 6 – Solve for [f].
1
2.00002.68422.48282.01841.54071.13490.81810.58160.40970.28680.2000
f A b
% SOLVE FOR [f]f = A\b;
63
64
-
9/22/2019
33
Example (8 of 10)
Slide 65
Step 7 – Check for Convergence
A solution was obtained, but it was all based on a guess for how many points N to use on the grid.
It is critical to check for convergence by increasing the value of N until the changes in the solution are neglibable.
Converged aroundN ≈ 20.
Example (9 of 10)
Slide 66
Step 8 – Obtain the final answer at convergence (i.e. N = 20).
% PLOT RESULTh = plot(x,f,'-ob','LineWidth',2);h2 = get(h,'Parent');set(h2,'LineWidth',2,'FontSize',18);xlabel('$x$','Interpreter','LaTex');ylabel('$f(x)$','Interpreter','LaTex');T = [0 0.5 1 1.5 2];L = {'0' '0.5' '1.0' '1.5' '2.0'};set(gca,'XTick',T,'XTickLabel',L);T = [0.5 1 1.5 2 2.5 3];L = {'0.5' '1.0' '1.5' '2.0' '2.5' '3.0'};set(gca,'YTick',T,'YTickLabel',L);
65
66
-
9/22/2019
34
Example (10 of 10)
Slide 67
Step 9 – Post process the data.
The finite‐difference method is complete.
Usually, after obtaining a solution, the results are analyzed further.
Code Altogether
Slide 68
% DEFINE BOUNDARY VALUESxa = 0;xb = 2;fa = 2;fb = 0.2;
% GRID PARAMETERSNx = 20;dx = (xb - xa)/(Nx - 1);
% BUILD MATRIX OPERATORSI = speye(Nx,Nx);[DX,DX2] = fdder1d(Nx,dx);
% CALCULATE [A] AND [b]A = DX2 + 5*DX + 6*I;b = sparse(Nx,1);
% INCORPORATE BOUNDARY VALUESA([1 Nx],:) = 0;A(1,1) = 1;A(Nx,Nx) = 1;b([1 Nx]) = [ fa fb ];
% SOLVE PROBLEMf = A\b;
2
2 5 6 0
0 20 2 2 0.2
d f x df xf x
dx dxx
f f
67
68