You are on page 1of 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 matrix would not be the same as before. We have employed the Central Difference Scheme.
1
Set N=10 (total number of grids), h =
(grid length)
N
yi1 2yi + yi+1
y(xi ) =
h2
y

y
i+1
i1
y 0 (xi ) =
2h
Then the ODE becomes,
yi+1 yi1
yi1 2yi + yi+1

+ yi = 0,
2
h
h
which can be written as,
yi1 (1 + h) + yi (h2 2) + yi+1 (1 h) = 0
and 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 (solid
line) and the Numerical solution (o) are both provided. Exact solution:
y(x) = ex1 [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.

2.4

2.2

1.8

1.6

1.4

1.2

0.2

0.4

0.6

Figure 1: Example 1

0.8

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.
1
Set N=10 (total number of grids), h =
(grid length)
N
ui1 2ui + ui+1
u(xi ) =
h2
Exact Solution:
u(x) = x2 + x
Note that the solution is smooth in this case and f = 0. Thus, the error term should
be close to 0.

0.35

0.3

0.25

0.2

0.15

0.1

0.05

0.2

0.4

0.6

Figure 2: Example 2

0.8

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 1
and u(0) = 1, u(1) = 1
Same as Example 1, we have employed the Central Difference Scheme.
1
Set N=10 (total number of grids), h =
(grid length)
N
ui1 2ui + ui+1
u(xi ) =
h2
Exact Solution:
9
u(x) = x2 x + 1, where 0 x 0.5
8
1
21
u(x) = (x 1)2 (x 1) 1, where 0.5 x 1
2
8
Note that the solution is NOT smooth in this case and f does NOT exist. Thus, the
error term should NOT be close to 0, but a relatively acceptable range.

1
0.8
0.6
0.4
0.2
0
0.2
0.4
0.6
0.8
1

0.2

0.4

0.6

0.8

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.001
Example 1 0.0018
0.1772 104
0.0018 104
13
13
Example 2 0.0011 10
0.0214 10
0.8918 1013
Example 3 0.0125
0.0013
0.0001
Note that in both Examples 1 and 3, when you decrease the grid length, the degree of accuracy is increased. However, for Example 2, since the error size is near the built-in the
Matlab, instability will become significant when you decrease the grid length.

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: R
Rb
b
a(u, v) = a (cu0 v 0 + quv)dx, and g(v) = a f vdx v(a)
~ = B,
~ where
AU
Aij = a(i , j ), Bi = g(i ) a(N +1 , i )
1
Set N=10 (total number of grids), h =
= 0.1 (grid length), c = 1, q = = = 0
N
R1 0 0
Aij = a(i , j) = 0 i j dx,
R1
Bi = g(i ) = 0 2i dx
That is,
Aii = 20, Ai,i1 = Ai,i+1 = 10, Bi = 0.2
Exact Solution:
u(x) = x2 + x

0.25

0.2

0.15

0.1

0.05

0.2

0.4

0.6

Figure 4: Example 4

0.8

5. Compare this example with Example 3.


u = 2, where 0 x 0.5
u = 1, where 0.5 x 1
and u(0) = 1, u(1) = 1
We employed the Finite Element Method to work out the same problem.
We have: R
Rb
b
a(u, v) = a (cu0 v 0 + quv)dx, and g(v) = a f vdx v(a)
~ = B,
~ where
AU
Aij = a(i , j ), Bi = g(i ) a(N +1 , i )
1
= 0.1 (grid length), c = 1, q = 0, = 1 ,
Set N=10 (total number of grids), h =
N
= 1
R1
Aij = a(i , j) = 0 0i 0j dx,
R1
Bi = g(i ) = 0 2i dx, where 0 x 0.5
R1
Bi = g(i ) = 0 i dx, where 0.5 x 1
That is,
Aii = 20, Ai,i1 = 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:
9
u(x) = x2 x + 1, where 0 x 0.5
8
1
21
u(x) = (x 1)2 (x 1) 1, where 0.5 x 1
2
8

1
0.8
0.6
0.4
0.2
0
0.2
0.4
0.6
0.8
1

0.2

0.4

0.6

Figure 5: Example 5

0.8

6. This is a new example.


u = 2 sinx, 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: R
Rb
b
a(u, v) = a (cu0 v 0 + quv)dx, and g(v) = a f vdx v(a)
~ = B,
~ where
AU
Aij = a(i , j ), Bi = g(i ) a(N +1 , i )
1
Set N=10 (total number of grids), h =
= 0.1 (grid length), c = 1, q = = = 0
N
That is,
Aii = 20, Ai,i1 = Ai,i+1 = 10, Bi = 10[sinxi1 + 2sinxi sinxi+1 ]
Exact Solution:
u(x) = sinx

1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0

0.2

0.4

0.6

0.8

Figure 6: Example 6
Note that you may check the error by the program attached.

You might also like