这是我写的一个作业 你拿去改改吧
jacobi
#include
using namespace std;
#include
#define N 3
int main()
{
int i,j;
double a[N][N]={
{10,-1,-2},
{-1,10,-2},
{-1,-1,5}};
double b[N]={7.2,8.3,4.2};
double x[N]={0},tm;
double xnew[N]={0};
double max;
int m=0;
while(++m)
{
cout<
for(i=0;i
{
tm=0;
for(j=0;j
tm+=x[j]*a[i][j];
xnew[i]=(b[i]-tm)/a[i][i];
cout<<" x["< }
cout<
if(fabs(x[j]-xnew[j])>max)
max=fabs(x[j]-xnew[j]);
x[j]=xnew[j];
}
if(max<0.000001)
break;
}
return 1;
}
gussi
#include
using namespace std;
#include
#define N 3
int main()
{
int i,j;
double a[N][N]={
{10,-1,-2},
{-1,10,-2},
{-1,-1,5}};
double b[N]={7.2,8.3,4.2};
double x[N]={0},tm;
int m=0;
double max;
while(++m)
{
cout<
for(i=0;i
{
tm=0;
for(j=0;j
tm+=x[j]*a[i][j];
if(fabs((b[i]-tm)/a[i][i]-x[i])>max)
max=fabs((b[i]-tm)/a[i][i]-x[i]);
x[i]=(b[i]-tm)/a[i][i];
cout<<" x["< }
cout<
break;
}
return 1;
}