数值计算方法实验报告班级:姓名:序号:1.实验内容(1)高斯消去法(2)全主元消去法(3)列主元消去法(4)Doolittle分解法(5)追赶法(6)Jacobi迭代法(7)Gauss-Seidel迭代法2.实验过程(源程序代码)(1)高斯消去法/*求解线性方程组--高斯消去法*/#includeiostreamusingnamespacestd;classCGauss{private:float**a;float*x;intn;public:CGauss(introw);~CGauss();voidInput();voidOutput();voidCalculate();};CGauss::CGauss(introw){n=row;a=newfloat*[n];x=newfloat[n];for(inti=0;in;i++)a[i]=newfloat[n+1];}CGauss::~CGauss(){delete[]x;for(inti=0;in;i++){deletea[i];}deletea;}/*输入过程*/voidCGauss::Input(){cout输入增广矩阵的各值:endl;for(inti=0;in;i++)for(intj=0;jn+1;j++)cina[i][j];}/*输出过程*/voidCGauss::Output(){cout解向量为:endl;for(inti=0;in;i++)coutx[i]=a[i][n]endl;}voidCGauss::Calculate(){/*循环变量*/inti,j,k;//消去过程for(k=0;kn-1;k++){for(i=k+1;in;i++){for(j=k+1;jn+1;j++){a[i][j]=a[i][j]-a[i][k]*a[k][j]/a[k][k];}}}//回代过程floattemp;a[n-1][n]=a[n-1][n]/a[n-1][n-1];for(i=0,k=n-2;k=0;i++,k--){temp=0;for(j=k+1;jn;j++){temp=temp+a[k][j]*a[j][n];}a[k][n]=(a[k][n]-temp)/a[k][k];}}intmain(){cout高斯消去法解线性方程组:endlendl;/*系数矩阵的行数*/intn;cout输入方程的阶数:;cinn;CGaussobj(n);obj.Input();obj.Calculate();obj.Output();return0;}(2)全主元消去法/*求解线性方程组--全主元消去法*/#includeiostreamusingnamespacestd;classCGauss{private:float**a;float*x;int*z;/*z为记录列交换过程的数组下标,用在最后理顺解向量下标*/intn;public:CGauss(introw);~CGauss();voidInput();voidOutput();voidCalculate();};CGauss::CGauss(introw){n=row;a=newfloat*[n];x=newfloat[n];z=newint[n];for(inti=0;in;i++){a[i]=newfloat[n+1];z[i]=i;//对数组下标进行初始化}}CGauss::~CGauss(){delete[]x;delete[]z;for(inti=0;in;i++){deletea[i];}deletea;}/*输入过程*/voidCGauss::Input(){cout输入增广矩阵的各值:endl;for(inti=0;in;i++)for(intj=0;jn+1;j++)cina[i][j];}/*输出过程*/voidCGauss::Output(){cout解向量为:endl;for(inti=0;in;i++)coutx[i]=a[i][n]endl;}voidCGauss::Calculate(){/*循环变量*/inti,j,k;//消去过程floattemp;introw,col;for(k=0;kn-1;k++){temp=0;row=0;col=0;/*选主元*/for(i=k;in;i++){for(j=k;jn;j++){if(a[i][j]temp){temp=a[i][j];row=i;col=j;}}}if(0==temp){cout系数矩阵为奇异阵!/n;return;}/*行交换*/if(row!=k){for(j=k;jn+1;j++){temp=a[k][j];a[k][j]=a[row][j];a[row][j]=temp;}}/*列交换*/z[k]=k;if(col!=k){z[k]=col;for(i=0;in;i++){temp=a[i][k];a[i][k]=a[i][col];a[i][col]=temp;}}for(i=k+1;in;i++){for(j=k+1;jn+1;j++){a[i][j]=a[i][j]-a[i][k]*a[k][j]/a[k][k];}}}/*回代过程*/a[n-1][n]=a[n-1][n]/a[n-1][n-1];for(k=n-2;k=0;k--){temp=0;for(j=k+1;jn;j++){temp=temp+a[k][j]*a[j][n];}a[k][n]=(a[k][n]-temp)/a[k][k];}/*理顺解向量下标*/for(k=n-2;k=0;k--){if(z[k]!=k){temp=a[z[k]][n];a[z[k]][n]=a[k][n];a[k][n]=temp;}}}intmain(){cout全主元消去法解线性方程组:endlendl;/*系数矩阵的行数*/intn;cout输入方程的阶数:;cinn;CGaussobj(n);obj.Input();obj.Calculate();obj.Output();return0;}(3)列主元消去法/*求解线性方程组--高斯列主元消去法*/#includeiostreamusingnamespacestd;classCGauss{private:float**a;float*x;intn;public:CGauss(introw);~CGauss();voidInput();voidOutput();voidCalculate();};CGauss::CGauss(introw){n=row;a=newfloat*[n];x=newfloat[n];for(inti=0;in;i++)a[i]=newfloat[n+1];}CGauss::~CGauss(){delete[]x;for(inti=0;in;i++){deletea[i];}deletea;}/*输入过程*/voidCGauss::Input(){cout输入增广矩阵的各值:endl;for(inti=0;in;i++)for(intj=0;jn+1;j++)cina[i][j];}/*输出过程*/voidCGauss::Output(){cout解向量为:endl;for(inti=0;in;i++)coutx[i]=a[i][n]endl;}voidCGauss::Calculate(){/*循环变量*/inti,j,k;//消去过程floattemp;intt;for(k=0;kn-1;k++){temp=a[k][k];t=k;for(i=k+1;in;i++){if(a[i][k]temp){temp=a[i][k];t=i;}}if(0==temp){cout系数矩阵为奇异阵!/n;return;}if(t!=k){for(j=k;jn+1;j++){temp=a[k][j];a[k][j]=a[t][j];a[t][j]=temp;}}for(i=k+1;in;i++){for(j=k+1;jn+1;j++){a[i][j]=a[i][j]-a[i][k]*a[k][j]/a[k][k];}}}//回代过程a[n-1][n]=a[n-1][n]/a[n-1][n-1];for(k=n-2;k=0;k--){temp=0;for(j=k+1;jn;j++){temp=temp+a[k][j]*a[j][n];}a[k][n]=(a[k][n]-temp)/a[k][k];}}intmain(){cout列主元消去法解线性方程组:endlendl;/*系数矩阵的行数*/intn;cout输入方程的阶数:;cinn;CGaussobj(n);obj.Input();obj.Calculate();obj.Output();return0;}(4)Doolittle分解法/*求解线性方程组--Doolittle分解法*/#includeiostreamusingnamespacestd;classCDoolittle{private:float**a;float*x;intn;public:CDoolittle(introw);~CDoolittle();voidInput();voidOutput();voidCalculate();};CDoolittle::CDoolittle(introw){n=row;a=newfloat*[n];x=newfloat[n];for(inti=0;in;i++)a[i]=newfloat[n+1];}CDoolittle::~CDoolittle(){delete[]x;for(inti=0;in;i++){deletea[i];}deletea;}/*输入过程*/voidCDoolittle::Input(){cout输入增广矩阵的各值:endl;for(inti=0;in;i++)for(intj=0;jn+1;j++)cina[i][j];}/*输出过程*/voidCDoolittle::Output(){cout解向量为:endl;for(inti=0;in;i++)coutx[i]=a[i][n]endl;}voidCDoolittle::Calculate(){/*循环变量*/inti,j,k;floattemp;/*分解过程*/for(i=0;in;i++){for(j=i;jn+1;j++){temp=0;for(k=0;ki;k++){temp=temp+a[i][k]*a[k][j];}a[i][j]=a[i][j]-temp;}for(j=i+1;jn;j++){temp=0;for(k=0;ki;k++){temp=temp+a[j][k]*a[k][i];}a[j][i]=(a[j][i]-temp)/a[i][i];}}/*回代过程*/a[n-1][n]=a[n-1][n]/a[n-1][n-1];for(k=n-2;k=0;k--){temp=0;for(j=k+1;jn;j++){temp=temp+a[k][j]*a[j][n];}a[k][n]=(a[k][n]-temp)/a[k][k];}}intmain(){coutDoolittle分解法解线性方程组:endlendl;/*系数矩阵的行数*/intn;cout输入方程的阶数:;cinn;CDoolittleobj(n);obj.Input();obj.Calculate();obj.Output();return0;}(5)追赶法/*求解线性方程组--追赶