0% found this document useful (0 votes)
75 views

Thomas Algorithm in C

This C program defines matrices M, r, G, R, and x. It initializes M as a 5x5 matrix with values based on row and column indexes. It initializes r as a column vector of 1s. It then calculates G and R using forward substitution on M and r. Finally, it calculates x using back substitution on M, G, and R.

Uploaded by

Julius Baniqued
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Thomas Algorithm in C

This C program defines matrices M, r, G, R, and x. It initializes M as a 5x5 matrix with values based on row and column indexes. It initializes r as a column vector of 1s. It then calculates G and R using forward substitution on M and r. Finally, it calculates x using back substitution on M, G, and R.

Uploaded by

Julius Baniqued
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

#include <stdio.h>
#include <stdlib.h>
#include "matrix.h"
main()
{
Matrix M, x, r, G, R;
int i, j, N;
M.row = 5;
M.col = 5;
N = M.row;
M.mat=(double **)malloc(sizeof(double *)*M.row);
for(i=0;i<M.row;i++)
{
M.mat[i]=(double*)malloc(sizeof(double)*M.col);
}
for (i=0;i<N;i++)
{
for (j=0;j<N;j++)
{
if (i==j)
M.mat[i][j] =
else if (i==j+1)
M.mat[i][j] =
else if (j==i+1)
M.mat[i][j] =
else
M.mat[i][j] =
}
}

4;
-1;
-1;
0;

print_mat(M);
printf("\n\n\n");
r.row = N;
r.col = 1;
r.mat=(double **)malloc(sizeof(double *)*r.row);
for(i=0;i<r.row;i++)
{
r.mat[i]=(double*)malloc(sizeof(double)*r.col);
}
for(i=0;i<N;i++)
{
r.mat[i][0] = 1;
}
print_mat(r);
printf("\n\n\n");
G.row = N;
G.col = 1;
G.mat=(double **)malloc(sizeof(double *)*G.row);
for(i=0;i<G.row;i++)
{
G.mat[i]=(double*)malloc(sizeof(double)*G.col);
}
R.row = N;
R.col = 1;
R.mat=(double **)malloc(sizeof(double *)*R.row);
for(i=0;i<R.row;i++)
{
R.mat[i]=(double*)malloc(sizeof(double)*R.col);

67
}
68
69
G.mat[0][0] = M.mat[0][1] / M.mat[0][0];
70
R.mat[0][0] = r.mat[0][0] / M.mat[0][0];
71
72
for (i = 1; i < N-1; i++)
73
{
74
G.mat[i][0] = M.mat[i][i+1] / (M.mat[i][i] - M.mat[i][i-1] * G.mat[i-1][0]);
75
R.mat[i][0] = (r.mat[i][0] - M.mat[i][i-1] * R.mat[i-1][0]) / (M.mat[i][i] - M.mat[i][i-1] *
G.mat[i-1][0]);
76
}
77
78
G.mat[N-1][0] = 0;
79
R.mat[N-1][0] = (r.mat[N-1][0] - M.mat[N-1][N-2] * R.mat[N-2][0]) / (M.mat[N-1][N-1] - M.mat[N-1][N-2]
* G.mat[N-2][0]);
80
81
print_mat(G);
82
printf("\n\n\n");
83
84
print_mat(R);
85
printf("\n\n\n");
86
87
x.row = N;
88
x.col = 1;
89
x.mat=(double **)malloc(sizeof(double *)*x.row);
90
for(i=0;i<x.row;i++)
91
{
92
x.mat[i]=(double*)malloc(sizeof(double)*x.col);
93
}
94
95
x.mat[N-1][0] = R.mat[N-1][0];
96
i = N -2;
97
while (i > -1)
98
{
99
x.mat[i][0] = R.mat[i][0] - G.mat[i][0] * x.mat[i+1][0];
100
i = i-1;
101
}
102
103
print_mat(x);
104
printf("\n\n\n");
105 }

You might also like