《数值分析实验作业,gauss消去法的数值稳定性分析(9页).doc》由会员分享,可在线阅读,更多相关《数值分析实验作业,gauss消去法的数值稳定性分析(9页).doc(9页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、-实验3.1 Gauss 消去法的数值稳定性试验实验目的:观察和理解Gauss消元过程中出现小主元(即很小)时引起的方程组解的数值不稳定性。实验内容:求解方程组,其中(1),;(2),.实验要求:(1) 计算矩阵的条件数,判断系数矩阵是良态的还是病态的。(2) 用Gauss列主元消去法求得L和U及解向量.(3) 用不选主元的Gauss消去法求得和及解向量.(4) 观察小主元并分析其对计算结果的影响.程序如下:计算矩阵条件数及Gauss列主元消去法:format longeng A1=0.3e-15 59.14 3 1;5.291 -6.130 -1 2;11.2 9 5 2;1 2 1 1;b
2、1=59.17;46.78;1;2;n=4;k2=cond(A1) %k2为矩阵的条件数;for k=1:n-1 a=max(abs(A1(k:n,k); p,k=find(A1=a); B=A1(k,:);c=b1(k); A1(k,:)=A1(p,:);b1(k)=b1(p); A1(p,:)=B;b1(p)=c; if A1(k,k)=0 A1(k+1:n,k)=A1(k+1:n,k)/A1(k,k); A1(k+1:n,k+1:n)=A1(k+1:n,k+1:n)-A1(k+1:n,k)*A1(k,k+1:n); else breakendendL1=tril(A1,0);for i=
3、1:n L1(i,i)=1;endL=L1U=triu(A1,0)for j=1:n-1 b1(j)=b1(j)/L(j,j); b1(j+1:n)=b1(j+1:n)-b1(j)*L(j+1:n,j);endb1(n)=b1(n)/L(n,n);for j=n:-1:2 b1(j)=b1(j)/U(j,j); b1(1:j-1)=b1(1:j-1)-b1(j)*U(1:j-1,j);endb1(1)=b1(1)/U(1,1);x1=b1运行结果如下:K2=68.43;=18.9882;3.3378;-34.747;-33.9865不选主元的Gauss消去法程序:clearformat lon
4、gengA1=0.3e-15 59.14 3 1;5.291 -6.130 -1 2;11.2 9 5 2;1 2 1 1;b1=59.17;46.78;1;2;n=4;for k=1:n-1 A1(k+1:n,k)=A1(k+1:n,k)/A1(k,k); A1(k+1:n,k+1:n)=A1(k+1:n,k+1:n)-A1(k+1:n,k)*A1(k,k+1:n);endL1=tril(A1,0);for i=1:n L1(i,i)=1;endL=L1U=triu(A1,0)for j=1:n-1 b1(j)=b1(j)/L(j,j); b1(j+1:n)=b1(j+1:n)-b1(j)*
5、L(j+1:n,j);endb1(n)=b1(n)/L(n,n);for j=n:-1:2 b1(j)=b1(j)/U(j,j); b1(1:j-1)=b1(1:j-1)-b1(j)*U(1:j-1,j);endb1(1)=b1(1)/U(1,1);x1=b1程序运行结果如下:同理可得对应的系数矩阵条件数及Gauss列主元消去法求解结果:K2=8.994;不选主元的Gauss消去法结果:实验4.5 三次样条插值函数的收敛性问题提出:多项式插值不一定收敛的,即插值的节点多,效果不一定就好。对三次样条插值函数又如何呢?理论上证明三次样条插值函数的收敛性是比较困难的,也超过了本课程的内容。通过本实验
6、可以验证这一理论结果. 实验内容:请按一定的规则分别选择等距或者非等距的插值节点,并不断增加插值节点的个数。考虑实验4.4中的函数或者选择其他感兴趣的函数,可用Matlab的函数“spline”作此函数的三次样条插值函数。实验要求:(1) 随着节点个数的增加,比较被逼近函数和三次样条差值函数的误差变化情况。分析所得结果并与拉格朗日插值多项式比较。(2) 三次样条插值函数的思想最早产生于工业部门。作为工业迎合用的例子,考虑如下例子:某汽车制造商根据三次样条差值函数设计车门曲线,其中一段的数据如下:0123456789100.00.791.532.192.713.033.272.893.063.1
7、93.290.80.2(3)计算实验4.4的样条插值.程序如下:format shortx1=-1:0.5:1;y1=1./(1+25.*x1.*x1);x2=-1:0.25:1;y2=1./(1+25.*x2.*x2);x3=-1:0.1:1;y3=1./(1+25.*x3.*x3);x4=-1,-0.82,-0.6,-0.53,-0.34,-0.2,0,0.04,0.2,0.25,0.5,0.8,1;y4=1./(1+25.*x4.*x4);xx=-1:0.01:1;yy1=spline(x1,y1,xx);yy2=spline(x2,y2,xx);yy3=spline(x3,y3,xx)
8、;yy4=spline(x4,y4,xx);hold on fplot(1./(1+25.*x.*x),-1,1,m) plot(xx,yy1,g) plot(xx,yy2,b) plot(xx,yy3,k) plot(xx,yy4,r) hold off %比较被逼近函数与三次样条插值函数图像,直观表现不同插值节点处误差的变化 xx=-1:0.2:1; y=1./(1+25.*xx.*xx)%函数在相应节点处的真实值; yy1=spline(x1,y1,xx) y1la=lagrange(x1,y1,xx) yy2=spline(x2,y2,xx) y2la=lagrange(x2,y2,x
9、x) yy3=spline(x3,y3,xx) y3la=lagrange(x3,y3,xx) yy4=spline(x4,y4,xx) y4la=lagrange(x4,y4,xx)其中lagrange函数对应的m文件为:function y=lagrange(x0,y0,x);n=length(x0);m=length(x);for i=1:m z=x(i); s=0.0; for k=1:n p=1.0; for j=1:n if j=k p=p*(z-x0(j)/(x0(k)-x0(j); end end s=p*y0(k)+s; end y(i)=s;end程序运行结果如下:插值结果
10、比较:y0.03850.05880.10.20.510.50.20.10.05880.0385yy10.0385-0.252-0.06230.36870.802410.80240.3687-0.0623-0.2520.0385y1la0.0385-0.3793-0.11010.40050.834210.83420.4005-0.1101-0.37930.0385yy20.03850.05510.10850.17650.534210.53420.17650.10850.05510.0385y2la0.0385-0.25870.30490.07260.563610.56360.07260.3049
11、-0.25870.0385yy30.03850.05880.10.20.510.50.20.10.05880.0385y3la0.03850.05880.10.20.510.50.20.10.05880.0385yy40.03850.05870.10.20160.510.50.19890.09930.05880.0385y4la0.03850.43120.10.24610.510.50.2640.23870.05880.0385车门曲线求解程序:x=0:10;y=0,0.79,1.53,2.19,2.71,3.03,3.27,2.89,3.06,3.19,3.29;dx0=0.8;dx10=0
12、.2;S=csfit(x,y,dx0,dx10)其中csfit函数的m文件为:function S=csfit(X,Y,dx0,dxn)%Clamped Cubic Spline%Input -X is the 1xn abscissa vector% -Y is the 1xn ordinate vector% -dx0=S(x0) first derivative boundary condition% -dxn=S(xn) first derivative boundary condition%Output-S: rows of S are the coefficients, in de
13、scending order, for the% cubic interpolantsN=length(X)-1;H=diff(X);D=diff(Y)./H;A=H(2:N-1);B=2*(H(1:N-1)+H(2:N);C=H(2:N);U=6*diff(D);%Clamped spline endpoint constraintsB(1)=B(1)-H(1)/2;U(1)=U(1)-3*(D(1)-dx0);B(N-1)=B(N-1)-H(N)/2;U(N-1)=U(N-1)-3*(dxn-D(N);for k=2:N-1 temp=A(k-1)/B(k-1); B(k)=B(k)-te
14、mp*C(k-1); U(k)=U(k)-temp*U(k-1);endM(N)=U(N-1)/B(N-1);for k=N-2:-1:1 M(k+1)=(U(k)-C(k)*M(k+2)/B(k);endM(1)=3*(D(1)-dx0)/H(1)-M(2)/2;M(N+1)=3*(dxn-D(N)/H(N)-M(N)/2;for k=0:N-1 S(k+1,1)=(M(k+2)-M(k+1)/(6*H(k+1); S(k+1,2)=M(k+1)/2; S(k+1,3)=D(k+1)-H(k+1)*(2*M(k+1)+M(k+2)/6; S(k+1,4)=Y(k+1);endend程序运行结果为:S= -0.0085-0.00150.80-0.0045-0.0270.77150.79-0.0037-0.04040.70411.53-0.0409-0.05140.61232.190.1074-0.17410.38682.71-0.26850.14790.36063.030.4266-0.6575-0.14913.27-0.26790.6222-0.18442.890.0549-0.18140.25653.060.0584-0.01680.05843.19区间三次样条差值(3)实验4.4中的样条插值见上表中的y值.-第 9 页-