作业帮 > 综合 > 作业

用C语言写一个高斯消元法解方程组的程序

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/03 10:37:39
用C语言写一个高斯消元法解方程组的程序
不要用列主消元的啊
就是化成上三角阵直接求的那种
我们以方程组 2x1 + 6x2 - x3 = -12
5x1 - x2 +2x3 = 29
-3x1 - 4x2 + x3 = 5
为例 来说明楼主自己把方程组化为矩阵形式.以下为源代码 .
#include
#include
#include
#include
int GS(int,double**,double *,double);
double **TwoArrayAlloc(int,int);
void TwoArrayFree(double **);
int main(void)
{
int i,n;
double ep,**a,*b;
n = 3;
ep = 1e-4;
a = TwoArrayAlloc(n,n);
b = (double *)calloc(n,sizeof(double));
if(b == NULL)
{
printf("memory get error\n");
exit(1);
}
a[0][0]= 2; a[0][1]= 6; a[0][2]= -1;
a[1][0]= 5; a[1][1]=-1; a[1][2]= 2;
a[2][0]=-3; a[2][1]=-4; a[2][2]= 1;
b[0] = -12; b[1] = 29; b[2] = 5;
if(!GS(n,a,b,ep))
{
printf("can't solve it with GS elimination\n");
exit(0);
}
printf("The solution of equations is as follows:\n");
for(i=0;i
再问: 能不能调整成输入任意一个矩阵的形式呢? 谢了!
再答: 无非要多加个矩阵维数和存储数组的维数的输入。你在我的基础上作点修改啊。这又不算难