高斯赛德尔迭代法与雅克比迭代法在程序结构上相同,只是迭代的核心思想不同,下面以三个x变量、三个方程的方程组为例,多个的情况同样使用vector稍加修改就可以。
迭代公式(思想)如下:
程序流程图如下:
计算结果表如下:
/********高斯-赛德尔迭代法*********
*--------------分析--------------
*1.Xi为每一步迭代的初值,X1=X2=X3=0,Yi存放迭代结果,temp[i]保存迭代过程中x的老值
*2.迭代精度e,max|tempi-Yi|,1<=i<=n
*3.最大迭代次数N
*4.输入数据:
*(1)Bi:方程组的值
*(2)Aij:方程组变量系数
*/
#include<iostream>
#include<math.h>
using namespace std;
double sum(double x[4],double a[4][4],int i,int down,int top)
{
double res(0);
double temp_res=0;//当i=j时,令其为0
for(int j=down;j<=top;j++)
{
if(i==j)
{
res+=temp_res;
//cout<<"res:"<<res<<endl;
}
else
{
res+=a[i][j]*x[j];
//cout<<"res:"<<res<<endl;
}
}
return res;
}
//the max in an array
double max(double *array)
{
double max_var = array[1];
for(int i=1;i<4;i++)
{
if(max_var<array[i])
{
max_var=array[i];
}
}
//cout<<"max_var:"<<max_var<<endl;
return max_var;
}
void interation(unsigned N,double a[4][4],double *b,double x[4],unsigned k,double e)
{
int i=1;
double Y[4]={0,};
double temp[4]={0,};
for(;i<4;i++)
{
//核心思想:当i=2,k=1时,x2=(1/a22)[(b2-(Y[1]+x[3]))],x[i]初值全为0
//将新值保存到相应的x[i]中,求和时用的是x[i],所以用x[i]替换Y[i]
Y[i]=(b[i]-sum(x,a,i,1,3))/a[i][i];
//用一个临时数组保存x的值(老值),因为下一步x值被Y值覆盖
for(int k=0;k<4;k++)
temp[k]=x[k];
x[i]=Y[i];
cout<<"Y"<<i<<":"<<Y[i]<<endl;
}
double max_val[4];
for(i=1;i<4;i++)
{
max_val[i]=fabs(temp[i]-Y[i]);
}
double the_max=max(max_val);//get the max in the array
//cout<<"the max:"<<the_max<<endl;
if(the_max<e)
{
cout<<"---Iterating successfully-----"<<endl;
cout<<"----------------------------"<<endl;
cout<<"The values are as follows:"<<endl;
for(i=1;i<4;i++)
{
cout<<"Y["<<i<<"]:"<<Y[i]<<" ";
}
cout<<endl;
}
else
{
if(k==N)
{
cout<<"--!---Iteration failed---!--"<<endl;
}
else
{
k=k+1;
x=Y ;//x array=Y array
interation(N,a,b,x,k,e);
}
}
}
int main()
{
//store coefficients
double coeff[4][4]={{0,0,0,0},{0,10,-1,-2},{0,-1,10,-2},{0,-1,-1,5}};
//double coeff[4][4];
//store equations' value
double values[4]={0,7.2,8.3,4.2};
//double values[4];
double Y[4];//Y[i]
unsigned i,j;
double e;//precision
unsigned N;//iteration times
unsigned k=1;//flag
//先初始化array为0
memset(Y,0,sizeof(Y));
cout<<"---------------Gauss-Seidel Interaction----------------"<<endl;
cout<<"----Three equations and three variables X---------"<<endl;
/*
cout<<"Please input equations' coefficients and values:"<<endl;
cout<<"Please input coeffients from first equation to last:"<<endl;
for(i=1;i<4;i++)
{
for(j=1;j<4;j++)
cin>>coeff[i][j];
}
cout<<"---------------------------------------------"<<endl;
cout<<"Please input equations' values:"<<endl;
for(i=1;i<4;i++)
{
cin>>values[i];
}
*/
cout<<"-----------------------------------------------"<<endl;
cout<<"Please input precision e:"<<endl;
cin>>e;
cout<<"----------------------------------------------"<<endl;
cout<<"Please input interation times N:"<<endl;
cin>>N;
cout<<"----Inputting end....--------------------------"<<endl;
//迭代初值为0
double x[4]={0,0,0,0};
interation(N, coeff,values,x, k, e);
cout<<"----------------"<<endl;
cout<<"Hello Boker..2021/5/12/0:05"<<endl;
return 0;
}
运行结果如下: