矩阵与数值分析实验报.pdf
一、设NjjNS2112,分别编制从小到大喝从大到小的顺序程序计算100000010000100,SSS,并指出有效位数代码:/szfx.cpp:定义控制台应用程序的入口点。#include stdafx.h#include using namespace std;int _tmain(int argc,_TCHAR*argv)int n1=100;int n2=10000;int n3=1000000;double s1=0;double s2=0;double s3=0;char a;coutB:endl;for(double j=2;j=n1;j+)s1+=(double)1/(j*j-1);cout结果为:s1endl;coutS:=2;j-)s1+=(double)1/(j*j-1);cout结果为:s1endl;coutB:endl;for(double j=2;j=n2;j+)s2+=(double)1/(j*j-1);cout结果为:s2endl;s2=0;coutS:=2;j-)s2+=(double)1/(j*j-1);cout结果为:s2endl;coutB:endl;for(double j=2;j=n3;j+)s3+=(double)1/(j*j-1);cout结果为:s3endl;s3=0;coutS:=2;j-)s3+=(double)1/(j*j-1);cout结果为:s3a;return 0;计算结果:结果分析:二、解线性方程组1.分别用 Jacobi 迭代法和 Gauss-Seidel 迭代法求解线性方程组21001210012100124321xxxx=0001迭代法计算的停止条件为:6)()1(3110maxkjkjjxx。Jacobi方法代码:Jacobi方法计算结果:Jacobi方法结果分析:Gauss-Seidel 方法代码:Gauss-Seidel 方法结果:Gauss-seidel 结果分析:2.用Gauss 列主元消去法、QR方法求解如下方程组。017433315222235221214321xxxxGuass 列主元消去法代码:#include#include#include#include#include using namespace std;#define NUMBER 20#define Esc 0 x1b#define Enter 0 x0d float ANUMBERNUMBER+1,ark;int flag,n;void exchange(int r,int k);float max(int k);void message();int main()float xNUMBER;int r,k,i,j;char celect;system(cls);printf(nnUse Gauss.);printf(nn1.Jie please press Enter.);printf(nn2.Exit press Esc.);celect=getch();if(celect=Esc)exit(0);printf(nn input n=);scanf(%d,&n);printf(nnInput matrix A and B:);for(i=1;i=n;i+)printf(nnInput a%d1-a%d%d and b%d:,i,i,n,i);for(j=1;j=n+1;j+)scanf(%f,&Aij);for(k=1;k=n-1;k+)ark=max(k);if(ark=0)printf(nIt s wrong!);message();elseif(flag!=k)exchange(flag,k);for(i=k+1;i=n;i+)for(j=k+1;j=1;k-)float me=0;for(j=k+1;j=n;j+)me=me+Akj*xj;xk=(Akn+1-me)/Akk;for(i=1;i=n;i+)printf(nnx%d=%f,i,xi);message();return 0;void exchange(int r,int k)int i;for(i=1;i=n+1;i+)A0i=Ari;for(i=1;i=n+1;i+)Ari=Aki;for(i=1;i=n+1;i+)Aki=A0i;float max(int k)int i;float temp=0;for(i=k;itemp)temp=fabs(Aik);flag=i;return temp;void message()printf(nn Go on Enter,Exit press Esc!);switch(getch()case Enter:main();case Esc:exit(0);default:printf(nnInput error!);message();Gauss 列主元消去法计算结果:Guass 列主元消去法结果分析:QR 方法求解代码:QR 方法求解结果:QR 方法结果分析: