tut6 sample fdm & fem.pdf

6
MAT 3310 Chapter 3 Appendix 1. This example is a general case on solving an ODE by Finite Difference Method. y- 2y 0 + y = 0, where 0 x 1 and y(0) = 2, y(1) = 1 The governing equation is not the same as we have discussed before. Note that the ma- trix would not be the same as before. We have employed the Central Difference Scheme. Set N=10 (total number of grids), h = 1 N (grid length) y”(x i )= y i-1 - 2y i + y i+1 h 2 y 0 (x i )= y i+1 - y i-1 2h Then the ODE becomes, y i-1 - 2y i + y i+1 h 2 - y i+1 - y i-1 h + y i = 0, which can be written as, y i-1 (1 + h)+ y i (h 2 - 2) + y i+1 (1 - h)=0 and y 0 =2, y 10 = 1 (in the program, y(1) = 2, y(11) = 1) We have employed the LU factorization to solve this problem. The Exact solution (solid line) and the Numerical solution (’o’) are both provided. Exact solution: y(x)= e x-1 [x - 2e(x - 1)] Absolute Max Error has also provided for you as a reference. Note that this error is NOT the same as the one stated in the notes, cause the governing equation is different. Code has been uploaded on the course web page. You are advised to try it out in Matlab. 0 0.2 0.4 0.6 0.8 1 1 1.2 1.4 1.6 1.8 2 2.2 2.4 Figure 1: Example 1 1

Upload: julianli0220

Post on 17-Jul-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Tut6 Sample FDM & FEM.pdf

MAT 3310 Chapter 3 Appendix

1. This example is a general case on solving an ODE by Finite Difference Method.

y”− 2y′ + y = 0, where 0 ≤ x ≤ 1 and y(0) = 2, y(1) = 1

The governing equation is not the same as we have discussed before. Note that the ma-trix would not be the same as before. We have employed the Central Difference Scheme.

Set N=10 (total number of grids), h =1N

(grid length)

y”(xi) =yi−1 − 2yi + yi+1

h2

y′(xi) =yi+1 − yi−1

2hThen the ODE becomes,yi−1 − 2yi + yi+1

h2− yi+1 − yi−1

h+ yi = 0,

which can be written as,yi−1(1 + h) + yi(h2 − 2) + yi+1(1− h) = 0and y0 = 2 , y10 = 1 (in the program, y(1) = 2, y(11) = 1)

We have employed the LU factorization to solve this problem. The Exact solution (solidline) and the Numerical solution (’o’) are both provided. Exact solution:y(x) = ex−1[x− 2e(x− 1)]

Absolute Max Error has also provided for you as a reference. Note that this error is NOTthe same as the one stated in the notes, cause the governing equation is different.Code has been uploaded on the course web page. You are advised to try it out in Matlab.

0 0.2 0.4 0.6 0.8 11

1.2

1.4

1.6

1.8

2

2.2

2.4

Figure 1: Example 1

1

Page 2: Tut6 Sample FDM & FEM.pdf

2. This example follows our Notes, Chapter 3.

−u” = 2, where 0 ≤ x ≤ 1 and u(0) = 0, u(1) = 0

Same as Example 1, we have employed the Central Difference Scheme.

Set N=10 (total number of grids), h =1N

(grid length)

u”(xi) =ui−1 − 2ui + ui+1

h2

Exact Solution:u(x) = −x2 + x

Note that the solution is smooth in this case and f” = 0. Thus, the error term shouldbe close to 0.

0 0.2 0.4 0.6 0.8 10

0.05

0.1

0.15

0.2

0.25

0.3

0.35

Figure 2: Example 2

2

Page 3: Tut6 Sample FDM & FEM.pdf

3. This example follows our Notes, Chapter 3. But our solution is differentiable up to first order.

−u” = 2, where 0 ≤ x ≤ 0.5−u” = 1, where 0.5 ≤ x ≤ 1and u(0) = 1, u(1) = −1

Same as Example 1, we have employed the Central Difference Scheme.

Set N=10 (total number of grids), h =1N

(grid length)

u”(xi) =ui−1 − 2ui + ui+1

h2

Exact Solution:u(x) = −x2 − 9

8x + 1, where 0 ≤ x ≤ 0.5

u(x) = −12(x− 1)2 − 21

8(x− 1)− 1, where 0.5 ≤ x ≤ 1

Note that the solution is NOT smooth in this case and f” does NOT exist. Thus, theerror term should NOT be close to 0, but a relatively acceptable range.

0 0.2 0.4 0.6 0.8 1−1

−0.8

−0.6

−0.4

−0.2

0

0.2

0.4

0.6

0.8

1

Figure 3: Example 3

To summarize the maximum absolute error vs grid length for all the above 3 cases:h 0.1 0.01 0.001Example 1 0.0018 0.1772 ∗ 10−4 0.0018 ∗ 10−4

Example 2 0.0011 ∗ 10−13 0.0214 ∗ 10−13 0.8918 ∗ 10−13

Example 3 0.0125 0.0013 0.0001Note that in both Examples 1 and 3, when you decrease the grid length, the degree of ac-curacy is increased. However, for Example 2, since the error size is near the ε built-in theMatlab, instability will become significant when you decrease the grid length.

3

Page 4: Tut6 Sample FDM & FEM.pdf

4. Compare this example with Example 2.

−u” = 2, where 0 ≤ x ≤ 1 and u(0) = 0, u(1) = 0

We employed the Finite Element Method to work out the same problem.

We have:a(u, v) =

∫ b

a(cu′v′ + quv)dx, and g(v) =

∫ b

afvdx− αv(a)

A~U = ~B, whereAij = a(φi, φj), Bi = g(φi)− βa(φN+1, φi)

Set N=10 (total number of grids), h =1N

= 0.1 (grid length), c = 1, q = α = β = 0

Aij = a(φi, φj) =∫ 1

0φ′iφ

′jdx,

Bi = g(φi) =∫ 1

02φidx

That is,Aii = 20, Ai,i−1 = Ai,i+1 = −10, Bi = 0.2

Exact Solution:u(x) = −x2 + x

0 0.2 0.4 0.6 0.8 10

0.05

0.1

0.15

0.2

0.25

Figure 4: Example 4

4

Page 5: Tut6 Sample FDM & FEM.pdf

5. Compare this example with Example 3.

−u” = 2, where 0 ≤ x ≤ 0.5−u” = 1, where 0.5 ≤ x ≤ 1and u(0) = 1, u(1) = −1

We employed the Finite Element Method to work out the same problem.

We have:a(u, v) =

∫ b

a(cu′v′ + quv)dx, and g(v) =

∫ b

afvdx− αv(a)

A~U = ~B, whereAij = a(φi, φj), Bi = g(φi)− βa(φN+1, φi)

Set N=10 (total number of grids), h =1N

= 0.1 (grid length), c = 1, q = 0, α = 1 ,β = −1Aij = a(φi, φj) =

∫ 1

0φ′iφ

′jdx,

Bi = g(φi) =∫ 1

02φidx, where 0 ≤ x ≤ 0.5

Bi = g(φi) =∫ 1

0φidx, where 0.5 ≤ x ≤ 1

That is,Aii = 20, Ai,i−1 = Ai,i+1 = −10,Bi = 0.2, for 2 ≤ i ≤ 4, Bi = 0.1, for 6 ≤ i ≤ 8,and B1 = 10.2, B5 = 0.15, B9 = −9.9

Exact Solution:u(x) = −x2 − 9

8x + 1, where 0 ≤ x ≤ 0.5

u(x) = −12(x− 1)2 − 21

8(x− 1)− 1, where 0.5 ≤ x ≤ 1

0 0.2 0.4 0.6 0.8 1−1

−0.8

−0.6

−0.4

−0.2

0

0.2

0.4

0.6

0.8

1

Figure 5: Example 5

5

Page 6: Tut6 Sample FDM & FEM.pdf

6. This is a new example.

−u” = π2sinπx, where 0 ≤ x ≤ 1and u(0) = 0, u(1) = 0

We employed the Finite Element Method to work out the same problem.

We have:a(u, v) =

∫ b

a(cu′v′ + quv)dx, and g(v) =

∫ b

afvdx− αv(a)

A~U = ~B, whereAij = a(φi, φj), Bi = g(φi)− βa(φN+1, φi)

Set N=10 (total number of grids), h =1N

= 0.1 (grid length), c = 1, q = α = β = 0That is,Aii = 20, Ai,i−1 = Ai,i+1 = −10, Bi = 10[−sinπxi−1 + 2sinπxi − sinπxi+1]

Exact Solution:u(x) = sinπx

0 0.2 0.4 0.6 0.8 10

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

Figure 6: Example 6

Note that you may check the error by the program attached.

6