-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jacobi.c
83 lines (70 loc) · 1.87 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdio.h>
#include <math.h>
#define N 3 // Number of equations
int main()
{
// Define the coefficient matrix A and the right-hand side vector b
double A[N][N] = {
{27, 6, -1},
{6, 15, 2},
{1, 1, 54}};
double b[N] = {85, 72, 110};
// Initialize the solution vector x with zeros
double x[N] = {0};
// Define the maximum number of iterations and the tolerance
int max_iterations = 100;
double tolerance = 1e-6;
printf("Iteration\t");
for (int i = 0; i < N; i++)
{
printf("x%d\t\t", i + 1);
}
printf("\n");
// Jacobi iterative method
for (int iteration = 1; iteration <= max_iterations; iteration++)
{
printf("%d\t\t", iteration);
double x_new[N] = {0};
// Calculate the new values of x
for (int i = 0; i < N; i++)
{
x_new[i] = b[i];
for (int j = 0; j < N; j++)
{
if (j != i)
{
x_new[i] -= A[i][j] * x[j];
}
}
x_new[i] /= A[i][i];
}
// Print the values of x for this iteration
for (int i = 0; i < N; i++)
{
printf("%0.6lf\t", x_new[i]);
}
printf("\n");
// Check for convergence
int converged = 1;
for (int i = 0; i < N; i++)
{
if (fabs(x_new[i] - x[i]) > tolerance)
{
converged = 0;
break;
}
}
// Update x with the new values
for (int i = 0; i < N; i++)
{
x[i] = x_new[i];
}
// If converged, exit the loop
if (converged)
{
printf("Converged\n");
break;
}
}
return 0;
}