You are on page 1of 37

STATE UNIVERSITY OF NEW YORK AT BUFFALO

MTH538 NUMERICAL ANALYSIS II


Final Project

NAME: LENG-FENG LEE


ADVISOR: DR. MIKHAIL KHENNER.
DATE: 10th April 2006.

MTH 538 NUMERICAL ANALYSIS II FINAL PROJECT


Leng-Feng Lee (#2963-2068, llee3@eng.buffalo.edu)
Advisor: Dr. Mikhail Khenner (mkhenner@nsm.buffalo.edu)
10 April 2006.
ABSTRACT:
In this final project, three problems involving the numerical methods for: Linear system
of Equations; Eigen Values problem; and Linear and Nonlinear Second Order Boundary
Value Problem (BVP) are assigned. In particular, the linear system is solved by using
both Trapezoidal rule and Composite Simpsons rule. The Eigen values problems is solve
using QR algorithm, and the second order boundary value problem is solved using finite
difference method (both linear and nonlinear). The problem statement, the steps to obtain
the solution, the results (graphical or analytical), and discussion related to the solution
were presented where necessary.
PROBLEM 1:
Given:
u ( x ) = f ( x ) + K ( x, t ) u (t ) dt
b

(1)

Where a and b and the function f and K are given. To approximate the function u on the
interval [a, b], a partition x0 = a < x1 < " < xm 1 < xm = b is selected and the equation:
u ( xi ) = f ( xi ) + K ( xi , t ) u (t ) dt ,
b

for each i = 0," , m

(2)

Are solved for u ( x0 ) , u ( x1 ) ," , u ( xm ) . The integrals are approximated using quadrature
formulas based on the nodes x0 , x1 ," , xm . In this problem, a = 0 , b = 1 , f ( x ) = x 2 , and
K ( x, t ) = e x t .
Part A. Show the linear system:

1
u ( 0 ) = f ( 0 ) + K ( 0, 0 ) u ( 0 ) + K ( 0,1) u (1) ;
2
1
u (1) = f (1) + K (1, 0 ) u ( 0 ) + K (1,1) u (1) ;
2

(3)

must be solved when the trapezoidal rule is used.

Part B. Set up and solve the linear system that results when the Composite Simpsons rule
is used with m = 4, 6,8 .

SOLUTION:
A. The Trapezoidal rule is given by:

h
h3 "
f ( x ) dx = [ f ( x0 ) + f ( x1 )]
f ( )
2
2

(4)

h3 "
f ( ) 0 , we have x0 = a, x1 = b , and h = x1 x0 = b a = 1 .
2
Hence, we can write Equation (2) for x0 = a, x1 = b as follow:

Assuming the error term

1
u ( a ) = f ( a ) + K ( a, a ) u ( a ) + K ( a, b ) u ( b ) ;
2
1
u ( b ) = f ( b ) + K ( b, a ) u ( a ) + K ( b, b ) u ( b ) ;
2

(5)

Substitute Equation (5) with a = 0, b = 1 , we get:


1
u ( 0 ) = f ( 0 ) + K ( 0, 0 ) u ( 0 ) + K ( 0,1) u (1) ;
2
1
u (1) = f (1) + K (1, 0 ) u ( 0 ) + K (1,1) u (1)
2

(6)

Which is same as Equation (3), hence proved.


Now, evaluate Equation (6) by substituting the know values, we obtain the following
linear equations:
u ( x0 ) = 0.5000u ( x0 ) + 1.3591u ( x1 )
u ( x1 ) = 1.3591u ( x0 ) + 0.5000u ( x1 ) + 1
Further write in the form of Au = b , we have:
0
0.5000 1.3591
A=
and b =

1
1.3591 0.5000
Solve for u ( x0 ) , u ( x1 ) using Gaussian Elimination method, we have:
u ( x0 ) = 0.8509; u ( x1 ) = 0.3130

This result is plotted in Figure 1.

(7)

B. Now solve the problem by using the Composite Simpsons rule. The Composite
Simpsons rule is given as:

h
f ( x ) dx =
f (a) + 2
3

For m = 4 , h =

( n 2 )1

f ( x ) + 4
j =1

2j

j =1

ba
f ( x2 j 1 ) + f ( b )
h 4 f ( 4) ( )
180

(8)

ba 1
= , the interval is divided as follow:
m
4

Now, using Equation (8), assuming the error

b a 4 ( 4)
h f ( ) 0 , we can have the
180

following set of linear equations:


K ( xi , x0 ) u ( x0 ) + 4 K ( xi , x1 ) u ( x1 ) + "

1
u ( xi ) = f ( xi ) +
2 K ( xi , x2 ) u ( x2 ) + 4 K ( xi , x3 ) u ( x3 ) + "
12

K ( xi , x4 ) u ( x4 )

(9)

For x0 = 0, x1 = 0.25, x2 = 0.50, x3 = 0.75, x4 = 1.0 , we can solve for u ( x0 ) ," , u ( x4 ) by


solving the following linear system:
u ( x0 ) = 0.0833u ( x0 ) + 0.4280u ( x1 ) + 0.2748u ( x2 ) + 0.7057u ( x3 ) + 0.2265u ( x4 )
u ( x1 ) = 0.1070u ( x0 ) + 0.3333u ( x1 ) + 0.2140u ( x2 ) + 0.5496u ( x3 ) + 0.1764u ( x4 ) + 0.0625
u ( x2 ) = 0.1374u ( x0 ) + 0.4280u ( x1 ) + 0.1667u ( x2 ) + 0.4280u ( x3 ) + 0.1374u ( x4 ) + 0.25
u ( x3 ) = 0.1764u ( x0 ) + 0.5496u ( x1 ) + 0.2140u ( x2 ) + 0.3333u ( x3 ) + 0.1070u ( x4 ) + 0.5625
u ( x4 ) = 0.2265u ( x0 ) + 0.7057u ( x1 ) + 0.2748u ( x2 ) + 0.4280u ( x3 ) + 0.0833u ( x4 ) + 1
Write in the form of Au = b , we have:
0.2748 0.7057
0.2265
0.9167 0.428
0
0.1070 0.6667 0.2140 0.5496 0.1764
0.0625

A = 0.1374 0.4280 0.8333 0.4280 0.1374 and b = 0.25

0.1764 0.5496 0.2140 0.6667 0.1070


0.5625
0.2265 0.7057 0.2748 0.4280 0.9167
1.0

(10)

And solve for u ( xi ) using Gaussian Elimination, we have:


u ( x0 ) = 1.2343; u ( x1 ) = 0.9507; u ( x2 ) = 0.7659; u ( x3 ) = 0.5845; u ( x4 ) = 0.4485

This result is plotted in Figure 1.

For m = 6 , h =

ba 1
= , the interval is divided as follow:
m
6

Again, using Equation (8), assuming the error

b a 4 ( 4)
h f ( ) 0 , we can have the
180

following set of linear equations:


K ( xi , x0 ) u ( x0 ) + 4 K ( xi , x1 ) u ( x1 ) + 2 K ( xi , x2 ) u ( x2 ) + "

1
u ( xi ) = f ( xi ) +
4 K ( xi , x3 ) u ( x3 ) + 2 K ( xi , x4 ) u ( x4 ) + "
(11)
18

4 K ( xi , x5 ) u ( x5 ) + K ( xi , x6 ) u ( x6 )

1
2
3
4
5
For x0 = 0, x1 = , x2 = , x3 = , x4 = , x5 = , x6 = 1 , we can solve for u ( x0 ) ," , u ( x6 )
6
6
6
6
6
by solving the following linear system:
u ( x0 ) = 0.0556u ( x0 ) + 0.2625u ( x1 ) + 0.1551u ( x2 ) + 0.3664u ( x3 ) + 0.2164u ( x4 ) + 0.5113u ( x5 ) + 0.1510u ( x6 )

u ( x1 ) = 0.0656u ( x0 ) + 0.2222u ( x1 ) + 0.1313u ( x2 ) + 0.3101u ( x3 ) + 0.1832u ( x4 ) + 0.4328u ( x5 ) + 0.1278u ( x6 ) + 0.0278

u ( x2 ) = 0.0775u ( x0 ) + 0.2625u ( x1 ) + 0.1111u ( x2 ) + 0.2625u ( x3 ) + 0.1551u ( x4 ) + 0.3664u ( x5 ) + 0.1082u ( x6 ) + 0.1111


u ( x3 ) = 0.0916u ( x0 ) + 0.3101u ( x1 ) + 0.1313u ( x2 ) + 0.2222u ( x3 ) + 0.1313u ( x4 ) + 0.3101u ( x5 ) + 0.0916u ( x6 ) + 0.2500

u ( x4 ) = 0.1082u ( x0 ) + 0.3664u ( x1 ) + 0.1551u ( x2 ) + 0.2625u ( x3 ) + 0.1111u ( x4 ) + 0.2625u ( x5 ) + 0.0775u ( x6 ) + 0.4444

u ( x5 ) = 0.1278u ( x0 ) + 0.4328u ( x1 ) + 0.1832u ( x2 ) + 0.3101u ( x3 ) + 0.1313u ( x4 ) + 0.2222u ( x5 ) + 0.0656u ( x6 ) + 0.6944


u ( x6 ) = 0.1510u ( x0 ) + 0.5113u ( x1 ) + 0.2164u ( x2 ) + 0.3664u ( x3 ) + 0.1551u ( x4 ) + 0.2625u ( x5 ) + 0.0556u ( x6 ) + 1

Write in the form of Au = b , we have:

(12)

0.1551 0.3664 0.2164 0.5113 0.1510


0.9444 0.2625
0.0656 0.7778 0.1313
0.3101 0.1832 0.4328 0.1278

0.0775 0.2625 0.8889 0.2625


0.1551 0.3664 0.1082

A = 0.0916
0.3101 0.1313 0.7778 0.1313 0.3101 0.0916
0.1082 0.3664
0.1551 0.2625 0.8889 0.2625 0.1775

0.3101 0.1313 0.7778 0.656


0.1278 0.4328 0.1832
0.1510 0.5113 0.2164 0.3664
0.1551 0.2625 0.9444

and

0
0.0278

0.1111

b = 0.25
0.4444

0.6944
1

Solve for u ( xi ) using Gaussian Elimination, we have:


u ( x0 ) = 1.2138; u ( x1 ) = 1.0223; u ( x2 ) = 0.8805; u ( x3 ) = 0.7435;
u ( x4 ) = 0.6340; u ( x5 ) = 0.5225; u ( x6 ) = 0.4283;

This result is plotted in Figure 1.

For m = 8 , h =

ba 1
= , the interval is divided as follow:
m
8

Once again, using Equation (8), assuming the error

b a 4 ( 4)
h f ( ) 0 , we can have the
180

following set of linear equations:


K ( xi , x0 ) u ( x0 ) + 4 K ( xi , x1 ) u ( x1 ) + 2 K ( xi , x2 ) u ( x2 ) + "

1
u ( xi ) = f ( xi ) + 4 K ( xi , x3 ) u ( x3 ) + 2 K ( xi , x4 ) u ( x4 ) + 4 K ( xi , x5 ) u ( x5 ) + " (13)
18

2 K ( xi , x6 ) u ( x6 ) + 4 K ( xi , x7 ) u ( x7 ) + K ( xi , x8 ) u ( x8 )

1
2
3
4
5
6
7
For x0 = 0, x1 = , x2 = , x3 = , x4 = , x5 = , x6 = , x7 = , x8 = 1 , we can solve for
8
8
8
8
8
8
8
u ( x0 ) ," , u ( x8 ) by solving the following linear system:

u ( x0 ) = 0.0417u ( x0 ) + 0.1889u ( x1 ) + 0.1070u ( x2 ) + 0.2425u ( x3 ) + 0.1374u ( x4 ) + 0.3114u ( x5 ) + 0.1764u ( x6 ) + 0.3998u ( x7 ) + 0.1133u ( x8 )

u ( x1 ) = 0.0472u ( x0 ) + 0.1667u ( x1 ) + 0.0994u ( x2 ) + 0.2140u ( x3 ) + 0.1212u ( x4 ) + 0.2748u ( x5 ) + 0.1557u ( x6 ) + 0.3528u ( x7 ) + 0.1000u ( x8 ) + 0.0156

u ( x2 ) = 0.0535u ( x0 ) + 0.1889u ( x1 ) + 0.0833u ( x2 ) + 0.1889u ( x3 ) + 0.1070u ( x4 ) + 0.2425u ( x5 ) + 0.1374u ( x6 ) + 0.3114u ( x7 ) + 0.0882u ( x8 ) + 0.0625

u ( x3 ) = 0.0606u ( x0 ) + 0.2140u ( x1 ) + 0.0944u ( x2 ) + 0.1667u ( x3 ) + 0.0944u ( x4 ) + 0.2140u ( x5 ) + 0.1212u ( x6 ) + 0.2748u ( x7 ) + 0.0778u ( x8 ) + 0.1406

u ( x4 ) = 0.0687u ( x0 ) + 0.2425u ( x1 ) + 0.1070u ( x2 ) + 0.1889u ( x3 ) + 0.0833u ( x4 ) + 0.1889u ( x5 ) + 0.1070u ( x6 ) + 0.2425u ( x7 ) + 0.0687u ( x8 ) + 0.2500
u ( x5 ) = 0.0778u ( x0 ) + 0.2748u ( x1 ) + 0.1212u ( x2 ) + 0.2140u ( x3 ) + 0.0944u ( x4 ) + 0.1667u ( x5 ) + 0.0944u ( x6 ) + 0.2140u ( x7 ) + 0.0606u ( x8 ) + 0.3906

(14)

u ( x6 ) = 0.0882u ( x0 ) + 0.3114u ( x1 ) + 0.1374u ( x2 ) + 0.2425u ( x3 ) + 0.1070u ( x4 ) + 0.1889u ( x5 ) + 0.0833u ( x6 ) + 0.1889u ( x7 ) + 0.0535u ( x8 ) + 0.5625

u ( x7 ) = 0.1000u ( x0 ) + 0.3528u ( x1 ) + 0.1557u ( x2 ) + 0.2748u ( x3 ) + 0.1212u ( x4 ) + 0.2140u ( x5 ) + 0.0944u ( x6 ) + 0.1667u ( x7 ) + 0.0472u ( x8 ) + 0.7656
u ( x8 ) = 0.1133u ( x0 ) + 0.3998u ( x1 ) + 0.1764u ( x2 ) + 0.3114u ( x3 ) + 0.1374u ( x4 ) + 0.2425u ( x5 ) + 0.1070u ( x6 ) + 0.1889u ( x7 ) + 0.0417u ( x8 ) + 1

Write in the form of Au = b , we have:


0.9583 0.1889 0.1070 0.2425 0.1374 0.3114 0.1764 0.3998 0.1133
0.0472 0.8333 0.0944 0.2140 0.1212 0.2748 0.1557 0.3528 0.1000

0.0535 0.1889 0.9176 0.1889 0.1070 0.2425 0.1374 0.3114 0.0882

0.0606 0.2140 0.0944 0.8333 0.0944 0.2140 0.1212 0.2748 0.0778


A = 0.0687 0.2425 0.1070 0.1889 0.9167 0.1889 0.1070 0.2425 0.0687

0.0778 0.2748 0.1212 0.2140 0.0944 0.8333 0.0944 0.2140 0.0606


0.0882 0.3114 0.1374 0.2425 0.1070 0.1889 0.9167 0.1889 0.0535

0.1000 0.3528 0.1557 0.2748 0.1212 0.2140 0.0944 0.8333 0.0472

0.1133 0.3998 0.1764 0.3114 0.1374 0.2425 0.1070 0.1889 0.9583

and

0
0.0156

0.0625

0.1406
b = 0.2500

0.3906
0.5625

0.7656

1.000

Solve for u ( xi ) using Gaussian Elimination, we have:


u ( x0 ) = 1.2062; u ( x1 ) = 1.0614; u ( x2 ) = 0.9466; u ( x3 ) = 0.8361; u ( x4 ) = 0.7445;
u ( x5 ) = 0.6529; u ( x6 ) = 0.5735; u ( x7 ) = 0.5735; u ( x8 ) = 0.4927; u ( x9 ) = 0.4209;

Now, we plot all the solution that we have obtain thus far on the same graph, as shown in
Figure 1.

Figure 1: Approximation of u(x) given in Problem 1 using different methods: Trapezoidal rule, and
Composite Simpson rule with m=4, 6, and 8.

PROBLEM 1 DISCUSSION:

Since there is not exact solution given in the problem, we cant compare the error
between each method and concluded that which method is superior. However, as more
points are used in the approximation in a given method, we could expect it approach the
true solution nonetheless. From the result shown in Figure 1, we can see that the
Trapezoidal rule only evaluates two points and thus we would expect it is least accurate
method (assuming that the given solution of the function, Equation (1) is nonlinear). For
Composite Simpsons rule, different number of points was being used and we can see
that as the number of points used increase, the solution curve become smooth and we can
expect it approaches the true solution.
In the process of approximating the integral using different method thereby forming
different set of linear system, we omitted the error terms associated with each method, as
mentioned in the formulation. These errors, which is a function of h , the interval size is
approximated to zero if sufficiently small h is used. However, for the Trapezoidal rule,
the h = 1 , hence the error (h3 / 2) f " ( ) is considerable large compare to the error from
the Composite Simpsons rule, ( b a ) h 4 /180 f ( 4) ( ) . Thus we could expect the
result obtained from the trapezoidal rule is away from the rest of the results obtained
using Composite Simpsons rule, as seen in Figure 1.

PROBLEM 2:
Part A. The ( m 1) ( m 1) tridiagonal matrix:

0 "
0

1 2

1 2 % %
#

A= 0
0
%
% %


%
% %
#
0
0 1 2
"

(15)

is involved in the Forward Difference Method to solve the heat equation. For the stability
of the method we need ( A ) < 1 . With m = 11 , approximate the eigenvalues of A for
1
1
each of the following: = ; = ;
4
2

3
4

= . when is the method stable?

Part B. The actual eigen values of the matrix A is given by:

i = 1 4 sin
,
2m

for i = 1," , m 1

(16)

Compare with the approximations in part A, and again, when is the method stable?

SOLUTION:
Since A is a symmetric, tridiagonal matrix, to approximate the eigen values in matrix A ,
QR algorithm is implemented. The QR algorithm is given as follow:

QR algorithm:
Step 1: Given square matrix A, form its QR factorization as: A1 = Q1 R1 ;
Step 2: Define: A1 = R1Q1 .
Step 3: Continue this process for k 1 :
Ak = Qk Rk
Ak +1 = Rk Qk
Step 4: Continue this until the A matrix become diagonal matrix. Or the off diagonal
terms becomes less then tolerance, say 1106 .
As the QR method is slow in convergence, an accelerated method which modified the
above algorithm is implemented. This method is explained as follow:
Define:

1
A=0

#
0

1 0
2 %

"

% %
% %

%
%

"

n 1

0
#
0

n 1
n

Step 1: Factorized Qk Rk by Ak n I = Qk Rk ;
Step 2: Determine Ak +1 = Rk Qk + n I ;
Step 3: When n 1 is sufficiently small, set n = n and delete the last row and column of
A and continue the Step 1 and Step 2 with the smaller matrix.
For this question, the QR method and accelerated QR method were coded to solve the
given problem, and the result was then compared with the one obtained using Equation
(16).

Stability issue for forward difference method used in solving heat equation:
From the textbook, in order for the forward difference method to be stable, the
requirement is ( A ) 1 . ( A ) is define as the spectral radius of matrix A and is define
by ( A ) = max , where is the eigen value of A . Since the actual eigen values of A
is given by Equation (16), The condition for stability reduces to determine whether:

( A ) = max 1 4 sin
1i m 1

1
2m

(17)

Which further simplifies to:


2

i 1
0 sin
,
2
2m

for i = 1," , m 1.

(18)

Since the stability requires that this inequality (18) condition hold as h 0 ( h is the
interval size), or equivalently, as m . The fact that:
2

i
lim sin
= 1
m
2m

(19)

Substitute Equation (19) into Inequality (18), implies that the stability requirement is:
0

1
2

(20)

1
1
3
Hence, in this problem, only = ; = will give stable result but not = .
4
2
4

Convergence of QR Algorithm:
While stability issues concerning the accuracy of the forward difference method in
solving the heat equation, there is a much important issue in solving for the eigen values
of a tridiagonal matrix using QR algorithm The convergence criteria for this method.
Let A be real n n and eigen values {i } is given by:

1 > 2 > " > n > 0

(21)

The iteration will converge to an upper triangular matrix with {i } as diagonal elements.
Further, if A is symmetric, as in our case, the sequence { Ai } converges to a diagonal
matrix. Equation (21) requires that in order for QR algorithm to converge, the eigen
values of matrix A needs to have different magnitude.
Hence, in this problem, where only = 1/ 4 and = 3 / 4 converged in the QR algorithm,
where = 1/ 2 have does not converge to obtain eigen values. By using MATLAB eig()
function, we obtained the eigen values for = 1/ 2 case, they are in fact repeated eigen
values, hence no convergence can be guaranteed by using QR algorithm.
RESULT:
The QR algorithm is coded in MATLAB, in which the QR factorization process is
provided by the MATLAB function qr().

For = 1/ 4 case:
Note that the tolerance for convergence is set to 1106 (the off diagonal terms of
matrix A ), and the number of iterations used to achieve this tolerance is 188. For this
tolerance value, the error between the exact solution and the one obtained using QR
algorithm is calculated, the errors are in the magnitude of 1010 .
Eigen Values

1
2
3
4
5
6
7
8

QR Algorithm
0.97974648679230

Exact Solution
0.97974648680725

Error( 1010 )
0.14948042803553

0.92062676643054

0.92062676641559

0.14945933379806

0.82743036697264

0.82743036697264

0.00001443289932

0.70770750650094

0.70770750650094

0.00002775557562

0.57115741913664

0.57115741913664

0.00000777156117

0.42884258086336

0.42884258086336

0.00000333066907

0.29229249349906

0.29229249349906

0.00000222044605

0.17256963302736

0.17256963302736

0.00000055511151

9
10

0.07937323358441

0.07937323358441

0.00000069388939

0.02025351319275

0.02025351319275

0.00000006938894

For = 1/ 2 case:
As mentioned earlier, QR algorithm cannot guarantee convergence if the eigen values are
not of different magnitude. i.e., repeated unsigned eigen values exist, this is the case for
both = 1/ 2 and = 1/ 4 . For = 1/ 2 , the simple QR algorithm that implemented here
does not converge. Hence, in this case, the eigen values were calculated using the
MATLAB eig(), which uses a much improved QR algorithm (by Wilkinson) to obtain the
eigen values. The result is shown below and error is computed.
Eigen Values
MATLAB eig()
Exact Solution
Error ( 1015 )
0.95949297361450
0.95949297361450
0.11102230246252
1

2
3
4
5
6
7
8
9
10

0.84125353283118

0.84125353283118

0.22204460492503

0.65486073394529

0.65486073394529

0.11102230246252

0.41541501300189

0.41541501300189

0.27755575615629

0.14231483827329

0.14231483827329

0.22204460492503

-0.14231483827329

-0.14231483827329

0.47184478546569

-0.41541501300189

-0.41541501300189

0.16653345369377

-0.65486073394529

-0.65486073394528

0.11102230246252

-0.84125353283118

-0.84125353283118

0.66613381477509

-0.95949297361450

-0.95949297361450

0.00000000000000

For = 3 / 4 case:
The tolerance for convergence is set to 1106 (the off diagonal terms of matrix A ), and
the number of iterations used to achieve this tolerance is 195. For this tolerance value, the
error between the exact solution and the one obtained using QR algorithm is calculated,
the errors are in the magnitude of 1012 .
Eigen Values
QR Algorithm
Exact Solution
Error( 1012 )
-1.93923946042174
-1.93923946042175
0.00088817841970
1

2
3
4
5
6
7
8
9
10

-1.76188029924678

-1.76188029924677

0.00444089209850

-1.48229110091793

-1.48229110091793

0.00377475828373

-1.12312251950282

-1.12312251950283

0.00488498130835

-0.71347225740928

-0.71347225740993

0.64248606435058

-0.28652774259007

-0.28652774259007

0.00016653345369

0.12312251950283

0.12312251950283

0.00011102230246

0.48229110091793

0.48229110091793

0.00016653345369

0.76188029924613

0.76188029924677

0.64315219816535

0.93923946042175

0.93923946042175

0.00088817841970

PROBLEM 2 DISCUSSION:

In this problem, we implemented the QR algorithm to find the eigen values of a


tridiagonal symmetric sparse matrix that obtain from solving the heat equation using
forward difference method. There are two important questions addressed here: the
stability of the approximation using forward difference method to solve the heat equation
and the convergence criteria of the simple QR algorithm that we implemented here.
The first question, concerning the stability issue of the forward difference method in
solving the heat equation, does not cause any implementation issue for the QR algorithm
that we used here. It gives the boundary value for , in which the forward difference
method is stable in solving the heat equation. This requires that 0 0.5 , as shown in
Equation (20). Thus only = 1/ 2 and = 1/ 4 give a correct solution to the heat
equation.
The second question, which is more of our interest, concerning the convergence criteria
of the implemented QR algorithm. From literature, the simple form of the OR algorithm
will guarantee convergence in the case where all the eigen values are of different
magnitude. If there are eigen values that are of same magnitude, the method does not
guarantee convergence. This is the case in for = 3 / 4 and = 1/ 2 , in which some of
the eigen values are same magnitude. In these two cases, only = 3 / 4 case gives
convergence result but not = 1/ 2 . On the other hand, for = 1/ 4 , all the eigen values
are of different magnitude and thus we could expect convergence in this case, and this is
in fact the case.
The accelerated QR algorithm is also implemented in solving this problem, unfortunately
no convergence obtained for all the cases. For all the cases ( = 1/ 2 , = 1/ 4 , and
= 3 / 4 ), the n 1 does not reduce to the tolerance value of 1106 . So no convergence
can be obtained. As a result, we are not able to see how the accelerated QR algorithm
converges in fewer steps compare to the simple QR algorithm that we implemented here.
Nonetheless, we included the code for accelerated QR algorithm for reference. For
= 1/ 4 and = 3 / 4 , the iterations needed to converge to a tolerance of 1 106 is 188
and 195 steps, repectively.

PROBLEM 3:
Part A. The deflection of a beam with supported ends subject to uniform loading. The
boundary-value problem governing this physical situation is:

d 2w S
qx
=
w+
( x l ),
2
2 EI
dx
EI

0< x<l

(22)

with boundary condition w ( 0 ) = w ( l ) = 0 .


Given the beam is a W10-type steel I-beam with the following characteristics:
Length, l = 120 in; intensity of uniform, q = 100 lb/ft = 100 /12lb/in. ; modulus of
elasticity, E = 3 107 lb / in 2 ; stress at end, S = 1000lb ; central moment of inertia,
I = 625 in 4 .
a. Approximately the deflection of the beam every 6 in.
b. The actual relationship is given by:
w ( x ) = c1e ax + c2 e ax + b( x l ) + c

(23)

where
c1 = 7.7042537 104 ; c2 = 7.9207462 104 ; a = 2.3094010 104 ; b = 4.1666666 103 ;

c = 1.5625 105 . Is the maximum error on the interval within 0.2 in?
c. State law requires that max 0< x <l w( x) < 1/ 300 . Does this beam meet state code?
Part B. A more appropriate representation of the curvature gives the differential equation:
1 + ( w ' ( x ) )2

3/ 2

d 2w S
qx
=
w+
( x l ),
2
dx
EI
2 EI

0< x<l

(24)

Approximate the deflection of the beam every 6 in. and compare the results to problem 2.

SOLUTION:
A. The finite-difference method for a linear second-order boundary-value problem:
y " = p ( x ) y '+ q ( x ) y + r ( x ) ,

a x b,

y ( a ) = , y (b) =

(25)

Can be solved by using finite difference method. For N interior mesh points, using
centered-difference formula, and ignore the truncation error, one can solve for the N
interior mesh point wi by solving the linear system:
Aw = b

(26)

where:
h

2
"
0
2 + h q ( x1 ) 1 + 2 q ( x2 ) 0

1 h q ( x ) 2 + h 2 q ( x ) %

%
#
i
2

and
A=
%
%
%
0
0

h
1 + q ( xN 1 )
#
%
%
%

2
"
0
0 1 q ( xN 1 ) 2 + h q ( xN )

2
h r ( x1 ) + 1 + 2 p ( x1 ) w0
w1

w
h 2 r ( x2 )

# .
b=
and
=
w
#

wN 1
h 2 r ( xN 1 )

wN
h 2 r ( x ) + 1 + h p ( x ) w
N
N N +1

Compare Equation (22) with Equation (25), we have the following relations:
p ( x ) = 0,

q ( x) =

S
,
EI

r ( x) =

qx
(x l)
2 EI

(27)

The linear finite difference method was implemented, with N = 20 and h = 6 (as required
by question) and Equation (28) was then solved in MATLAB. On the other hand, in order
to obtain better accuracy, we also solve the same problem with N = 40 and h = 3 ; in
addition to N = 60 and h = 1 . All these results are then plotted on the same graph and
compare with the exact solution obtained using Equation (23).
To study the errors in these methods, the total errors at each point of interest between the
approximation obtained using finite difference method and the exact solution were
calculated by adding square of the difference at each point using the following equation:
N

Error = ( wi ) Approx ( wi ) Actual

i =1

(29)

RESULT:
The calculated deflection of the beam w( x) is plotted using the linear finite difference
method for different number of N intervals were obtained and plotted in Figure 2
( N =20) , Figure 3 ( N =40) and Figure 4 ( N =60). Compared to the exact solution from
Equation (23), the error is in the order of 103 inch.

Figure 2: Solution obtained from solving the linear BVP problem using nonlinear finite difference
method, with N=20.

Figure 3: Solution obtained from solving the linear BVP problem using nonlinear finite difference
method, with N=40.

Figure 4: Solution obtained from solving the linear BVP problem using nonlinear finite difference
method, with N=60.

The deflection of the beam given by Equation (23) has values from negative to positive,
which does not make any physical sense. Since the given load is a uniform load across
the beam, and the two end where fixed at the boundary value (which is zero), the
deflection should not have values fluctuated between positive and negative values. Hence,
there must be a mistake in the given Equation (23). Nonetheless, Equation (23) is used to
calculate the error. We can see that the exact deflection plotted using Equation (23) is off
by 1 103 inch. If we added this value in Equation (23) to correct the deflection formula,
the errors shown in all the figures here were reduced to 1 106 inch.
Next, we will approximate the nonlinear equation of the deflection formula given in
Equation (24) by using nonlinear finite difference method and compare with the result
that we obtained in this section.

Part B. The nonlinear second-order boundary-value problem of the form:


y " = f ( x, y , y ' ) ,

a x b,

y (a) = , y (b) =

(30)

can be solved using similar approach as the linear finite difference method (so called
Nonlinear finite difference method). However, since the resulting system of equation is
not linear, a iterative process was then implemented to determine the solution.
Equation (30), when solve using centered-difference method, a N N nonlinear system
can be obtained as follow:
w

2w1 + w2 + h 2 f x1 , w1 , 2
= 0,
2h

w1 + 2 w2 w3 + h 2 f x2 , w2 , 3
= 0,
2h

(31)

w wN 2

wN 2 + 2 wN 1 wN + h 2 f xN 1 , wN 1 , N
= 0,
2h

wN 1

wN 1 + 2wN + h 2 f xN , wN ,
=0
2h

Newtons method for nonlinear system is used to approximate the solution to this system.
The solution is iterated and will converge to the solution of the system provided that the
initial approximation of the solution is close to the solution and the Jacobian matrix for
the system is nonsingular. The Jacobian of the system of Equation (31) is tridiagonal with
the entry expressed as:

wi +1 wi 1
h
,
1 + 2 f y ' xi , wi ,
2h

w wi 1

J ( w1 ," , wN )ij =
2 + h 2 f y xi , wi , i +1
,
2
h

w wi 1
h
1 f y ' xi , wi , i +1
,
2
2h

where w0 = and wN +1 = .

for i = j 1 and j = 2," , N ,


for i = j and j = 1," , N ,

(32)

for i = j + 1 and j = 1, ", N 1,

Newton method then required that at each iteration, the N N nonlinear system be
solved for v1 , v2 ,", vN , since:
wik = wik 1 + vi ,

for each i = 1, 2,", N

(33)

Following the steps, re-write Equation (24) in the following form:


2 3/ 2 S
d 2w
w + qx ( x l ) ,
1
'
w
x
=
+
(
)
(
)
2

EI

2 EI
dx

0< x<l

(34)

In which, compare to Equation (30), we have:


2
f ( x, w, w ') = 1 + ( w ' ( x ) )

3/ 2

qx
S
( x l )
w+
2 EI
EI

(35)

The partial derivative of f ( x, w, w ' ) with respect to w is given by:


f w ( x, w, w ') =

S
EI

1 + ( w ' ( x ) )2

3/ 2

(36)

The partial derivative of f ( x, w, w ' ) with respect to w ' is given by:


f w ' ( x, w, w ') =

2 1/ 2 S
3
qx
2 w ' ( x ) 1 + ( w ' ( x ) ) w +
( x l )

2
2 EI
EI

(37)

This nonlinear finite difference method was then coded in MATLAB, by using the
solution obtained from part A (the linear case) as the initial guess. Similarly, the solutions
were obtained using different number of interior point N , and the error was computed. In
addition, the solutions were then compared with those obtained from part A.

RESULT:
The nonlinear finite difference method for solving this problem is coded in MATLAB.
The initial value used in this method is the result obtained from Part A since Newton
iteration need the initial guess to be close to the solution. Similarly, different number of
intervals ( N ) are used in the computation. The tolerance used in the iteration process is
1 106 .

The results obtained using the nonlinear finite difference method are plotted in the
subsequent figures. Figure 5 shows the result for N = 20 , Figure 6 shows the result when
N = 40 is used, and Figure 7 shows the result for N = 60 . Finally, Figure 8 shows the
error between the approximated deflection and the actual deflection for both linear and
nonlinear case.

Figure 5: Solution obtained from solving the nonlinear BVP problem using nonlinear finite
difference method, with N=20.

Figure 6: Solution obtained from solving the nonlinear BVP problem using nonlinear finite
difference method, with N=40.

Figure 7: Solution obtained from solving the nonlinear BVP problem using nonlinear finite
difference method, with N=60.

Figure 8: Comparing the error for different number of interval N used in the approximation, for
both linear and nonlinear finite difference method.

PROBLEM 3 DISCUSSION:

From Figure 5, Figure 6, and Figure 7, we can see that there is no significant difference in
the solution obtained using different number of N . The errors in all three cases ( N = 20 ,
N = 40 , N = 60 ) are all in the order of 103 . However, if we compare the errors in the
same plot, as shown in Figure 8, we can see that using higher number of intervals reduce
the errors between the approximated deflection and the actual deflection. This is same for
both either the linear equation of Equation (22) is used or the nonlinear equation of
Equation (24) is used.
As we mention before, as a beam with uniform loading and two fixed end, the deflections
w ( x ) cannot change its value from negative value to positive value. It should be either
all positive values or all negative values (depending on where to measure from: -y or y in
Figure 9 ).

Figure 9: The beam deflection mode due to uniform distributed load with two fix boundary.

Hence, the actual deflection given by Equation (23) does not have any physical meaning
as the value changes from negative to positive for difference value of x .
On the other hand, we do not observed any significant improvement when the nonlinear
second order equation of (24) is used to approximate the actual deflection over the linear
second order equation of (22). The errors compare to the actual deflection equation are
same in both cases. Hence, Equation (22) is adequate to approximate the deflection while
it is less computational expensive.
Last, let us answer the question: State law requires that max 0< x <l w( x) < 1/ 300 , does the
beam deflection meet the requirement? From our results, we see that the maximum
deflection (at x = 60 inch ) is approximately 1.3 103 inch . Compare to 1/ 300 inch
= 3.333 103 inch , we can safely say that the beam deflection met the state law.

CONCLUSION:

In this project, we investigate the numerical methods involved in: solving system of
linear equations; solving eigen values of a tridiagonal symmetric matrix resulting from
solving the heat equation using the forward difference method; and solving second order
boundary value problem (both linear and nonlinear) using finite difference method.
We solve the linear system of equations that result from approximating a integral using
Trapezoidal rule and Composite Simpsons rule. For Composite Simpson rule, different
number of points is used. We then solve the linear system of equations using Gaussian
Elimination method to obtain the approximated solution of the given integral. The final
result is shown graphically.
In solving the eigen values problem, we study the stability of the forward difference
method and also study the convergence criteria for the QR algorithm. The stability
required that 0 0.5 while the convergence criteria required that all the eigen values
are of different values. We then show that the QR algorithm only converges for the case
where = 1/ 4 and = 3 / 4 , the iterations needed to converge to a tolerance of 1106 is
188 and 195 steps. On the other hand, for = 1/ 2 , convergence cannot achieve since
the eigen values are not of different magnitude.
In the last problem, we solve a beam deflection problem by solving the linear and
nonlinear deflection model of the beam. The deflection problem form a second order
boundary value problem and thus can be solved by using the finite difference method.
The linear and nonlinear finite difference method is implemented to obtain the beam
deflection at different beams locations. The result was then compare with the actual
beam deflection, which is given as a formula. Although there is doubt on the actual beam
deflection formula (as discussed in problem 3 discussion), we used it to calculate the
error between the one obtained from finite difference method and the actual beam
deflection equation. It is then show that the error is of the order of 1 103 . Finally, we
also determine that the maximum beam deflection is about 1.3 103 inch , and this
deflection value meet the state law, which required the maximum deflection to be within
3.333 103 inch .
In conclusion, this project gives us an in depth study of each of the numerical methods
implemented to solve the problems. It also shows us the advantages and limitations, the
computational cost, and the accuracy of each method.
Finally, the code(s) implemented in this project in attached as appendix for reference.

D:\usr\llee3\CourseWorks\MTH538-NumericalAnaly...\Problem1_n4.m
Page 1
April 20, 2006
12:54:17 PM
% ----------------------------------------------------------------------% Name: Leng-Feng Lee
% Course: MTH 538 Numerical Analysis
% Final Project - Problem 1
% Description: Approximate integral using Trapezoidal rule, Composite
%
Simpson rule with n=4,6,8. Results plotted
% ----------------------------------------------------------------------clear all
close all
clc
% m = 1, solve using Trapezoidal Rule.
x0=linspace(0,1,2);
t0=linspace(0,1,2);
for i=1:1:length(x0),
UO(i)=0.5*exp(abs(x0(i)-t0(1)));
U1(i)=0.5*exp(abs(x0(i)-t0(2)));
end
A0=[UO' U1']-eye(2)
b0=(-x0.^2)'

% Form Au = b

UU0=A0\b0 % <--- Solve using Gaussian Elimination


% m = 4, solve using Composite Simpson's rule:
x1=linspace(0,1,5);
t1=linspace(0,1,5);
for i=1:1:length(x1),
U0(i)=(1/12)*exp(abs(x1(i)-t1(1)));
U1(i)=(1/12)*4*exp(abs(x1(i)-t1(2)));
U2(i)=(1/12)*2*exp(abs(x1(i)-t1(3)));
U3(i)=(1/12)*4*exp(abs(x1(i)-t1(4)));
U4(i)=(1/12)*exp(abs(x1(i)-t1(5)));
end
A1=[U0' U1' U2' U3' U4']-eye(5)
b1=(-x1.^2)'

% Form Au = b

UU1=A1\b1 % <--- Solve using Gaussian Elimination

% m = 6, solve using Composite Simpson's rule:


x2=linspace(0,1,7);
t2=linspace(0,1,7);
for i=1:1:length(x2),
U0(i)=(1/18)*exp(abs(x2(i)-t2(1)));
U1(i)=(1/18)*4*exp(abs(x2(i)-t2(2)));
U2(i)=(1/18)*2*exp(abs(x2(i)-t2(3)));
U3(i)=(1/18)*4*exp(abs(x2(i)-t2(4)));
U4(i)=(1/18)*2*exp(abs(x2(i)-t2(5)));
U5(i)=(1/18)*4*exp(abs(x2(i)-t2(6)));
U6(i)=(1/18)*exp(abs(x2(i)-t2(7)));
end
A2=[U0' U1' U2' U3' U4' U5' U6']-eye(7) % Form Au = b
b2=(-x2.^2)'
UU2=A2\b2 % <--- Solve using Gaussian Elimination

D:\usr\llee3\CourseWorks\MTH538-NumericalAnaly...\Problem1_n4.m
Page 2
April 20, 2006
12:54:17 PM
% m = 8, solve using Composite Simpson's rule:
x3=linspace(0,1,9);
t3=linspace(0,1,9);
for i=1:1:length(x3),
U0(i)=(1/24)*exp(abs(x3(i)-t3(1)))
U1(i)=(1/24)*4*exp(abs(x3(i)-t3(2)))
U2(i)=(1/24)*2*exp(abs(x3(i)-t3(3)))
U3(i)=(1/24)*4*exp(abs(x3(i)-t3(4)))
U4(i)=(1/24)*2*exp(abs(x3(i)-t3(5)))
U5(i)=(1/24)*4*exp(abs(x3(i)-t3(6)))
U6(i)=(1/24)*2*exp(abs(x3(i)-t3(7)))
U7(i)=(1/24)*4*exp(abs(x3(i)-t3(8)))
U8(i)=(1/24)*exp(abs(x3(i)-t3(9)))
end
A3=[U0' U1' U2' U3' U4' U5' U6' U7' U8']-eye(9)
b3=(-x3.^2)'

% Form Au = b

UU3=A3\b3 % <--- Solve using Gaussian Elimination


% Plot all the result on the same graph
plot(x0,UU0','k','LineWidth',2); hold on
plot(x1,UU1','b','LineWidth',2);
plot(x2,UU2',':r','LineWidth',2);
plot(x3,UU3','m','LineWidth',2);
plot(x0,UU0','*k'); hold on
plot(x1,UU1','*b');
plot(x2,UU2','*:r');
plot(x3,UU3','*m');
xlabel('x position', 'fontweight','bold');
ylabel('u(x) value', 'fontweight','bold');
title('Approximate U(x) using different approaches', 'fontweight','bold');
legend('Trapizoidal rule', 'Composite Simpson with m=4', 'Composite Simpson with m=6', 'C
omposite Simpson with m=8');

D:\usr\llee3\CourseWorks\MTH538-NumericalAna...\QR_Algorithm2.m
Page 1
April 20, 2006
12:52:47 PM
% ----------------------------------------------------------------------% Name: Leng-Feng Lee
% Course: MTH 538 Numerical Analysis
% Final Project-Problem 2
% Description: Solving Eigen values using QR Algorithm.
% ----------------------------------------------------------------------close all
clear all
clc
alpha = 3/4;
n=10;
x=1:1:10;
x=x./x
a = (1-2*alpha)*x; % Value on the diagonal
b = alpha*x(1:9);
% Value on the off diagonal
c = alpha*x(1:9);
%Create the tridiagonal matrix:
A=diag(a) + diag(alpha*ones((n-1),1),1) + diag(alpha*ones(n-1,1),-1)
eig(A)
Max =1000;
TOL=1e-6;
ind=1;
for k=1:1:Max,
supind=1;
subind=1;
ind=1;
%[Q,R] = qr(A-diag(a));
[Q,R] = qr(A);
A=R*Q;

temp=size(A);
for i=1:1:temp(1,1),
for j=1:1:temp(1,1),
if (i == j && i < n)
% Supper diagonal of A
supA(supind)=A(i,j+1);
supind=supind+1;
end
if (i == j && i > 1)
% Subdiagonal of A
subA(subind)=A(i,j-1);
subind=subind+1;
end
end
end
k
% Check for convergence:
if (normVec(subA)<TOL && normVec(supA) <TOL) % If the off diagonal term less than TOL
disp('Solution within tolerence, eigen values are');
sol=diag(A)
disp('# of iterations =');
k
break;
end
%pause

D:\usr\llee3\CourseWorks\MTH538-NumericalAna...\QR_Algorithm2.m
Page 2
April 20, 2006
12:52:47 PM
end
sol=diag(A); % Solution
%sol=eig(A); % Solution for alpha = 1/2
% sol2=[sol(1:4,1);
%
sol(7,1);
%
sol(9,1);
%
sol(10,1);
%
sol(8,1);
%
sol(6,1);
%
sol(5,1)]; % For alpha =3/4 arrange properly to calculate the error.
% Calculate the exact eigenvalues using the fomula:
i=10:-1:1;
t=sin(pi*i/(2*(n+1)));
lamda=1-4*alpha*t.^2;
%Calculate the error:
Error=((sol2'-lamda).^2).^0.5;

D:\usr\llee3\CourseWorks\MTH538-N...\Accelerated_QR_Algorithm.m
Page 1
April 20, 2006
12:53:23 PM
%
%
%
%
%
%
%

----------------------------------------------------------------------Name: Leng-Feng Lee


Course: MTH 538 Numerical Analysis
Course Project-Problem 2
Description: Find eigen value of a square matrix using Accelerated
OR Algorithm.
-----------------------------------------------------------------------

close all
clear all
clc
% Form the Tridiagonal matrix:
alpha = .5;
n=10;
x=1:1:10;
x=x./x;
a = (1-2*alpha)*x; % Value on the diagonal
b =alpha*x(1:9);
% Value on the off diagonal
A=gallery('tridiag',b, a, b);

eig(A)
Max =50;
TOL=1e-3;
for k=1:1:Max,
supind=1;
subind=1;
ind=1;
[Q,R] = qr(A);
A=R*Q;
% Extract the supper diagonal and subdiagonal of A
for i=1:1:n,
for j=1:1:n,
if (i == j && i < n)
% Supper diagonal of A
supA(supind)=A(i,j+1);
supind=supind+1;
end
if (i == j && i > 1)
% Subdiagonal of A
subA(subind)=A(i,j-1);
subind=subind+1;
end
if (i==j),
EigenValues(ind) = A(i,j);
ind=ind+1;
end
end
end
% Check for convergence:
if (normVec(subA)<TOL && normVec(supA) <TOL)
disp('Solution within tolerence, eigen values are');
disp('# of iterations =');
k

D:\usr\llee3\CourseWorks\MTH538-N...\Accelerated_QR_Algorithm.m
Page 2
April 20, 2006
12:53:23 PM
k=Max;
end
k
%pause
end

D:\usr\llee3\CourseWorks\MTH538-NumericalAn...\Problem3_Prob7.m
Page 1
April 20, 2006
12:54:54 PM
%
%
%
%
%
%

----------------------------------------------------------------------Name: Leng-Feng Lee


Course: MTH 538 Numerical Analysis
Final Project-Problem 3 Part A
Description: Solve linear second order PDE using Linear Finite Method.
-----------------------------------------------------------------------

close all
clear all
clc
% NonLinear Finte-Difference Method
% Solve the following Nonlinear Second order ODE:
%
% y" = p(x)*y' + q(x)*y + r(x), a<=x<=b, y(a)=alpha, y(b)=bete
%
%Equation Coefficients:
S=1000;
%lb
E=3e7;
%lb/in^2
q=100/12;
%lb/ft -> convert to lb/in
I=625;
%in^4
a=0;
%boundary value
b=120;
%boundary value
alpha=0;
beta=0;
%Method variables:
N=20; % Numbers of intervals
x=linspace(0,120,N+1);
h=(b-a)/(N);
% step size
% Solve the Aw = b system, now form the A matrix and b vector:
for i=1:1:N-1,
xi = a+i*h;
q_x=S/(E*I);
%q(x) not depends on x
p_x=0;
%p(x)=0, no first order term.
r_x=q*xi*(xi-b)/(2*E*I); %r(x) term
% Calculate the diagonal term:
diag(i)=2+h^2*q_x;
% Calculate the supper diagonal term:
if (i < N-1),
supdiag(i)=-1+(h/2)*p_x;
end
% Calculate the sub diagonal term:
if (i > 1),
subdiag(i-1)=-1-(h/2)*p_x;
end
% Calculate the b vector in Ay=b:
if (i == 1),
bvec(i) = -h^2*r_x + (1+(h/2)*p_x)*alpha;
elseif (i == N-1),

D:\usr\llee3\CourseWorks\MTH538-NumericalAn...\Problem3_Prob7.m
Page 2
April 20, 2006
12:54:54 PM
bvec(i)=-h^2*r_x + (1-(h/2)*p_x)*beta;
else
bvec(i)=-h^2*r_x;
end
end
% Now form the tridiagonal Sparse matrix A:
A=gallery('tridiag',subdiag, diag, supdiag);
% Calculate w using Gaussian Elimination:
w=A\bvec'
wfull=[0 w' 0];
%Create the actual w(x) based on Eqn.
c1=7.7042537e4;
c2=7.9207462e4;
a1=2.3094010e-4;
b1=-4.1666666e-3;
d1=-1.5625e5;
wactual = c1*exp(a1*x) + c2*exp(-a1*x) + b1*(x-b).*x + d1 +1e-3;
% wfull-wactual
for i=1:length(wfull),
Error(i) = ((wfull(i)-wactual(i))^2)^0.5; %Calculate the error at each points
end
%Plotting the result:
figure(1)
subplot(2,1,1);
plot(x,wfull,'LineWidth',2); hold on
plot(x,wactual,'k','LineWidth',2);
plot(x,wfull,'*b');
plot(x,wactual,'*k');
xlabel('x position', 'fontweight','bold');
ylabel('w(x) deflection, inch', 'fontweight','bold');
title('Approximate w(x) using Linear Finite Difference approach (N=60)', 'fontweight','bo
ld');
legend('Finite Difference Method', 'Actual deflection given by Equation');
grid on
subplot(2,1,2);
plot(x,Error,'LineWidth',2); hold on
plot(x,Error,'*b');
xlabel('x position', 'fontweight','bold');
ylabel('Error, (inch)', 'fontweight','bold');
grid on

D:\usr\llee3\CourseWorks\MTH538-NumericalAn...\Problem3_Prob6.m
Page 1
April 20, 2006
12:55:35 PM
%
%
%
%
%
%

----------------------------------------------------------------------Name: Leng-Feng Lee


Course: MTH 538 Numerical Analysis
Final Project-Problem 3 Part A
Description: Solve linear second order PDE using Linear Finite Method.
-----------------------------------------------------------------------

close all
clear all
clc
% NonLinear Finte-Difference Method:
% Solve the following Nonlinear Second order ODE:
%
% y" = f(x,y,y'), a<=x<=b, y(a)=alpha, y(b)=bete
%
%Equation Coefficients:
S=1000;
%lb
E=3e7;
%lb/in^2
q=100/12;
%lb/ft -> convert to lb/in
I=625;
%in^4
a=0;
b=120;
alpha=0;
%boundary value
beta=0;
%boundary value
%Method variables:
N=20;
%Number of intervals
h=(b-a)/(N); %interval size
%Use the solution from previous problem as the initial guess:
% w = [0.0002 0.0004 0.0006 0.0007 0.0009 0.0010 0.0011 0.0011 0.0012 0.0012 0.0012 ...
%
0.0011 0.0011 0.0010 0.0009 0.0007 0.0006 0.0004 0.0002];
% N = 20
w=linspace(0,0,19); % <- obtain the same result too if zero initial condition is used.
% w=[0.0001
...
%
0.0010
...
%
0.0011
...
%
0.0003

0.0002 0.0003 0.0004 0.0005 0.0006 0.0006 0.0007 0.0008 0.0009 0.0009 0.0010

% w=[0.0001
...
%
0.0008
...
%
0.0012
...
%
0.0011
...
%
0.0007
60

0.0001 0.0002 0.0003 0.0003 0.0004 0.0004 0.0005 0.0006 0.0006 0.0007 0.0007

TOL =1e-6;

0.0011 0.0011 0.0011 0.0012 0.0012 0.0012 0.0012 0.0012 0.0012 0.0012 0.0011
0.0011 0.0010 0.0010 0.0009 0.0009 0.0008 0.0007 0.0006 0.0006 0.0005 0.0004
0.0002 0.0001];

% N = 40

0.0008 0.0009 0.0009 0.0009 0.0010 0.0010 0.0010 0.0011 0.0011 0.0011 0.0011
0.0012 0.0012 0.0012 0.0012 0.0012 0.0012 0.0012 0.0012 0.0012 0.0012 0.0011
0.0011 0.0011 0.0010 0.0010 0.0010 0.0009 0.0009 0.0009 0.0008 0.0008 0.0007
0.0006 0.0006 0.0005 0.0004 0.0004 0.0003 0.0003 0.0002 0.0001 0.0001];% N =

D:\usr\llee3\CourseWorks\MTH538-NumericalAn...\Problem3_Prob6.m
Page 2
April 20, 2006
12:55:35 PM
MaxIter = 50;
% Solve the Aw = b system, now form the A matrix and b vector:
for j=1:1:MaxIter,
for i=1:1:N-1,
xi = a + i*h;
% Approximate w' :
if (i==1),
temp = ( w(i+1) - alpha )/(2*h);
elseif (i==N-1),
temp = ( beta - w(i-1) )/(2*h);
else
temp = ( w(i+1) - w(i-1) )/(2*h);
end
% Calculate the diagonal term:
diag(i) = 2 + h^2*fy(xi,w(i),temp);
% Calculate the supper diagonal term:
if (i < N-1),
supdiag(i) = -1 + (h/2)*fyp(xi,w(i),temp);
end
% Calculate the sub diagonal term:
if (i > 1),
subdiag(i-1) = -1 - (h/2)*fyp(xi,w(i),temp);
end
% Calculate the b vector
if (i == 1),
bvec(i) = -(2*w(i) elseif (i == N-1),
bvec(i) = -(2*w(i) else
bvec(i) = -(2*w(i) end

in Ay=b:
w(i+1) -alpha + h^2*ff(xi,w(i),temp));
w(i-1) -beta + h^2*ff(xi,w(i),temp));
w(i+1) -w(i-1) + h^2*ff(xi,w(i),temp));

end
% Now form the tridiagonal Sparse matrix A:
A=gallery('tridiag',subdiag, diag, supdiag);
% Calculate v using Gaussian Elimination:
v=A\bvec'
j
% updating w:
w=w+v';
% Check for convergence
if normVec(v) < TOL,
j = MaxIter;
end
end
wfull=[0 w 0];

D:\usr\llee3\CourseWorks\MTH538-NumericalAn...\Problem3_Prob6.m
Page 3
April 20, 2006
12:55:35 PM
%Create the actual w(x) based on Eqn.
x=linspace(0,120,N+1);
c1 = 7.7042537e4;
c2 = 7.9207462e4;
a1 = 2.3094010e-4;
b1 = -4.1666666e-3;
d1 = -1.5625e5;
wactual = c1*exp(a1*x) + c2*exp(-a1*x) + b1*(x-b).*x + d1 ;%+1e-3;
%Calculating the error:
for i=1:length(wfull),
Error(i) = ((wfull(i)-wactual(i))^2)^0.5; %Calculate the error at each points
end
%Plotting the result:
figure(1)
subplot(2,1,1);
plot(x,wfull,'LineWidth',2); hold on
plot(x,wactual,'k','LineWidth',2);
plot(x,wfull,'*b');
plot(x,wactual,'*k');
xlabel('x position', 'fontweight','bold');
ylabel('w(x) deflection, inch', 'fontweight','bold');
title('Approximate w(x) using Nonlinear Finite Difference approach (N=60)', 'fontweight',
'bold');
legend('Nonlinear Finite Difference Method', 'Actual deflection given by Equation');
grid on
subplot(2,1,2);
plot(x,Error,'LineWidth',2); hold on
plot(x,Error,'*b');
xlabel('x position', 'fontweight','bold');
ylabel('Error, (inch)', 'fontweight','bold');
grid on

D:\usr\llee3\CourseWorks\MTH538-NumericalAnalysisII\Pro...\ff.m
Page 1
April 20, 2006
12:56:31 PM

function fout = ff(xi, wi, wip)

%Equation Coefficients:
S=1000; %lb
E=3e7; %lb/in^2
q=100/12; %lb/ft -> convert to lb/in
I=625; %in^4
a=0;
b=120;
alpha=0;
beta=0;

temp1 = (1 + wip^2)^(3/2);
temp2 = ( (S*wi)/(E*I) + q*xi*(xi-b)/(2*E*I) );
fout = temp1*temp2;

D:\usr\llee3\CourseWorks\MTH538-NumericalAnalysisII\Pro...\fy.m
Page 1
April 20, 2006
12:56:47 PM

function fy = f_y(xi, wi, wip)

%Equation Coefficients:
S=1000; %lb
E=3e7; %lb/in^2
q=100/12; %lb/ft -> convert to lb/in
I=625; %in^4

% temp1 = 3*wip*
% temp2 = ( (S*wi)/(E*I) + q*xi*(xi-b)/(2*E*I) );
fy = (S/(E*I))*(1 + wip^2)^(3/2);

D:\usr\llee3\CourseWorks\MTH538-NumericalAnalysisII\Pr...\fyp.m
Page 1
April 20, 2006
12:57:08 PM

function fyp = f_yp(xi, wi, wip)

%Equation Coefficients:
S=1000; %lb
E=3e7; %lb/in^2
q=100/12; %lb/ft -> convert to lb/in
I=625; %in^4
a=0;
b=120;
alpha=0;
beta=0;

temp1 = 3*wip*(1+wip^2)^(1/2);
temp2 = ( (S*wi)/(E*I) + q*xi*(xi-b)/(2*E*I) );
fyp = temp1*temp2;

You might also like