⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c++jacobi.txt

📁 用C++编写的一个jacobi迭代的求解
💻 TXT
字号:
#include   <iostream>   
#include   <cmath>   
using   namespace   std;   
int   a,b,m;   
 double   *x0;   
  void   Jacobi(double   **c,double   *d,int   n,double   eps);   
   void   main()   
  {   
  int   n;   
  double   **A,*B;   
          double   e;   
   cin>>n;   
  cout<<"输入方程组的变量的个数以及方程的个数!"<<endl;   
  cin>>a>>b;   
  A=new   double*[b];   
  for(int   i=0;i<b;i++)   
  {   
  A[i]=new   double[a];   
  }   
  B=new   double[b];   
  x0=new   double[a];   
  cout<<"输入每个方程组的变量的系数以及方程右端的值!"<<endl;   
  for(int   k=0;k<b;k++)   
  {   
  for(int   j=0;j<a;j++)   
  {   
  cin>>A[k][j];   
  cout<<"A["<<k<<j<<"]="<<A[k][j]<<endl;   
  }   
  cin>>B[k];   
  cout<<"B["<<k<<"]="<<B[k]<<endl;   
  }   
  cout<<"输入方程组迭代的次数及所要求的精度!"<<endl;   
  cin>>m>>e;   
  cout<<"输入方程组迭代的初值!"<<endl;   
  for(int   j=0;j<a;j++)   
  {   
  cin>>x0[j];   
  }   
Jacobi(A,B,m,e);   
  }   
    
    void   Jacobi(double   **c,double   *d,int   n,double   eps)   
    {   
    int   k,i;   
            double   *y   =   new   double[a],*x=new   double[a],s,temp=0.0;   
    k=1;   
    while(1)   
    {   
    temp   =   0.0;   
    for(i=0;i<a;i++)   
    {   
    s=0.0;   
    for(int   j=0;j<a;j++)   
    {   
    if(j!=i)   
    {   
    s+=c[i][j]*x0[j];   
    }   
    }   
    s=(d[i]-s)/c[i][i];   
    y[i]=s;   
    if(fabs(x0[i]-s)>temp)   
    {   
    temp=fabs(x0[i]-s);   
    }   
    }   
    if(temp<eps)   
    {   
    cout<<"迭代成功!迭代结果为:"<<endl;   
    for(i=0;i<a;i++)   
    {   
    cout<<"y["<<i<<"]   ="<<y[i]<<endl;   
    }   
    break;   
    }   
    if(k==m)   
    {   
    cout<<"迭代失败!!"<<endl;   
    break;   
    }   
    k+=1;   
    for(i=0;i<a;i++)   
    {   
    x0[i]=y[i];   
    }   
    }   
    }   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -