Вы находитесь на странице: 1из 26

Analytical and Numerical Analysis of an Ordinary

Differential Equation
J.A. Fernandeza, J.P. Romeroa
a

Institute of Civil Engineering, University of the Philippines Diliman

Abstract
This paper presents two approaches in solving an ordinary differential equation, first by
using analytical methods such as MUC, and the next is by using numerical method like Finite
Difference Method, which is performed with the help of computer programming. In addition,
the level of accuracy of the solutions shall be compared by plotting the equations graph with its
values coming from both of the analytical and numerical methods. The MUC or the Method of
Undetermined Coefficients is a method of solving an ODE in an analytical manner. This method
is used specifically for Non-homogenous Linear Equations with Constant Coefficients to
determine the complete solution for an ODE, and it is only applicable to a certain forms of f(x).
The Finite Difference Method, however, is a numerical method which requires setting nodes for
the given equation that is inside the given boundary conditions. This method is rigorous and
knowledge of computer programming languages such as C or C++ could help hasten the solving
time.

1. Introduction
We were given an ODE to be solved both by analytical and numerical methods. The given
equation was (5D2 3D + 2)y = cos x + 5x bounded from 0 to 1. The conditions were given as y(0) = 0
and y(1) = 1.2. Through this equation, we must demonstrate how we approached to the particular
solution and give its numerical values. As stated, we shall use MUC for the analytical method and the
Finite Difference Method for the numerical analysis.
2. The Method of Undetermined Coefficients
The MUC is a special type of method to be used with a specific type of ODEs. Usually, it is only
applicable to a set of f(x), and is given from the table:

With the specified yps, and also the steps which will be elaborated further, we can solve for the
given ODE.
Figure 1 MUC Table

2.1 Steps in Solving ODEs with MUC


a) Check whether f(x) contains only those terms listed in the table.
b) Solve for the roots m, (m) = 0, and set up yc.
c) Assume the form of yp on the basis of the form of f(x). If f(x) is a multi-function,
treat each term of f(x) individually by setting up yp i.e., yp = yp1 + yp2 + + ypk.
d) Check yp versus yc and whenever a term in any of the yp duplicates in the term in the
yc, all the terms in that particular yp must be multiplied by the lowest possible integral
power of x sufficient to eliminate duplication.
e) Check duplication between yps. Necessary operations must be carried out to see to it
that no term is duplicated.
f) Substitute the assumed form of yp into the differential equation and evaluate the
coefficients of identical terms.
The steps mentioned above will help us in solving analytically the ODE. The analytical
solution for the stated ODE shall be discussed further.
2.2 Analytical Solution to the given ODE
As stated, the given ODE was (5D2 3D + 2)y = cos x +5x bounded between 0 and 1
with y(0) = 0 and y(1) = 1.2. We shall obtain a particular solution by solving the ODE
analytically with MUC. The discussion of the solution and the steps shall be elaborated as
follows:
2.2. a) Solving for yc
Manipulating the left-hand side of the ODE as:

by Quadratic Formula,

, we obtain the roots of m as;

Since the roots are complex and distinct, it follows that:

Hence, yc will become:

2.2. b) Solving for yp


We now focus on the right-hand side of the ODE. We check if the terms at the right-hand
side, and found out that there are corresponding conditions to the terms:

Since there are no duplications in yc, we can proceed as;

Then, we can differentiate yp up to the 2nd order [since our ODE is up to the 2nd order]:

Substituting to the DE:

Solving for the coefficients, and then plugging the yp to yc, we obtain the closed solution
for the ODE:

Evaluating the closed solution with the given boundary conditions, we obtain the
particular solution to the given ODE:

3. Numerical Analysis
Another approach in solving the ODE is by numerical analysis, specifically by using the
Finite Difference Method. In this method, we set the number of nodes depending on the level of
accuracy that we want our solution to obtain. Increasing the number of nodes also increases the
level of accuracy of our solution. For the given ODE, we shall use the Central Difference
Formulas for the 1st and 2nd order differential. The formulas are given as:
1st order derivative Central Difference Formula
2nd order derivative Central Difference Formula
The formulas are plugged-in to the ODE as:

Then for each node, there would be corresponding equations formed. However, for the initial
and terminal nodes, there would be fictitious nodes, or nodes outside of the boundaries of the
solution. These fictitious nodes will either be terminated since they are outside of the
boundary. As a result, we formed a series of equations which are not independent of each other.
These equations cannot be solved by simple elimination and substitution; instead we shall first
transform the equations into a matrix, so that we can apply special decomposition methods. We
notice that the formed matrix was tridiagonal, as illustrated below:

Figure 2 Tridiagonal System Matrix

For this type of matrix, special kinds of decomposition methods are used to determine the
solutions. For this given ODE, we could use the Thomas Algorithm and the Crout
Decomposition to determine the solutions.
The Crout Decomposition method decomposes the matrix into 2, and these matrices are often
represented as L (for the lower triangular decomposed matrix) and U (for the upper triangular
decomposed matrix. These matrices are formed by these governing formulas, when solved, it
seems like it sweeps through the matrix. The formulas for the Crout decomposition are as
follows:

For j = 2, 3, , n-1

And

The matrix that will be formed will depend on how many nodes you set for the Crout
decomposition. Usually, the form of the matrix when using Crout is an nxn matrix (this means
that when using Crout decomposition, the matrix always appears as a square tridiagonal matrix).
Thomas Algorithm is another type of method that can be used to solve a tridiagonal
matrix. We can get a solution in O(n) operations, instead of O(n3/3). The equation to be
followed will be:

where a1 and cn are equal to zero. Solution Algorithm for the Thomas starts at k = 2, ,n:

Then:

and finally, for k = n-1, , 1:

To summarize the concept of the Thomas Algorithm, we have to remember these three steps:
decomposition, forward and back substitution; thus, using Thomas Algorithm simplifies the
solution of a tridiagonal matrix.
For this ODE, we set Crout Decomposition to be the method of solution for our constructed
tridiagonal matrix.

4. Results
Here are the results of the Analytical and Numerical analyses for the given ODE. Note
that for the Numerical Analysis, weve used DevC++ instead of Microsoft Visual C++ Express
2010 for the following reasons: 1) the codes for the analysis were made at DevC++. We
assumed that it will also work for MSVC++E2010 but unfortunately it did not work. 2) The
compiler of VC++2010 is stricter compared to DevC++ that is why our program did not work on
VC++2010. However, we presume that the results that we got from our code in DevC++ are
correct since we compared our answer to the given sample data.
4.1) Graphical Illustration of the Results from Analytical Analysis
n=9
x
0
0.125
0.25
0.375
0.5
0.625
0.75
0.875
1

y
1.1803
1.17195
1.160577
1.147971
1.13602
1.12671
1.122119
1.124413
1.2

1.21
1.2
1.19
1.18
1.17
1.16
y

1.15
1.14
1.13
1.12
1.11
0

0.2

0.4

0.6

Figure 3 Graph of the ODE solved by Analytical Method (n=9)

0.8

n=16
x

1.21

0
0.0667
0.1334
0.2001
0.2668
0.3335
0.4002
0.4669
0.5336
0.6003
0.667
0.7337
0.8004
0.8671
0.9338
1.0005
n=26
x
0
0.04
0.08
0.12
0.16
0.2
0.24
0.28
0.32
0.36
0.4
0.44
0.48
0.52
0.56
0.6
0.64
0.68
0.72
0.76
0.8
0.84
0.88
0.92
0.96
1

1.1803
1.176326
1.171261
1.165367
1.158914
1.152178
1.145446
1.139011
1.133174
1.128243
1.124535
1.122374
1.12209
1.124021
1.128512
1.2
y
1.1803
1.178062
1.175395
1.172354
1.168995
1.165377
1.161556
1.157594
1.15355
1.149487
1.145466
1.141551
1.137808
1.1343
1.131096
1.128263
1.125868
1.123983
1.122676
1.122019
1.122085
1.122947
1.124679
1.127356
1.131053
1.2

1.2
1.19
1.18
1.17
1.16
y

1.15
1.14
1.13
1.12
1.11
0

0.2

0.4

0.6

0.8

Figure 4 Graph of the ODE solved by Analytical Method (n=16)

1.21
1.2
1.19
1.18

1.17
1.16

1.15
1.14
1.13
1.12
1.11
0

0.2

0.4

0.6

Figure 5 Graph of the ODE solved by Analytical Method (n=26)

0.8

1.21
1.2
1.19
1.18
1.17
Series1

1.16

Series2
1.15

Series3

1.14
1.13
1.12
1.11
0

0.2

0.4

0.6

0.8

Figure 6 Graph of the results from the Analytical Method (with varying n-values)

4.2) Graphical Illustration of the Results from the Numerical Analysis


n=9
0
0.125
0.25
0.375
0.5
0.625
0.75
0.875
1

1.19053
1.188372
1.18358
1.177933
1.173306
1.171666
1.173308
1.18201
1.2

1.205
1.2
1.195
1.19
Series1

1.185
1.18
1.175
1.17
0

0.2

0.4

0.6

0.8

1.205

n=16
0
0.0667
0.1334
0.2001
0.2668
0.3335
0.4002
0.4669
0.5336
0.6003
0.667
0.7337
0.8004
0.8671
0.9338
1

1.180891
1.180285
1.17872
1.176456
1.173762
1.170913
1.168194
1.165896
1.164317
1.163764
1.164549
1.166993
1.171423
1.178173
1.187583
1.2

1.2
1.195
1.19
1.185
1.18

Series1

1.175
1.17
1.165
1.16
0

0.2

0.4

0.6

0.8

n=26
0
0.04
0.08
0.12
0.16
0.2
0.24
0.28
0.32
0.36
0.4
0.44
0.48
0.52
0.56
0.6
0.64
0.68
0.72
0.76
0.8
0.84
0.88
0.92
0.96
1

1.180521
1.180303
1.179704
1.178778
1.177582
1.176174
1.17461
1.172951
1.171257
1.169588
1.168007
1.166577
1.165363
1.164429
1.163841
1.163666
1.163972
1.164827
1.166302
1.168467
1.171393
1.175152
1.179818
1.185465
1.192167
1.2

1.205
1.2
1.195
1.19
1.185
1.18

Series1

1.175
1.17

1.165
1.16
0

0.2

0.4

0.6

0.8

1.205
1.2
1.195
1.19
1.185

Series1

1.18

Series2
Series3

1.175

1.17
1.165
1.16
0

0.2

0.4

0.6

0.8

Figure 7 Graph of the results from the Numerical Analysis (with varying n-values)

4.3) Graphs of the Analytical and the Numerical Results


n=9
1.21

1.2
1.19
1.18
1.17
1.16

n=9(analytical)

1.15

n=9(numerical)

1.14
1.13
1.12
1.11
0

0.2

0.4

0.6

0.8

n=16

1.21
1.2
1.19
1.18
1.17
1.16

n=16(analytical)

1.15

n=16(numerical)

1.14
1.13
1.12

1.11
0

0.2

0.4

0.6

0.8

n=26
1.21
1.2
1.19
1.18

1.17
1.16

n=26(analytical)

1.15

n=26(numerical)

1.14
1.13
1.12
1.11
0

0.2

0.4

0.6

0.8

From the illustrations shown, we can see that the values from the analytical analysis do not
converge with the numerical results. It may be due to the erroneous result for the exact equation
coming from the analytical analysis of the ODE, or may be other reasons. However, we notice
that the results coming from the numerical analysis, when plotted into one graph, approaches
identity as the number of nodes increases. This is may be due to the increase of level of accuracy
as the number of nodes increases.
4.4) Graph of log h vs. L2-norm of the error
Now, the graphs that will be illustrated here are plots of log h against log e L2. eL2 is the
L2-norm of the error and is given by this equation:

n=9(numerical)
0
1.19053
0.125 1.188372
0.25
1.18358
0.375 1.177933
0.5 1.173306
0.625 1.171666
0.75 1.173308
0.875
1.18201
1
1.2

numerical
1.19053
1.188372
1.18358
1.177933
1.173306
1.171666
1.173308
1.18201
1.2

exact
1.1803
1.1719501
1.1605768
1.1479706
1.1360203
1.1267103
1.1221185
1.1244126
1.2

n=9(exact)
0
0.125
0.25
0.375
0.5
0.625
0.75
0.875
1

1.1803
1.17195005
1.1605768
1.147970602
1.136020253
1.126710345
1.122118519
1.124412604
1.2

exact(exactnum
num)^2
exact^2
-0.01023 0.000104653
1.39310809
-0.016422
0.00026968 1.373466921
-0.023003 0.000529147 1.346938508
-0.029962 0.000897745 1.317836502
-0.037286 0.001390227 1.290542016
-0.044956 0.002021011
1.2694762
-0.051189 0.002620363
1.25914997
-0.057597
0.00331746 1.264303704
0
0
1.44
sum
0.011150287 11.95482191

eL2
0.03054

n=16(numerical)
0 1.180891
0.0667 1.180285
0.1334
1.17872
0.2001 1.176456
0.2668 1.173762
0.3335 1.170913
0.4002 1.168194
0.4669 1.165896
0.5336 1.164317
0.6003 1.163764
0.667 1.164549
0.7337 1.166993
0.8004 1.171423
0.8671 1.178173
0.9338 1.187583
1
1.2

num

exact

1.180891

1.173358

1.180285

1.173184

1.17872
1.176456

1.172736
1.172093

1.173762

1.171334

1.170913
1.168194
1.165896
1.164317
1.163764
1.164549
1.166993

1.170539
1.169787
1.169157
1.168727
1.168577
1.16879
1.169457

1.171423

1.17068

1.178173
1.17258
1.187583 1.175302
1.2
1.2

n=16(exact)
0
0.0667
0.1334
0.2001
0.2668
0.3335
0.4002
0.4669
0.5336
0.6003
0.667
0.7337
0.8004
0.8671
0.9338
1

exactnum
0.0075333
0.0071012
0.0059837
-0.004363
0.0024281
0.0003744
0.0015928
0.0032608
0.0044098
0.0048128
0.0042409
0.0024639
0.0007426
0.0055926
-0.012281
0
sum

(exactnum)^2

1.1803
1.176326
1.171261
1.165367
1.158914
1.152178
1.145446
1.139011
1.133174
1.128243
1.124535
1.122374
1.12209
1.124021
1.128512
1.2

exact^2

5.67499E-05 1.376768
5.04268E-05

1.37636

3.58051E-05 1.37531
1.9036E-05 1.373802
5.89585E-06 1.372023
1.40184E-07
2.53693E-06
1.06329E-05
1.94466E-05
2.3163E-05
1.79849E-05
6.07088E-06

1.370161
1.368401
1.366928
1.365922
1.365572
1.36607
1.367629

5.51477E-07 1.370493
3.12773E-05 1.374945
0.000150824 1.381335
0
1.44
0.000430542 22.01172

eL2
0.004423

n=26(numerical)
0 1.180521
0.04 1.180303
0.08 1.179704
0.12 1.178778
0.16 1.177582
0.2 1.176174
0.24
1.17461
0.28 1.172951
0.32 1.171257
0.36 1.169588
0.4 1.168007
0.44 1.166577
0.48 1.165363
0.52 1.164429
0.56 1.163841
0.6 1.163666
0.64 1.163972
0.68 1.164827
0.72 1.166302
0.76 1.168467
0.8 1.171393
0.84 1.175152
0.88 1.179818
0.92 1.185465
0.96 1.192167
1
1.2

num
1.180521
1.180303
1.179704
1.178778
1.177582
1.176174
1.17461
1.172951
1.171257
1.169588
1.168007
1.166577
1.165363
1.164429

exact
1.1732515
1.173189
1.1730174
1.1727528
1.1724123
1.1720132
1.1715721
1.1711067
1.1706342
1.1701713
1.1697353
1.169343
1.1690114
1.1687573

n=26(exact)
0
0.04
0.08
0.12
0.16
0.2
0.24
0.28
0.32
0.36
0.4
0.44
0.48
0.52
0.56
0.6
0.64
0.68
0.72
0.76
0.8
0.84
0.88
0.92
0.96
1
exactnum
-0.007269
-0.007114
-0.006687
-0.006025
-0.00517
-0.004161
-0.003038
-0.001844
-0.000623
0.0005833
0.0017283
0.002766
0.0036484
0.0043283

1.1803
1.178061938
1.175394729
1.172353673
1.168995136
1.165376547
1.161556384
1.15759417
1.153550464
1.149486847
1.14546592
1.14155129
1.137807566
1.134300343
1.131096198
1.128262681
1.125868302
1.123982523
1.122675749
1.122019316
1.122085483
1.12294742
1.124679199
1.127355781
1.131053006
1.2

(exactnum)^2
exact^2
5.28455E-05 1.376519104
5.06093E-05 1.376372375
4.47107E-05 1.375969799
3.63029E-05 1.375349156
2.67258E-05 1.374550603
1.73124E-05 1.373614904
9.22905E-06 1.372581101
3.40146E-06
1.37149089
3.87874E-07 1.370384441
3.40294E-07 1.369300983
2.98711E-06 1.368280729
7.65063E-06 1.367362998
1.33109E-05 1.366587664
1.87338E-05
1.36599351

eL2
0.004716

1.163841
1.163666
1.163972
1.164827
1.166302
1.168467
1.171393
1.175152
1.179818
1.185465
1.192167
1.2

1.1685977
1.1685502
1.1686332
1.1688655
1.1692677
1.1698619
1.170672
1.1717247
1.17305
1.1746819
1.1766587
1.2

0.0047567
0.0048842
0.0046612
0.0040385
0.0029657
0.0013949
-0.000721
-0.003427
-0.006768
-0.010783
-0.015508
0
sum

2.26259E-05
2.38558E-05
2.17267E-05
1.63091E-05
8.79565E-06
1.94587E-06
5.19787E-07
1.17466E-05
4.58056E-05
0.000116274
0.000240506
0
0.000794659

y
log x
0.125 0.0305402 -0.90309
0.0667 0.0044226 -1.175874
0.04 0.0047162 -1.39794

log y
-1.515128537
-2.354319287
-2.326408228

1.365620509
1.365509653
1.365703539
1.366246444
1.367187063
1.368576968
1.370473019
1.3729387
1.37604635
1.379877669
1.384525782
1.44
35.72706395

0
-1.5

-1

-0.5

-0.5

y = 1.6934x - 0.1027
R = 0.7754

log eL2

-1
-1.5
-2

Series1
Linear (Series1)

-2.5

log h

-3

The graph of log h against e L2 shows linear behavior with a slope or m=1.6934. The
correlation could be higher if additional iterations for the numerical method, as well as analytical
method, were done.

5) Conclusion
We therefore conclude that the numerical and analytical methods are very vital in solving
ODEs. Both require certain degree of knowledge and tools in performing the methods. In
performing any analytical procedure, one must be knowledgeable in the theories of mathematics
such as calculus in order to arrive at the correct equation. The numerical method requires the use
of a computer programming language, such as C++, in order to perform the analysis of an ODE
given the boundary conditions of the equation. Both might not display the same answers, but
definitely, both methods would give you different approaches to arrive at a solution.

References:
Chapra, Steven and Canale, Raymond, Numerical Methods for Engineers with Software
Programming Application. Fourth Edition, McGraw-Hill, New York, 2002.
Conte, S.D., and deBoor, C., Elementary Numerical Analysis, McGraw-Hill, New York,
1972.
Handouts for 3rd long exam of ES21

6) Appendix
C code for the Numerical Analysis
Header: (ce28.h)
#ifndef _CE28_H_
#define _CE28_H_
#include <stdlib.h>
typedef struct {
double *elem;
int size;
} vector;
typedef struct {
double **elem;
int rows;
int cols;
} matrix;
#endif
(ce28.c)
#include "ce28.h"
#include <stdio.h>
#include <math.h>
// INITIALIZING MEMORY ALLOCATIONS
vector init_vector(int m) {
vector tmp;
tmp.size = m;
tmp.elem = calloc(m,sizeof(double));
return tmp;
}
matrix init_matrix(int m, int n) {
int i;
matrix tmp;
tmp.rows = m;
tmp.cols = n;
tmp.elem = calloc(m,sizeof(double*));
for(i=0; i<m; i++)
tmp.elem[i]= calloc(n,sizeof(double));
return tmp;
}

//VECTORS AND MATRICES INPUT & OUTPUT

void alpha_write(char *name){


FILE *fp;
fp = fopen("file/output.txt", "w");
fprintf(fp, "%s", name);
fclose(fp);
}

vector scan_vector(vector A) {
int i;
FILE *fp;
fp = fopen("file/vector.txt", "r");
fscanf(fp, "%d", &A.size);
A = init_vector(A.size);
for (i = 0; i < A.size; i++){
fscanf(fp, "%lf", &A.elem[i]);
}
fclose(fp);
return A;
}
vector scan_vector1(vector A) {
int i;
FILE *fp;
fp = fopen("file\vector1.txt", "r");
fscanf(fp, "%d", &A.size);
A = init_vector(A.size);
for (i = 0; i < A.size; i++){
fscanf(fp, "%lf", &A.elem[i]);
}
fclose(fp);
return A;
}
matrix scan_matrix(matrix A) {
int i, j;
FILE *fp;
fp = fopen("file/matrix.txt", "r");
fscanf(fp, "%d %d", &A.rows, &A.cols);
A = init_matrix(A.rows, A.cols);

for (i = 0; i < A.rows; i++) {


for (j = 0; j < A.cols; j++){
fscanf(fp, "%lf", &A.elem[i][j]);
}
}
fclose(fp);
return A;
}
matrix scan_matrix1(matrix A) {
int i, j;
FILE *fp;
fp = fopen("file/matrix1.txt", "r");
fscanf(fp, "%d %d", &A.rows, &A.cols);
A = init_matrix(A.rows, A.cols);
for (i = 0; i < A.rows; i++) {
for (j = 0; j < A.cols; j++){
fscanf(fp, "%lf", &A.elem[i][j]);
}
}
fclose(fp);
return A;
}

// PRINTING MATRICES & VECTORS


void print_vector(vector A) {
int n;
FILE *fp;
fp = fopen("file/output.txt", "a");
for(n=0; n<A.size; n++)
fprintf(fp, "%lf\n", A.elem[n]);
fprintf(fp, "\n");
fclose(fp);
}
void print_matrix(matrix A) {
int m,n;
FILE *fp;
fp = fopen("file/output.txt", "a");
for(m=0; m<A.rows; m++) {
for(n=0; n<A.cols; n++)
fprintf(fp, "%lf\t", A.elem[m][n]);
fprintf(fp, "\n");
}

fprintf(fp, "\n");
fclose(fp);
}
void clear(){
FILE *fp;
fp = fopen("file/output.txt", "w");
fprintf(fp, "\n");
fclose(fp);
}
//FREE VECTORS AND MATRICES
void free_vector(vector A) {
free(A.elem);
}
void free_matrix(matrix A) {
int i;
for (i = 0; i < A.rows; i++)
free(A.elem[i]);
free(A.elem);
}
// VECTOR ADDITION :)
vector add_vector(vector A, vector B) {
int n;
if(A.size == B.size){
for(n=0; n<A.size; n++)
A.elem[n]+= B.elem[n];
}
else
printf("/nVector Addition cannot be performed");
return A;
}
// VECTOR SUBTRACTION :)
vector subtract_vector(vector A, vector B) {
int n;
printf("\nVECTOR SUBTRACTION :)\n");
if(A.size == B.size){
for(n=0; n<A.size; n++)
A.elem[n]-= B.elem[n];
}
else
printf("/nVector Subtraction cannot be performed");
return A;
}
// VECTOR MULTIPLICATION :)
vector scalmult_vector(vector A, double x) {
int n;

for(n=0; n<A.size; n++)


A.elem[n]*= x;
return A;
}
// VECTOR DOT PRODUCT :)
double dotprod_vector(vector A, vector B) {
int n;
double dotprod= 0;
if(A.size == B.size){
for(n=0; n< A.size; n++)
dotprod+= A.elem[n] * B.elem[n];
}
else
printf("/nDot Product cannot be performed");
return dotprod;
}
// VECTOR CROSS PRODUCT :)
vector crossprod_vector(vector A, vector B) {
vector tmp;
tmp= init_vector(3);
if(A.size==3 && B.size==3) {
tmp.elem[0]= A.elem[1]*B.elem[2]- A.elem[2]*B.elem[1];
tmp.elem[1]= A.elem[2]*B.elem[0]- A.elem[0]*B.elem[2];
tmp.elem[2]= A.elem[0]*B.elem[1]- A.elem[1]*B.elem[0];
}
else
printf("\nCross Product cannot be performed");
return tmp;
}
// MATRIX ADDITION
matrix add_matrix(matrix A, matrix B) {
int i, j;
if(A.rows==B.rows && A.cols==B.cols){
for(i=0; i<A.rows; i++) {
for(j=0; j<A.cols; j++)
A.elem[i][j] += B.elem[i][j];
}
}
else {
printf("\nMatrix Addition cannot be performed");
}
return A;
}
//MATRIX SUBTRACTION :)
matrix sub_matrix(matrix A, matrix B) {
int i, j;

if(A.rows==B.rows && A.cols==B.cols){


for(i=0; i<A.rows; i++) {
for(j=0; j<A.cols; j++)
A.elem[i][j] -= B.elem[i][j];
}
}
else
printf("\nMatrix Subtraction cannot be performed");
return A;
}
//MATRIX MULTIPLICATION :)
matrix mul_matrix(matrix A, matrix B) {
int i, j, k;
matrix tmp;
tmp = init_matrix(A.rows,B.cols);
if(A.cols==B.rows) {
for(i=0; i<A.rows; i++) {
for(j=0; j<B.cols; j++) {
tmp.elem[i][j]= 0;
for(k=0; k<A.cols; k++)
tmp.elem[i][j]+= A.elem[i][k]*B.elem[k][j];
}
}
}
else {
printf("\nMatrix Multiplication cannot be performed");
}
return tmp;
}
//MATRIX SCALAR MULTIPLICATION :)
matrix scalmul_matrix(matrix A, double x) {
int i, j;
for(i=0; i<A.rows; i++) {
for(j=0; j<A.cols; j++)
A.elem[i][j] *= x;
}
return A;
}
//MATRIX TRANSPOSITION :)
matrix trans(matrix A) {
int i, j;
matrix tmp;
tmp= init_matrix(A.cols,A.rows);
for(i=0; i<A.cols; i++) {
for(j=0; j<A.rows; j++)
tmp.elem[i][j] = A.elem[j][i];

}
return tmp;
}
// CROUT LU DECOMPOSITION
matrix crout_lu(matrix A){
matrix decom;
int a, b, c;
double sum = 0, prod;
decom = init_matrix(A.rows, A.cols);
for(a=0; a<A.cols; a++)
decom.elem[a][0] = A.elem[a][0];
for(b=1; b<A.cols; b++)
decom.elem[0][b] = A.elem[0][b]/decom.elem[0][0];
for(b=1; b<A.cols-1; b++){
for(a=b; a<A.rows; a++){
for(c=0; c<b; c++){
prod = decom.elem[a][c] * decom.elem[c][b];
sum = sum + prod;
}
decom.elem[a][b] = A.elem[a][b] - sum;
sum = 0;
}
for(c=b+1; c<A.rows; c++){
for(a=0; a<b; a++){
prod = decom.elem[b][a] * decom.elem[a][c];
sum = sum + prod;
}
decom.elem[b][c] = (A.elem[b][c] - sum)/decom.elem[b][b];
sum = 0;
}
}
for(c=0; c<A.rows-1; c++){ /*Solve for the last elment*/
prod = decom.elem[A.rows-1][c] * decom.elem[c][A.rows-1];
sum = sum + prod;
}
decom.elem[A.rows-1][A.rows-1] = A.elem[A.rows-1][A.rows-1] - sum;
return decom;
}

//L
matrix l(matrix A) {

int i, j;
matrix B,X;
X= init_matrix(A.rows,A.cols);
X=A;
B= init_matrix(A.rows,A.cols);
for (i=0; i<A.rows; i++)
for (j=0; j<A.cols; j++) {
if(i<j)
B.elem[i][j]= 0;
else
B.elem[i][j]= X.elem[i][j];
}
return B;
}
//U
matrix u(matrix A) {
int i, j;
matrix B,X;
X= init_matrix(A.rows,A.cols);
X=A;
B= init_matrix(A.rows,A.cols);
for (i=0; i<A.rows; i++)
for (j=0; j<A.cols; j++) {
if(i>j)
B.elem[i][j]= 0;
else if (i==j)
B.elem[i][j]= 1;
else if (i<j)
B.elem[i][j]= X.elem[i][j];
}
return B;
}
//FORWARD SUBSTITUTION :)
vector forwardsub(matrix A, vector B) {
int i,j;
if(A.rows == B.size) {
B.elem[0]/= A.elem[0][0];
for(i=1; i<B.size; i++) {
for(j=0; j<i; j++)
B.elem[i]-= A.elem[i][j] * B.elem[j];
B.elem[i]/= A.elem[i][i];
}
}
else
printf("\nForward Substitution cannot be performed");
return B;
}

//BACK SUBSTITUTION :)
vector backsub(matrix A, vector B) {
int i,j;
if (A.rows==B.size) {
for (i=B.size-2; i>=0; i--) {
for(j=i+1; j<B.size; j++)
B.elem[i]-= A.elem[i][j] * B.elem[j];
}
}
else
printf("\nBack Substitution cannot be performed.");
return B;
}
//THOMAS ALGORITHM
vector thomas_algo(matrix A, vector B) {
int k;
matrix X;
vector Y;
X=init_matrix(A.rows, A.cols);
Y=init_vector(B.size);
X=A; Y=B;
if (A.rows == B.size){
for(k=1; k<B.size; k++){
X.elem[k][0] = X.elem[k][0]/X.elem[k-1][1];
X.elem[k][1] = X.elem[k][1]-(X.elem[k][0]*X.elem[k-1][2]);
}
for(k=1; k<B.size; k++)
Y.elem[k] = Y.elem[k] - (X.elem[k][0]*Y.elem[k-1]);
Y.elem[B.size-1] = Y.elem[B.size-1]/X.elem[B.size-1][1];
for (k=B.size-2; k>=0; k--)
Y.elem[k] = (Y.elem[k] - (X.elem[k][2] * Y.elem[k+1])) / X.elem[k][1];
}
else
printf("Error.\n");

return Y;
}
(main)
#include<stdio.h>
#include<stdlib.h>

#include <math.h>
#include "ce28.h"
#include "ce28.c"
main()
{
matrix A,B,C;
matrix X; vector Y, Z;
clear();
A= init_matrix(5,5);
A= scan_matrix(A);
B= init_matrix(5,5);
C= init_matrix(5,5);
B= l(crout_lu(A));
C= u(crout_lu(A));
// print_matrix(A);
// print_matrix(B);
//print_matrix(C);
free_matrix(A);
free_matrix(B);
free_matrix(C);

X = init_matrix(5,5);
Y = init_vector(5);
X = scan_matrix(X);
Y = scan_vector(Y);
X = crout_lu(X);
A = l(X);
B = u(X);
Z = forwardsub(A,Y);
Z = backsub(B,Z);
print_matrix(X);
print_vector(Z);
free_matrix(X);

Вам также может понравиться