1.列主元法(178页实习1题)函数liezhu(A,b).m的头文件的程序如下:n=length(b);detA=det(A);d=0;x=zeros(n,1);c=zeros(1,n);ifabs(det(A))=epserror('系数矩阵是奇异的');%所求方程组系数矩阵必须是是非奇异的!return;end%按列选主元fori=1:n-1max=abs(A(i,i));m=i;forj=i+1:nifmaxabs(A(j,i))max=abs(A(j,i));m=j;endend%换行ifm~=ifork=i:nc(k)=A(i,k);A(i,k)=A(m,k);A(m,k)=c(k);endd=b(i);b(i)=b(m);b(m)=d;end%消元fork=i+1:nforj=i+1:nA(k,j)=A(k,j)-A(i,j)*A(k,i)/A(i,i);endb(k)=b(k)-b(i)*A(k,i)/A(i,i);A(k,i)=0;endEnd%回代求解x(n)=b(n)/A(n,n);fori=n-1:(-1):1sum=0;forj=i+1:nsum=sum+A(i,j)*x(j);endx(i)=(b(i)-sum)/A(i,i);endend其结果如下:A=[10,-7,0,1;-3,2.099999,6,2;5,-1,5,-1;2,1,0,2];b=[8;5.900001;5;1];[x,detA]=liezhu(A,b)x=0.0000-1.00001.00001.0000detA=-762.00012.LU分解法:(178页实习1题)LUFJ.m文件中函数LUFJ(A,b)的程序如下:functionLUFJ(A,b)%A为系数矩阵,b为右端项矩阵%UNTITLED2Summaryofthisfunctiongoeshere%Detailedexplanationgoeshere[m,n]=size(A);%初始化矩阵A,b,L和Un=length(b);L=eye(n,n);U=zeros(n,n);U(1,1:n)=A(1,1:n);%开始进行LU分解L(2:n,1)=A(2:n,1)/U(1,1);fork=2:nU(k,k:n)=A(k,k:n)-L(k,1:k-1)*U(1:k-1,k:n);L(k+1:n,k)=(A(k+1:n,k)-L(k+1:n,1:k-1)*U(1:k-1,k))/U(k,k);endL%输出L矩阵U%输出U矩阵y=zeros(n,1);%开始解方程组Ux=yy(1)=b(1);fork=2:ny(k)=b(k)-L(k,1:k-1)*y(1:k-1);endx=zeros(n,1);x(n)=y(n)/U(n,n);fork=n-1:-1:1x(k)=(y(k)-U(k,k+1:n)*x(k+1:n))/U(k,k);endfork=1:nfprintf('x[%d]=%f\n',k,x(k));endend运行结果如下:A=[10,-7,0,1;-3,2.099999,6,2;5,-1,5,-1;2,1,0,2];b=[8;5.900001;5;1];LUFJ(A,b)L=1.0e+006*0.0000000-0.00000.0000000.0000-2.50000.000000.0000-2.40000.00000.0000U=1.0e+007*0.0000-0.000000.00000-0.00000.00000.0000001.50000.57500000.0000x[1]=-0.000000x[2]=-1.000000x[3]=1.000000x[4]=1.0000003.雅可比迭代法:(179页实习3题)Jacobi.m文件中函数Jacobi(A,b,eps)的程序如下:functionJacobi(A,b,eps)%A为系数矩阵,b为后端项矩阵,epe为精度[m,n]=size(A);D=diag(diag(A));%求矩阵DL=D-tril(A);%求矩阵LU=D-triu(A);%求矩阵Utemp=1;x=zeros(m,1);k=0;whileabs(max(x)-temp)epstemp=max(abs(x));k=k+1;%记录循环次数x=-inv(D)*(L+U)*x+inv(D)*b;%雅克比迭代公式endfork=1:nfprintf('x[%d]=%f\n',k,x(k));end运行结果如下:A=[10-120;08-13;2-1100;-13-111];b=[-11;-11;6;25];Jacobi(A,b,0.00005)x[1]=-1.466538x[2]=-2.358692x[3]=0.657438x[4]=2.8424524.Gauss-Seidel迭代程序(179页实习3题)Gauss-Seidel.m文件中函数Gauss-Seidel(A,b,eps)的程序如下:functionGauss_Seidel(A,b,eps)%A为系数矩阵,b为后端项矩阵,epe为精度[m,n]=size(A);D=diag(diag(A));%求矩阵DL=D-tril(A);%求矩阵LU=D-triu(A);%求矩阵Utemp=1;x=zeros(m,1);k=0;whileabs(max(x)-temp)epstemp=max(abs(x));k=k+1;%记录循环次数x=inv(D-L)*U*x+inv(D-L)*b;%Gauss_Seidel的迭代公式endfork=1:nfprintf('x[%d]=%f\n',k,x(k));end其运行结果如下:A=[10-120;08-13;2-1100;-13-111];b=[-11;-11;6;25];Gauss_Seidel(A,b,0.005)x[1]=-1.466538x[2]=-2.358692x[3]=0.657438x[4]=2.842452