编程:用高斯-赛德尔迭代法解方程

来源:百度知道 编辑:UC知道 时间:2024/09/22 04:23:08
帮帮忙,急需用。谢谢

给出一个具体的方程,不然很难得编的!

你给多少分啊

程序在win-tc和tc2.0下试验均通过.
/*
高斯消去法求线性方程组Ax=b的解
本题的方程组为
2x0+6x1-x2=-12
5x0-x1+2x2=29
-3x0-4x1+x2=5
实际中你自己输入方程各系数并更改n即未知数x的个数
*/
#include <stdio.h>
#include <stdlib.h>
/*#include <malloc.h>*/
#include <math.h>

int GS(int,double**,double *,double);
double **TwoArrayAlloc(int,int);
void TwoArrayFree(double **);

void main()
{
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("Allocation 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("Cannot solve this problem!\n"