数值分析 C语言编程 求助!!谁是牛人来解决一下

2024-12-18 04:51:02
推荐回答(1个)
回答1:

这是我写的一个作业 你拿去改改吧
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< max=0;
for(i=0;i
{
tm=0;
for(j=0;j if(i!=j)
tm+=x[j]*a[i][j];
xnew[i]=(b[i]-tm)/a[i][i];
cout<<" x["< }
cout< for(j=0;j {
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< max=0;
for(i=0;i
{
tm=0;
for(j=0;j if(i!=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< if(max<0.000001)
break;

}
return 1;
}