1.列主元高斯消去法M文件function[x]=gauss(a,b)n=length(a);x=zeros(n,1);a=[ab];fork=1:n-1max=k;fori=k+1:nifa(i,k)a(max,k)max=i;endendtemp=a(k,k:n+1);a(k,k:n+1)=a(max,k:n+1);a(max,k:n+1)=temp;fori=k+1:na(i,k)=-a(i,k)/a(k,k);a(i,k+1:n+1)=a(i,k+1:n+1)+a(i,k)*a(k,k+1:n+1);endendx(n,1)=a(n,n+1)/a(n,n);fori=n-1:-1:1sum=0;forj=i+1:nsum=sum+x(j,1)*a(i,j);endx(i,1)=(a(i,n+1)-sum)/a(i,i);endMatlab运行结果2.LU三角分解法M文件functiony=LU(A,B);n=length(A);A=[AB];fork=1:n-1;fori=k:n;if(abs(A(i,k))==max(abs(A(k:n,k))))P(k)=i;temp=A(k,:);A(k,:)=A(i,:);A(i,:)=temp;endendforj=k+1:n;A(j,k)=A(j,k)/A(k,k);A(j,k+1:n+1)=A(j,k+1:n+1)-A(j,k)*A(k,k+1:n+1);endendP(n)=n;L(1,1)=1;L(2:n,1)=A(2:n,1);L(1,2:n)=0;U(1,1)=A(1,1);U(2:n,1)=0;U(1,2:n)=A(1,2:n);fori=2:n;L(i,1:i-1)=A(i,1:i-1);L(i,i)=1;L(i,i+1:n)=0;U(i,1:i-1)=0;U(i,i:n)=A(i,i:n);endx(n)=A(n,n+1)/U(n,n);fork=n-1:-1:1x(k)=A(k,n+1);forp=n:-1:k+1;x(k)=x(k)-U(k,p)*x(p);endx(k)=x(k)/U(k,k);endxLUPEndMatlab运行结果3.龙贝格(Romberg)算法M文件function[t]=romberg(f,a,b,e)t=zeros(15,4);t(1,1)=(b-a)/2*(f(a)+f(b));fork=2:4sum=0;fori=1:2^(k-2)sum=sum+f(a+(2*i-1)*(b-a)/2^(k-1));endt(k,1)=0.5*t(k-1,1)+(b-a)/2^(k-1)*sum;fori=2:kt(k,i)=(4^(i-1)*t(k,i-1)-t(k-1,i-1))/(4^(i-1)-1);endendfork=5:15sum=0;fori=1:2^(k-2)sum=sum+f(a+(2*i-1)*(b-a)/2^(k-1));endt(k,1)=0.5*t(k-1,1)+(b-a)/2^(k-1)*sum;fori=2:4t(k,i)=(4^(i-1)*t(k,i-1)-t(k-1,i-1))/(4^(i-1)-1);endifk6ifabs(t(k,4)-t(k-1,4))edisp(['答案',num2str(t(k,4))]);break;endendendifk=15disp(['溢出']);endMatlab运行结果4.最小二乘法M文件function[a,max,det]=zuixiaoerchengfa(x,y,r)n=length(x);c=ones(n,r+1);fori=2:r+1forj=1:nc(j,i)=x(j)^(i-1);endendA=c'*c;b=c'*y';a=inv(A)*b;det=0;max=0;fori=1:nsum=a(1);forj=2:r+1sum=sum+a(j)*x(i)^(j-1);endcc=abs(y(i)-sum);ifccmaxmax=cc;enddet=det+cc^2;enddet=sqrt(det);Matlab运行结果