-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjacobi.c
61 lines (50 loc) · 1.44 KB
/
jacobi.c
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
#include <stdio.h>
void jacobiMethod(int n, double A[n][n], double b[n], double x[n], int maxIter) {
double tempX[n];
for (int iter = 0; iter < maxIter; iter++) {
for (int i = 0; i < n; i++) {
tempX[i] = b[i];
for (int j = 0; j < n; j++) {
if (i != j) {
tempX[i] -= A[i][j] * x[j];
}
}
tempX[i] /= A[i][i];
}
for (int i = 0; i < n; i++) {
x[i] = tempX[i];
}
}
}
int main() {
int n;
printf("Enter the size of the system of equations: ");
scanf("%d", &n);
double A[n][n];
double b[n];
double x[n];
int maxIter;
printf("Enter the coefficients of the matrix A:\n");
for (int i = 0; i < n; i++) {
printf("Row %d: ", i + 1);
for (int j = 0; j < n; j++) {
scanf("%lf", &A[i][j]);
}
}
printf("Enter the constants of the right-hand side vector b:\n");
for (int i = 0; i < n; i++) {
printf("Element %d: ", i + 1);
scanf("%lf", &b[i]);
}
printf("Enter the maximum number of iterations: ");
scanf("%d", &maxIter);
for (int i = 0; i < n; i++) {
x[i] = 0.0;
}
jacobiMethod(n, A, b, x, maxIter);
printf("Solution:\n");
for (int i = 0; i < n; i++) {
printf("x[%d] = %lf\n", i + 1, x[i]);
}
return 0;
}