-
Notifications
You must be signed in to change notification settings - Fork 0
/
Week5.c
73 lines (69 loc) · 1.78 KB
/
Week5.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_ITERATIONS 1000
#define MAX_SIZE 100
#define TOLERANCE 1e-6
double A[MAX_SIZE][MAX_SIZE]; // 系数矩阵
double b[MAX_SIZE]; // 常数向量
double x[MAX_SIZE] = {0.0}; // 初始解向量
double x_new[MAX_SIZE] = {0.0}; // 新的解向量
int i, j, k;
int n;
double sum1, sum2;
int main()
{ // 方程组的维数
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
scanf("%lf", &A[i][j]);
}
}
for (int i = 0; i < n; i++)
{
scanf("%lf", &b[i]);
}
for (k = 0; k < MAX_ITERATIONS; k++)
{
// 求解新的解向量
for (i = 0; i < n; i++)
{
sum1 = 0.0;
sum2 = 0.0;
for (j = 0; j < i; j++)
{
sum1 += A[i][j] * x_new[j];
}
for (j = i + 1; j < n; j++)
{
sum2 += A[i][j] * x[j];
}
x_new[i] = (b[i] - sum1 - sum2) / A[i][i];
}
// 判断是否达到精度要求
double norm = 0.0;
for (i = 0; i < n; i++)
{
norm += pow(x_new[i] - x[i], 2);
}
norm = sqrt(norm);
if (norm < TOLERANCE)
{
printf("Solution converged after %d iterations:\n", k + 1);
for (i = 0; i < n; i++)
{
printf("x[%d] = %f\n", i, x_new[i]);
}
return 0;
}
// 更新解向量
for (i = 0; i < n; i++)
{
x[i] = x_new[i];
}
}
printf("Solution did not converge after %d iterations.\n", MAX_ITERATIONS);
return 0;
}