雅可比迭代法(c++ 数值分析)

#include
#include
#include
using namespace std;
const int n=4;
int main()
{
 float a[n][n],b[n];
 int i,j;
    void yacobi(float a[][n],float b[n]);          //声明
 cout<<"please input a[i][j]:"<  for(i=0;i   for(j=0;j    cin>>a[i][j];
 cout<  cout<<"please input b[i]:"<  for(i=0;i   cin>>b[i];
 cout<<'/n';
 yacobi(a,b);                                   //调用
 return 0;
}


void yacobi(float a[][n],float b[n])
{int i,j;
 float x1[n],x2[n],sum,max,cha;
 for(i=0;i   x1[i]=1/a[i][i]*b[i];
 do
 {
      for(i=0;i    {
             sum=0;
             for(j=0;j            sum+=a[i][j]*x1[j];
       x2[i]=x1[i]+1/a[i][i]*(b[i]-sum);
    cout<    }
   cout<       max=fabs(x1[0]-x2[0]);
   for(i=1;i     if(max<(cha=fabs(x1[i]-x2[i]))) 
       max=cha;
   for(i=0;i     x1[i]=x2[i];
 }while(max>0.00001);
}

 

你可能感兴趣的:(雅可比迭代法(c++ 数值分析))