1、m = length(y);%输入的插值点与它的函数值应有相同的个数if n =m error(The lengths of X and Y must be equal!); return; endp = zeros(1, n);for k =1 : nt = ones(1, n);for j = 1 :if j =k%输入的插值点必须互异if abs(x(k)-x(j) x = 0 1 4 9 16 25 36 49 64 y = 1 1 2 3 4 5 6 7 8 xx = 8 yi = Lagrange(x ,y, xx)实验结果:x = 0 1 4 9 16 25 36 49 64y
2、=1 1 2 3 4 5 6 7 8 xx = 8xx = 8yi =2.9036截图如下:实验二:用不同的方法计算积分取不同的步长h,分别用复合梯形公式及复合辛普森公式计算积分,给出误差中关于h的函数,并与积分精确值比较两个公式的精度,是否存在一个最小的h,使得精度不能再被改善。复合梯形公式:function I = T_quad(x,y)n=length(x); m=length(y);if n=mThe lengths of X and Y must be equalh=(x(n)-x(1)/(n-1);a=1 2*ones(1,n-2) 1;I=h/2 * sum(a.*y);复合辛普
3、森公式:function I = S_quad(x,y)if rem(n-1,2)=0I = T_quad(x,y);N = (n-1)/2;h=(x(n)-x(1)/N;a = zeros(1,n);for k =1:Na(2*k - 1) = a(2*k-1)+1;a(2*k) = a(2*k)+4;a(2*k + 1) = a(2*k+1)+1;I = h/6 * sum(a.*y);复合梯形测试数据: x=0.00001:0.0001:0.99999 y=sqrt(x).*log (x)I=T_quad(x,y)复合辛普森测试数据:I=S_quad(x,y)复合梯形实验结果:x = C
4、olumns 1 through 8 0.0000 0.0001 0.0002 0.0003 0.0004 0.0005 0.0006 0.0007 Columns 9 through 16 Columns 9993 through 10000 0.9992 0.9993 0.9994 0.9995 0.9996 0.9997 0.9998 0.9999y = Columns 1 through 8 -0.0364 -0.0956 -0.1227 -0.1422 -0.1579 -0.1712 -0.1828 -0.1932 -0.0008 -0.0007 -0.0006 -0.0005 -0
5、.0004 -0.0003 -0.0002 -0.0001 I=T_quad(x,y)I = -0.4444复合辛普森实验结果:0.0000 0.0001 0.0002 0.0003 0.0004 0.0005 0.0006 0.0007Columns 9993 through 10000 0.9992 0.9993 0.9994 0.9995 0.9996 0.9997 0.9998 0.9999 I=S_quad(x,y)实验三:用LU分解和列主元消去法解线性方程组输出Ax=b中系数A=LU分解的矩阵L和U,解向量x及detA;列主元法的行交换次序,解向量x及detA;比较两种方法所得的结
6、果。LU分解:function L,U,x=lux(A,b) n,n=size(A);p=eye(n);for k=1:n-1 r,m=max(abs(A(k:n,k); m=m+k-1; if(A(m,k)=0) if(m=k) A(k m,:)=A(m k,: p(k m)=p(m k); for i=k+1:n A(i,k)=A(i,k)/A(k,k); j=k+1:n; A(i,j)=A(i,j)-A(i,k)*A(k,j);L=tril(A,-1)+eye(n,n);U=triu(A);newb=p*b; y=zeros(n,1);n j=1:k-1; y(k)=(newb(k)-L
7、(k,j)*y(j)/L(k,k);end x=zeros(n,1);for k=n:-1:1 x(k)=(y(k)-U(k,j)*x(j)/U(k,k);高斯列主元消去法:function x=gauss_lie(A,b)%UNTITLED5 Summary of this function goes here% Detailed explanation goes here%采用高斯列主元法求解方程组Ax=bn=length(b);p=1:lu=A;y=;c,i=max(abs(lu(k:ik=i+k-1;if ik=km=p(k);p(k)=p(ik);p(ik)=m;ck=lu(k,:l
8、u(k,:)=lu(ik,:lu(ik,:)=ck;if k=nbreak;lu(k+1:n,k)=lu(k+1:n,k)/lu(k,k);n,k+1:n)=lu(k+1:n)-lu(k+1:n,k)*lu(k,k+1:n);l=diag(ones(n,1)+tril(lu,-1);u=triu(lu);y(1)=b(p(1);for i=2:y(i)=b(p(i)-l(i,1:i-1)*y(1:i-1);x(n)=y(n)/u(n,n);for i=n-1:x(i)=(y(i)-u(i,i+1:n)*x(i+1:n)/u(i,i);x=xLU分解测试数据: A=10,-7,0,1;-3,2.
9、099999,6,2;5,-1,5,-1;2,1,0,2 b=8;5.900001;5;1 l,u,x=lux(A,b) det(A)高斯列主元消去法测试数据:A=10,-7,0,1;b=8;x=gauss_lie(A,b)det(A)LU分解实验结果:A = 10.0000 -7.0000 0 1.0000 -3.0000 2.1000 6.0000 2.0000 5.0000 -1.0000 5.0000 -1.0000 2.0000 1.0000 0 2.0000b =8.0000 5.9000 5.0000 1.0000l =1.0000 0 0 0 0.5000 1.0000 0 0 -0.3000 -0.0000 1.0000 0 0.2000 0.9600 -0.8000 1.0000u = 10.0000 -7.0000 0 1.0000 0 2.5000 5.0000 -1.5000 0 0 6.0000 2.3000 0 0 0 5.0800x = 0.1949 -0.7661 0.9695 0.6882ans = -762.0001高斯列主元分解实验结果:A = 8.0000 x=gauss_lie(A,b) 0.0000 -1.0000
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1