线性方程组迭代法之高斯-赛德尔迭代法

高斯赛德尔迭代法与雅克比迭代法在程序结构上相同,只是迭代的核心思想不同,下面以三个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;
}

运行结果如下:
在这里插入图片描述

  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值