Você está na página 1de 19

CE 28

Acosta, Timothy John S.


July 21,2011

Contents
1 Analytical Method

1.1

Complimentary Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1.2

Particular Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1.3

General Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2 Numerical Method

2.1

Finite Difference Method

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2.2

Thomas Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2.3

The Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2.3.1

The Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2.3.2

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

3 Results and Discussion

11

3.1

Numerical Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

3.2

Analytical Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

3.3

eL2 Computations and Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

List of Figures
3.1

Different Node Densities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

3.2

Theoretical Graph for the Given Differential Equation . . . . . . . . . . . . . . . . . 12

3.3

Convergence of Numerical to Analytical Solution . . . . . . . . . . . . . . . . . . . . 13

3.4

h versus eL2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

3.5

log(h) versus log(eL2 ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

Abstract
This report tackles the 1st Machine Exercise given by Professor Sargado during the 1st
semester of SY 2011-12. This Machine Exercise (Mx) covers Non-homogenous Differential Equations, the Finite Difference Method for ODEs and the Thomas Algorathim. In this Mx, we are
given the 2nd order boundary value problem:
dy
d2 y
3
+ 2y = 12 cos(x) 6 sin(x) 8x + 12
2
dx
dx

0<x<

y(0) = 3
dy
= 1

dx x= 2
The Mx requires the student to solve the differential equation using an analytical and
numerical solution. The analytical solution in this report was done by the method of undetermined
coefficients. The analytical solution will serve as the theoretical solution, which will be used for
comparison with numerical solution. The numerical solution on the other hand will be done by
using the Finite Difference Method (FDM). The O (h2 ) central difference formula was used for the
numerical approximation of the 1st and 2nd order derivatives. The solution was then computed
based on 3 different nodal densities 15,50,100. The resulting coefficient matrix for the FDM solution
was tridiagonal making it possible to use the Thomas Algorithm to solve for the values of y at each
node. Given the solutions for both methods, the convergence of the numerical method to the
analytical method was done by the following equation:
sP
n
exact y numerical )2
i=1 (y
i
i
P
eL2 =
n
exact )2
(y
i=1 i

Chapter 1

Analytical Method
1.1

Complimentary Solution

The BVP in this exercise is an example of a non-homogenous differential equation. It is a


second order, linear non-homogenous differential equation in the form of:
y 00 + p(x)y 0 + q(x)y = g(x)

(1.1)

where g(t) is a non-zero function.


In order to solve this kind of equation, we must first assume that the equation is a homogenous
differential equation. The temporary solution for this assumed equation would be then called a
Complementary Solution.
yc (x) = c1 y1 (x) + c2 y2 (x)

(1.2)

We therefore write the given BVP as


d2 y
dy
3
+ 2y = 0
dx2
dx
Taking the characteristic equation of (1.3), we get

(1.3)

r2 3r + 2 = 0
(r 2)(r 1) = 0
r = 2|r = 1
Since the root values are real and unequal, the complementary solution (1.2) takes the form
yc (x) = c1 eAx + c2 eBx

(1.4)

Where A and B are the constants determined by the characteristic equation.


Plugging the given values into equation (1.4), we get
yc (x) = c1 e2x + c2 ex
1

(1.5)

1.2. PARTICULAR SOLUTION

1.2

CHAPTER 1. ANALYTICAL METHOD

Particular Solution

Now we consider the function g(x) which is a non-zero function.


g(x) = 12 cos(x) 6 sin(x) 8x + 12

(1.6)

We then call the solution for this function as the Particular Solution (Yp ) to the nonhomogeneous differential equation. Given the function, there are two methods to compute for the
Particular Solution, one is the Method of Undetermined Coefficients and the other is called Variation of Parameters. In this report, we will only tackle the 1st method mentioned.
Seperating the equation into three parts makes the function easier to solve. Therefore we have
g(x1 ) = 12 cos(x1 )

(1.7)

g(x2 ) = 6 sin(x2 )

(1.8)

g(x3 ) = 8x3 + 12

(1.9)

Solving for (1.7), we take an initial guess


Yp (x1 ) = A cos(x1 ) + B sin(x1 )

(1.10)

We then differentiate (1.10) and plug it into equation (1.3)


[A cos(x1 ) B sin(x1 )] 3[A sin(x1 ) + B cos(x1 )]
+2[A cos(x1 ) + B sin(x1 )] = 12 cos(x1 )
A cos(x1 ) 3B cos(x1 ) + 3A sin(x1 ) + B sin(x1 ) = 12 cos(x1 )

(1.11)

We equate the coefficients for cos(x1 ) and sin(x1 )


sin(x1 ) : 3A + B = 0
3A = B
cos(x1 ) : A 3B = 12
A 3(3A) = 12
10A = 12
6
A =
5
18
B =
5
Plugging into (1.10), our particular solution for (1.7) is
6
18
sin(x1 )
Yp (x1 ) = cos(x1 ) +
5
5
2

(1.12)

1.2. PARTICULAR SOLUTION


CHAPTER 1. ANALYTICAL METHOD
In order to solve for (1.8), we take the same initial guess used for (1.7). Repeating the
process, we come up with the equations for coefficients of cos(x2 ) and sin(x2 ).
cos(x2 ) : A 3B = 0
A = 3B
sin(x2 ) : B + 3A = 6
B + 9B = 6
10B = 6
3
B =
5
9
A =
5
Therefore, the particular solution for (1.8) is
3
9
Yp (x2 ) = cos(x2 ) sin(x2 )
5
5

(1.13)

For equation (1.9), our initial guess is


Yp (x3 ) = Ax3 + B

(1.14)

Differentiating and plugging it into (1.3), we get


[0] 3[A] + 2[Ax3 + B] = 8x3 + 12

(1.15)

Equating the coefficients for x and the constant


x : 2A = 8
A = 4
constant : 2B = 12
3A + 2B = 12
12 + 2B = 12
B = 0
Therefore, we get the particular solution for equation (1.9)
Yp (x3 ) = 4x3

(1.16)

Combining the particular solutions for equations for (1.7),(1.8),(1.9), we get the particular
solution for the original function g(x)
Yp (x) = 3 cos(x) + 3 sin(x) 4x

(1.17)

1.3. GENERAL SOLUTION

1.3

CHAPTER 1. ANALYTICAL METHOD

General Solution

Combining the complementary solution (1.5) from page 1 and particular solution(1.17)
from page 3, we get the general solution for the differential equation and its derivative.
y(x) = yc (x) + Yp (x)
y(x) = c1 e
0

2x

+ c2 e 3 cos(x) + 3 sin(x) 4x

2x

y (x) = 2c1 e

(1.18)

+ c2 e + 3 sin(x) 3 cos(x) 4

(1.19)
(1.20)

Applying the boundary conditions to these, we can compute for the constants c1 and c2
3 = c1 e0 + c2 e0 3 cos(0) + 3 sin(0) (0)
3 = c1 + c2 3
0 = c1 + c2

1 = 2c1 e + c2 e 2 + 3 sin( ) 3 cos( ) 4


2
2

1 = 2c1 e + c2 e 2 + 3 sin( ) 4
2

1 = 2(c2 )e + c2 e 2 1

0 = 2c2 e + c2 e 2
0
= c2

2e + e 2
0 = c2
0 = c1

Thus, our general solution for the given BVP becomes


y(x) = 3 cos(x) + 3 sin(x) 4x

(1.21)

Chapter 2

Numerical Method
2.1

Finite Difference Method

For the Numerical solution of the given differential equation, /emphthe Finite Difference
Method was used. In this method, we use the Central difference equation derived from the Taylor
series equation to define the derivatives of the respective variables.
d2 y
dx2
dy
dx

=
=

yi1 2ui + ui+1


(x2 )
ui+1 ui1
2x

(2.1)
(2.2)

Inputing the defined terms into the original equation (1.1) from section 1.1 of page 1, we get
yi1 2ui + ui+1
ui+1 ui1
3
+ 2ui = g(x)
(x2 )
2x
h 1
h
i
h 1
3 i
2
3 i
+
u
+

+
2
u
+

ui+1 = g(x)
i1
i
x2 2x
x2
x2 2x
Aui1 + Bui + Cui+1 = g(x)
We then produce ith equations depending on the number of nodes used. This gives us
n 2 equations with n unknowns. Therefore we have to implement the boundary conditions to get
n equations.
u0 = 3

du
= 1

dx x= 2
un+1 = 1(2x) + un2
Inputing this into our nth equation gives
Aun1 + Bun C(2x + un1 ) = g(x)
(A + C)un1 + Bun = g(x) + C(2x)
5

(2.3)

2.2. THOMAS ALGORITHM


CHAPTER 2. NUMERICAL METHOD
We may be then able to write the equations in matrix form

u0

1 0
0
...
0

.
A B C

.
.
.
.
0

.
g(x)

.
.
.
.
.
.
.
.
.
.
0

.
.
.
.
.
.
=
.

..

.
.
.

.
.
.
.
g(x)

.
.
.
.
.

g(x)
+
2Cx

0 0
0 (A + C) B
ui
As observed, only the main diagonals have values within the matrix. This is defined as a
Tri-diagonal system. Because of this pattern, we can rewrite the matrix into a n x 3 matrix which
allows us to use the Thomas Algorithm.


x1

e1 f1 g1

..

e2 f2 g2

..
..
..
.

.
.
.
.
(2.4)
. = g(x)

..

..
..

.
.
..
.
.

en fn gn
xn

2.2

Thomas Algorithm

The thomas algorithm is an iterative process which allows us to solve for the values of xk
given the tri-diagonal system defined above (2.4). The algorithm could be summarized into three
short processes.
1. Decomposition
DO

k = 2, n
ek = ek /fk1
fk = fk ek gk1

2. Forward Substitution
DO

k = 2, n
rk = rk ek rk1

3. Backward Substitution
xn = rn /fn
DO

k = n 1, 1, 1
xk = (rk gk xk+1 )/fk

Since the method is iterative, we may use a software to do the iterations for us. This
would become very convenient especially if the number of iterations desired is a very large number.

2.3. THE PROGRAM

2.3

CHAPTER 2. NUMERICAL METHOD

The Program

The software that was used for the numerical solution in this report was Microsoft Visual
C++ 2010 Express. The specified software was chosen because of its user-friendly interface and
its easier method for compilation.

2.3.1
1
2
3
4
5
6

The Source Code

#include "files.h"
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<conio.h>
#define pie 3.141592654

7
8
9
10

void main()
{

11
12
13
14

int n;
int i,j;
double a1,a2,a,b,c,g,dx,y;

15
16
17
18
19

/*User Input*/
printf("\nInput nodes:");
scanf("%d",&n);
dx=(pie/2)/(n-1);

20
21

printf("dx is %lf\n",dx);

22
23
24
25

sarray r=defsarray(n);
sarray x=defsarray(n);
matrix u=defmatrix(n,3);

26
27
28
29

double*R=r.elems;
double*X=x.elems;
double**U=u.elems;

30
31
32
33
34
35

a1=(1/(dx*dx));
a2=(3/(2*dx));
a=a1+a2;
b=((-2/(dx*dx))+2);
c=a1-a2;

36
37
38
39

printf("constant a is %lf\n",a);
printf("constant b is %lf\n",b);
printf("constant c is %lf\n",c);

40
41
42
43
44
45
46

/*Defining Right Side-Equation*/


printf("\nComputing Right side equation...\n");
system("PAUSE");
for(i=1;i<n;i++){
y=dx*i;
R[i]=((-12*(cos(y)))-(6*(sin(y)))-(8*y)+12);

2.3. THE PROGRAM


printf("R[%d] is %.8lf\n",i,R[i]);

47
48

CHAPTER 2. NUMERICAL METHOD

49
50

/*Defining Tri-Diagonal System*/

51
52
53
54
55
56
57
58
59
60
61
62
63

for(int i=1;i<n;i++)
for(int j=0;j<3;j++)
{if(j==0)
U[i][j]=a;
else
{
if(j==1)
U[i][j]=b;
else
U[i][j]=c;
}
}

64
65

g=a+c;

66
67
68
69
70
71
72

U[0][0]=0;
U[0][1]=1;
U[0][2]=0;
U[n-1][0]=g;
U[n-1][1]=b;
U[n-1][2]=0;

73
74

/*Implementing Boundary Conditions*/

75
76
77
78
79

R[0]=-3;
y=(n-1)*dx;
R[n-1]=((-12*(cos(y)))-(6*(sin(y)))-(8*(y))+12)+(2*c*dx);

80
81
82
83
84
85
86

printf("\n|Initial Constant Matrix:|\n\n");


for(i=0;i<n;i++){
printf("\n");
for(j=0;j<3;j++)
printf("| %.8lf |",U[i][j]);
}

87
88
89
90
91
92

printf("\n");
for(i=0;i<n;i++){
printf("\n");
printf("| %.8lf
}

|",R[i]);

93
94

/***Simultaneous Solving using Thomas Algorithm***/

95
96
97
98
99
100
101
102

/*Decomposition*/
for(i=1;i<n;i++){
U[i][0]=U[i][0]/U[i-1][1];
U[i][1]=U[i][1]-(U[i][0]*U[i-1][2]);
}
for(i=0;i<n;i++){
printf("\n");

2.3. THE PROGRAM


for(j=0;j<3;j++)
printf("| %.8lf

103
104
105

CHAPTER 2. NUMERICAL METHOD


|",U[i][j]);

106
107

/*Forward Substitution*/

108
109
110
111
112
113
114
115
116

for(i=1;i<n;i++){
R[i]=R[i]-(U[i][0]*R[i-1]);
}
printf("\n");
for(i=0;i<n;i++){
printf("\n");
printf("| %.8lf |",R[i]);
}

117
118

/*Backward Substitution*/

119
120
121
122
123
124
125

X[n-1]=R[n-1]/U[n-1][1];
for(i=n-2;i!=-1;i--){
X[i]=(R[i]-(U[i][2]*X[i+1]))/U[i][1];
}
printf("\n\nComputing Unknowns...\n");
system("PAUSE");

126
127
128
129
130

/*PRINT*/
printf("\n\nFINAL MATRIX:\n\n");
for(i=0;i<n;i++) {
printf("| %.10lf |\n",X[i]);

131
132
133
134

}
system("PAUSE");
printf("\n\nStoring Data...\n");

135
136
137
138
139
140
141

/*STORING DATA*/
FILE *data = fopen("mx1.txt", "w");
fprintf(data,"\nFINAL MATRIX:\n\n");
fprintf(data,"dx is %lf \n\n",dx);
for(i=0;i<n;i++) {
fprintf(data," %.10lf \n",X[i]);

142
143

144
145
146
147

fclose(data);
_getch();

148
149

system("PAUSE");

150
151
152

153

2.3. THE PROGRAM

2.3.2

CHAPTER 2. NUMERICAL METHOD

Summary

The program starts by asking the user to input the number of nodes/iterations that the
program would use. Once inputted, the program computes for the constants A, B and C that are
computed following the finite difference method. The program then directly defines a tri-diagonal
matrix with the implemented boundary conditions, the g(x) function and constants. The tridiagonal system then undergoes the iterative thomas algorithm until all the values at the nodes are
computed.

10

Chapter 3

Results and Discussion


3.1

Numerical Results

The 3 node densities chosen for this report were 15,50 and 100 nodes. After inputing the number
of nodes in the program, the following graph was produced.

Figure 3.1: Different Node Densities


As you can see, the graph converges to a certain value as the number of node increases. An
additional node density of 9 was added to further emphasize the convergence of the graph from
above.
11

3.2. ANALYTICAL RESULTS

3.2

CHAPTER 3. RESULTS AND DISCUSSION

Analytical Results

The analytical results as discussed in the previous chapters are considered as the theoretical
values for the differential equation. Therefore, the numerical results in theory must converge to a
certain values, which is the results from the analytical solution.

Figure 3.2: Theoretical Graph for the Given Differential Equation


Among the three node densities used for the numerical solution, the node density used for the graph
above was 100. The largest node density was chosen because the differential equation is applied
over the nodes and not over the intervals. Therefore, a larger node density would mean that there
are fewer spaces (intervals) resulting to a more accurate description of the differential equation.

3.3

eL2 Computations and Analysis

The theory is that the numerical solution converges to the analytical solution. Since the
node densities determine the accuracy of the convergence of the graphs, the graph for each density
must approach the analytical solution as the nodal density increases. As shown below in Figure
(3.3), combining both the theoretical graph and numerical graph can show us graphical the convergence of the numerical solution to the analytical solution.

12

3.3. EL2 COMPUTATIONS AND ANALYSIS

CHAPTER 3. RESULTS AND DISCUSSION

Figure 3.3: Convergence of Numerical to Analytical Solution


We can also prove this convergence mathematically by ploting a h versus eL2 graph.
We can determine that it converges to the analytical solution if the error (eL2 ) of the numerical
approximation approaches 0 as the nodal density increases.

Figure 3.4: h versus eL2


Ploting the log(h) versus log(eL2 ) graph would give us a linear treadline. This linear treadline
would give us the order of convergence of the numerical solution. According to references [4], the
order should be around 2.

13

3.3. EL2 COMPUTATIONS AND ANALYSIS

CHAPTER 3. RESULTS AND DISCUSSION

Figure 3.5: log(h) versus log(eL2 )


The equation y = 2.8652x+2.4237 is the treadline for the given data points. The slope of
2.8652 gives us the order of convergence of the numerical solution. Rounding down this value, we
get a order of 2 for the convergence. The slope or value is not an exact value of 2 because of certain
accumulative errors. One possible accumulative error would be in the computation of the values
using Microsoft Excel 2010. Values were accurate only to the 7th decimal point. Accumulation of
these small erros may have lead to the 0.8 inaccuracy of our final slope for the treadline.

14

Bibliography
[1] Chapra Canale: Numerical Methods for Engineers, 6th Edition (2009)
[2] Joe D. Hoffmann: Numerical Methods for Engineers and Scientists, 2nd Edition (2001)
[3] Boyce and Diprima: Elementary Differential Equations and Boundary Value Problems, 7th
Edition (2001)
[4] J.M Sargado: Machine Exercise I, Finite Difference Method for ODEs and the Thomas Algorithm
[5] Paul Dawkins :Pauls Online Notes Differential Equations Variation Parameters.htm (20032011)
[6] Paul Dawkins :Pauls Online Notes Differential Nonhomgeneous Differential Equations.htm
(2003-2011)
[7] Paul Dawkins :Pauls Online Notes Differential Equations Undetermined Coefficients.htm
(2003-2011)

15

Você também pode gostar