-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjacobi1.c
More file actions
260 lines (225 loc) · 5.31 KB
/
jacobi1.c
File metadata and controls
260 lines (225 loc) · 5.31 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*************** PROGRAM STARTS HERE ***************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/********* DEFINED CONSTANTS *********/
#define MAX 15
#define MAXC 0
/********* FUNCTION DECLARATION *********/
void jacobi(float co[][MAX], float con[], float x[], int n);
void gaussjori(float co[MAX][MAX], float ai[MAX][MAX], int n);
void matrixm1(float matrix[MAX][MAX], float con[MAX], int n, float mat[MAX]);
void matrixm(float matrix[][MAX], float con[][MAX], int n, float mat[][MAX]);
void diadomatrix(float matrix[MAX][MAX], int n);
/********* MAIN STARTS HERE *********/
int main(void)
{
int i = 0, j, n; //Declaration of variables in int
float con[MAX], x[MAX]; //Declaration of arrays in float
float co[MAX][MAX]; //Declaration of 2d-arrays in float
// Input Section
printf("Enter the order of matrix:- "); //Taking order of the matrix
scanf("%d", &n);
while (i < n) //Check condition
{
printf("Enter Equation no. %d\n", i+1);
for (j = 0; j < n; j++)
{
printf("Enter the co-efficient of x%d:- ", j+1);
scanf("%f", &co[i][j]);
}
printf("Enter the value:- ");
scanf("%f", &con[i]);
printf("\n");
i++; // Incrementing i
}
diadomatrix(co, n);
printf("Enter the guess matrix: \n");
for (i = 0; i < n; i++)
{
printf("Enter value%d: ", i+1);
scanf("%f", &x[i]);
}
jacobi(co, con, x, n);
exit(0);
}
/********* FUNCTION DEFINITION *********/
void jacobi(float co[MAX][MAX], float con[MAX], float x[MAX], int n)
{
int i, j, check = 0;
float C[MAX], sol[MAX], tmp[MAX];
float l_u[MAX][MAX], d[MAX][MAX], T[MAX][MAX], di[MAX][MAX];
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (i == j)
{
d[i][j] = co[i][j];
}
else
{
l_u[i][j] = co[i][j];
}
}
}
gaussjori(d, di, n);
matrixm1(di, con, n, C);
matrixm(di, l_u, n, T);
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
T[i][j] = -T[i][j];
}
}
while (1)
{
matrixm1(T, x, n, tmp);
for (i = 0; i < n; i++)
{
sol[i] = tmp[i] + C[i];
if (floor(sol[i]*1000000) == floor(x[i]*1000000))
{
check = check + 1;
}
}
if (check == n)
{
break;
}
for (i = 0; i < n; i++)
{
x[i] = sol[i];
}
check = 0;
}
for (i = 0; i < n; i++)
{
printf("value of x%d is %f\n", i+1, sol[i]);
}
return ;
}
void gaussjori(float co[MAX][MAX], float li[MAX][MAX], int n)
{
int i, j, k = 0; //Declaration of variables in int
float val; //Declaration of variables in float
float ai[MAX][MAX]; //Declaration of 2d-arrays in float
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
ai[i][j] = 0;
if (i == j)
{
ai[i][j] = 1; //Initializing a identity matrix
}
}
}
while (k < n) //Check condition
{
for (i = 0; i < n; i++)
{
if (i == k) //check condition
{
continue; //Skipping the iteration
}
val = co[i][k]/co[k][k];
for (j = 0; j < n+1; j++)
{
co[i][j] = (co[i][j]-val*co[k][j]); //Applying row transformations
ai[i][j] = (ai[i][j]-val*ai[k][j]); //Applying row transformations
}
}
k++; //Incremnting k
}
for (i = 0; i < n; i++)
{
if (co[i][i] == 0)
{
printf("The given system of equations have no ");
printf("or infinitely many solutions.\n");
exit(1);
}
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
li[i][j] = (ai[i][j]/co[i][i]); //Normalizing
}
}
return ;
}
void matrixm1(float matrix[MAX][MAX], float con[MAX], int n, float mat[MAX])
{
int i, j, q;
float ele;
float val = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < 1; j++)
{
for (q = 0; q < n; q++)
{
ele = matrix[i][q] * con[q];
val += ele;
}
mat[i] = val;
val = 0;
}
}
return ;
}
void matrixm(float matrix[][MAX], float con[][MAX], int n, float mat[][MAX])
{
int i, j, q;
float ele;
float val = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
for (q = 0; q < n; q++)
{
ele = matrix[i][q] * con[q][j];
val += ele;
}
mat[i][j] = val;
val = 0;
}
}
return ;
}
void diadomatrix(float matrix[MAX][MAX], int n)
{
int i, j, dd = 0, dd1 = 0;
float val = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (i == j)
{
continue;
}
val += fabsf(matrix[i][j]);
}
if (fabsf(matrix[i][i]) > val)
{
dd = dd + 1;
dd1 = dd1 + 1;
}
else if (fabs(matrix[i][i]) == val)
{
dd1 = dd1 + 1;
}
val = 0;
}
if (dd != n && dd1 != n)
{
printf("Roots doesn't converge by this method, they diverge instead.\n");
exit(1);
}
return ;
}