Professional Documents
Culture Documents
Machine Exercise
Machine Exercise
Contents
1 Analytical Method
1.1
Complimentary Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2
Particular Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.3
General Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2 Numerical Method
2.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.2
Thomas Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.3
The Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.3.1
2.3.2
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
11
3.1
Numerical Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.2
Analytical Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.3
List of Figures
3.1
3.2
3.3
3.4
h versus eL2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.5
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
(1.1)
(1.2)
(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)
(1.5)
1.2
Particular Solution
(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)
(1.10)
(1.11)
(1.12)
(1.13)
(1.14)
(1.15)
(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
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( ) 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
(1.21)
Chapter 2
Numerical Method
2.1
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
=
=
(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)
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
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
#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
47
48
49
50
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
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
87
88
89
90
91
92
printf("\n");
for(i=0;i<n;i++){
printf("\n");
printf("| %.8lf
}
|",R[i]);
93
94
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");
103
104
105
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.2
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
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.
3.2
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.
3.3
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
13
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