-
Notifications
You must be signed in to change notification settings - Fork 0
/
GaussianElimination.c
92 lines (86 loc) · 1.65 KB
/
GaussianElimination.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
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <math.h>
int n=4; //未知数个数
int main(void)
{
double a[n][n+1];
int f=1;
int bj=0;
double count;
int f1=1;
double bs;
double x[n];
//输入矩阵元素
for (int i = 0; i < n; i++)
{
printf("请输入第 %d 行",i+1);
for (int j = 0; j < n+1; j++)
{
scanf("%lf",&a[i][j]);
}
}
//转换为上三角矩阵
for (int i = 0; i < n; i++)
{
f=1;
//保证对角线非0
if(a[i][i]==0)
{
f=0;
for (int k = i+1; k < n; k++)
{
if(a[k][i] != 0)
{
bj=k;
f=1;
}
}
if (f==1)
{
//交换行
for (int q = 0; q < n+1; q++)
{
count=a[bj][q];
a[bj][q]=a[i][q];
a[i][q]=count;
}
}
else printf("erro");
}
//消元
for (int k = i+1; k < n; k++)
{
bs=a[k][i]/a[i][i];
for (int j = 0; j < n+1; j++)
{
a[k][j]=a[k][j]-bs*a[i][j];
}
}
}
//输出上三角矩阵
printf("上三角矩阵为:\n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n+1; j++)
{
printf("%.3lf ",a[i][j]);
}
printf("\n");
}
//求解
for (int i = n-1; i >=0; i--)
{
count=a[i][n];
for (int j= i+1; j<n ;j++)
{
count-=a[i][j]*x[j];
}
x[i]=count/a[i][i];
}
//输出解
printf("解为:\n");
for (int i = 0; i < n; i++)
{
printf("x[%d]=%.3lf\n",i+1,x[i]);
}
}