#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
#define kk 50 //定义最大方程元数
#define exp 1e-5//定义结束门限值
int n,ll,hh,i,j;
double A[kk][kk],x[kk][kk],b[kk],y[kk],a[kk],z[kk],m,nn,d,e=1,w;
void main()
{
cout<<" **********************************************************************"<<endl;
cout<<" ***本程序可以用雅可比迭代法,塞德尔迭代法,逐次超松弛法求解线性方程组***"<<endl;
cout<<" ***************************制作人:李小龙*****************************"<<endl;
cout<<" ***********************说明:方程最多个数为**************************"<<endl;
cout<<" **********************************************************************"<<endl;
//*********************************数据的输入********************************
bb:cout<<"输入的方程元数"<<endl;
cin>>n;
cout<<"请输入方程系数矩阵:"<<"/n";
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>A[i][j];
cout<<"请输入右边向量:"<<"/n";
for(i=0;i<n;i++)
cin>>b[i];
//*******************************判断是否对角占优*************************
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
nn=0;
if(i==j)
{
d=fabs(A[i][i]);
}
else
nn=nn+fabs(A[i][j]);
}
if(nn>d)
{
cout<<"该方程不对角占优,迭代不收敛"<<endl;
cout<<"是否继续?是(),否()"<<endl;
cin>>hh;
if(hh!=1)
goto bb;
else exit(1);
}
}
//********************************计算出迭代矩阵*************************
for(i=0;i<n;i++)
{
b[i]=b[i]/A[i][i];
for(j=0;j<n;j++)
{
if(i==j)
{
x[i][i]=0;
}
else
{
x[i][j]=-A[i][j]/A[i][i];
}
}
}
//*******************************输出迭代矩阵*****************************
cout<<"计算出迭代矩阵为:"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cout<<x[i][j]<<" ";
cout<<b[i]<<" ";
cout<<endl;
}
//****************************迭代方法的选择*****************************
cout<<"请你选择迭代方法!"<<endl;
cout<<endl;
cout<<endl;
cout<<"选用雅可比迭代法,请输入()!"<<endl;
cout<<endl;
cout<<endl;
cout<<"选用塞德尔迭代法,请输入()!"<<endl;
cout<<endl;
cout<<endl;
cout<<"选用逐次超松弛法,请输入()!"<<endl;
cout<<endl;
cout<<endl;
cin>>ll;
//*****************************赋迭代初值***********************************
for(i=0;i<n;i++)
{
y[i]=1;
z[i]=1;
}
int f=1;
switch(ll)
{
case 1:goto cc;
break;
case 2:goto aa;
break;
case 3:goto dd;
}
//***************************雅可比迭代法************************************
cc:while(e>exp)
{
for(i=0;i<n;i++)
{
z[i]=y[i];
nn=0;
for(j=0;j<n;j++)
{
nn=nn+x[i][j]*z[j];
y[i]=nn+b[i];
}
e=fabs(z[i]-y[i]);
if(i==0)
{
cout<<"第"<<f++<<"次迭代";
}
cout<<setw(8)<<setprecision(8)<<y[i]<<" ";
}
cout<<endl;
}
cout<<endl;
cout<<endl;
cout<<"最后结果为:"<<endl;
cout<<endl;
for(i=0;i<n;i++)
{
cout<<"X"<<"["<<i<<"]"<<"="<<y[i];
cout<<endl;
}
exit(1);
//***********************************塞德尔迭代法********************************
aa: while(e>exp)
{
for(i=0;i<n;i++)
{
z[i]=y[i];
nn=0;
for(j=0;j<n;j++)
{
nn=nn+x[i][j]*y[j];
y[i]=nn+b[i];
}
e=fabs(z[i]-y[i]);
if(i==0)
{
cout<<"第"<<f++<<"次迭代";
}
cout<<setw(8)<<setprecision(8)<<y[i]<<" ";
}
cout<<endl;
}
cout<<endl;
cout<<endl;
cout<<"最后结果为:"<<endl;
cout<<endl;
for(i=0;i<n;i++)
{
cout<<"X"<<"["<<i<<"]"<<"="<<y[i];
cout<<endl;
}
exit(1);
//***********************************逐次超松弛法********************************
dd: cout<<"输入加速因子W:"<<endl;
cin>>w;
while(e>exp)
{
for(i=0;i<n;i++)
{
z[i]=y[i];
nn=0;
for(j=0;j<n;j++)
{
nn=nn+x[i][j]*y[j];
y[i]=(1-w)*z[i]+w*(nn+b[i]);
}
e=fabs(z[i]-y[i]);
if(i==0)
{
cout<<"第"<<f++<<"次迭代";
}
cout<<setw(8)<<setprecision(8)<<y[i]<<" ";
}
cout<<endl;
}
cout<<endl;
cout<<endl;
cout<<"最后结果为:"<<endl;
cout<<endl;
for(i=0;i<n;i++)
{
cout<<"X"<<"["<<i<<"]"<<"="<<y[i];
cout<<endl;
}
exit(1);
}
//**************************************程序结束****************************************