《数值分析期末试题(B).doc》由会员分享,可在线阅读,更多相关《数值分析期末试题(B).doc(8页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、 数值分析试卷() 2008.12.16班级:_ _学号: 姓名: _分数:_ 一、(10分)设,计算的行列式,逆矩阵,普半径与条件数解:先编写M函数:a=sparse(1:6,1:6,2*ones,6,6);b=sparse(1:5,2:6,(-1/3)*ones,6,6);c=sparse(1:4,3:6,(-1/4)*ones,6,6);d=sparse(2:6,1:5,(-1/2)*ones,6,6);m=a+b+c+dend调用函数:在窗口命令中输入A=full(m)得:A = 2.0000 -0.3333 -0.2500 0 0 0 -0.5000 2.0000 -0.3333 -
2、0.2500 0 0 0 -0.5000 2.0000 -0.3333 -0.2500 0 0 0 -0.5000 2.0000 -0.3333 -0.2500 0 0 0 -0.5000 2.0000 -0.3333 0 0 0 0 -0.5000 2.0000 det(A)ans = 49.4576 inv(A)ans = 0.5278 0.1112 0.0931 0.0342 0.0192 0.0075 0.1393 0.5572 0.1358 0.1020 0.0388 0.0192 0.0368 0.1470 0.5635 0.1378 0.1020 0.0342 0.0097 0.
3、0388 0.1486 0.5635 0.1358 0.0931 0.0025 0.0101 0.0388 0.1470 0.5572 0.1112 0.0006 0.0025 0.0097 0.0368 0.1393 0.5278 cond(A),cond(A,1),cond(A,inf)ans = 2.4014 3.1489 3.1489二、(10分) 证明方程在上有根,写出牛顿迭代程序,并取初始值为求近似根(保留六位小数)解: 牛顿迭代程序为:function x=nanewton(fname,dfname,x0,e,N)if nargin5,N=500;endif nargine&k
4、fun=inline(1-x-sin(x)fun = Inline function: fun(x) = 1-x-sin(x) dfun=inline(diff(1-x-sin(x)dfun = Inline function: dfun(x) = -1-cos(x) nanewton(fun,dfun,1,1e-6) 0.4537 0.5106 0.5110 0.5110ans = 0.5110三、(20分) 给定方程组()().要求:(1)用LU分解和列主元高斯消去求解上述两个方程组输出中矩阵及向量,分解的与,计算与解向量x(2)将方程组()中系数3.01改为3.00,0.987改为0.9
5、90.用列主元高斯消去法求解,输出向量x及,并(1)中结果比较(3)将方程组()中的2.改为2.1,5.改为5.9.用列主元高斯消去法求解,输出向量x及,并与(1)中结果比较解:(1)先编系数矩阵程序:A1=3.01 6.03 1.99;1.27 4.16 -1.23;0.987 -4.81 9.34b1=1 1 1A2=10 -7 0 1;-3 2. 6 2;5 -1 5 -1;2 1 0 2b2=8 5. 5 1在命令窗口中输入: L,U,P=lu(A1)L = 1.0000 0 0 0.3279 1.0000 0 0.4219 -0.2381 1.0000U = 3.0100 6.030
6、0 1.9900 0 -6.7873 8.6875 0 0 -0.0015P = 1 0 0 0 0 1 0 1 0 L,U,P=lu(A2)L = 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.0800P = 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 det(A1)ans = -0.0305 d
7、et(A2)ans =-762.0000选列主元高斯消去法程序(nagauss2.m):function x=nagauss2(a,b,flag)if nargink, t=a(k,:);a(k,:)=a(p,:);a(p,:)=t; end a(k+1):n,(k+1):(n+1)=a(k+1):n,(k+1):(n+1)-a(k+1):n,k)/a(k,k)*a(k,(k+1):(n+1); a(k+1):n,k)=zeros(n-k,1); if flag=0,a,endendx=zeros(n,1);x(n)=a(n,n+1)/a(n,n);for k=n-1:-1:1 x(k,:)=
8、(a(k,n+1)-a(k,(k+1):n)*x(k+1):n)/a(k,k);end对(i)应用选列主元高斯消去法求的x: 在窗口命令中输入: x=nagauss2(A1,b1)a = 3.0100 6.0300 1.9900 1.0000 0 1.6158 -2.0696 0.5781 0 -6.7873 8.6875 0.6721a = 3.0100 6.0300 1.9900 1.0000 0 -6.7873 8.6875 0.6721 0 0 -0.0015 0.7381x = 1.0e+003 * 1.5926 -0.6319 -0.4936对(ii)应用选列主元高斯消去法求的x:
9、 在窗口命令中输入: x=nagauss2(A2,b2)a = 10.0000 -7.0000 0 1.0000 8.0000 0 -0.0000 6.0000 2.3000 8.3000 0 2.5000 5.0000 -1.5000 1.0000 0 2.4000 0 1.8000 -0.6000a = 10.0000 -7.0000 0 1.0000 8.0000 0 2.5000 5.0000 -1.5000 1.0000 0 0 6.0000 2.3000 8.3000 0 0 -4.8000 3.2400 -1.5600a = 10.0000 -7.0000 0 1.0000 8.
10、0000 0 2.5000 5.0000 -1.5000 1.0000 0 0 6.0000 2.3000 8.3000 0 0 0 5.0800 5.0800x = -0.0000 -1.0000 1.0000 1.0000(2) 在窗口命令中输入: A3=3.00 6.03 1.99;1.27 4.16 -1.23;0.990 -4.81 9.34 b3=1 1 1A3 = 3.0000 6.0300 1.9900 1.2700 4.1600 -1.2300 0.9900 -4.8100 9.3400b3 = 1 1 1 det(A3)ans = -0.4070 x=nagauss2(A3
11、,b3)a = 3.0000 6.0300 1.9900 1.0000 0 1.6073 -2.0724 0.5767 0 -6.7999 8.6833 0.6700a = 3.0000 6.0300 1.9900 1.0000 0 -6.7999 8.6833 0.6700 0 0 -0.0200 0.7350x = 119.5273 -47.1426 -36.8403(3)在窗口命令中输入: A4=10 -7 0 1;-3 2.1 6 2;5 -1 5 -1;2 1 0 2b4=8 5.9 5 1A4 = 10.0000 -7.0000 0 1.0000 -3.0000 2.1000 6.
12、0000 2.0000 5.0000 -1.0000 5.0000 -1.0000 2.0000 1.0000 0 2.0000b4 = 8.0000 5.9000 5.0000 1.0000 det(A4)ans = -762.0000 x=nagauss2(A4,b4)a = 10.0000 -7.0000 0 1.0000 8.0000 0 0 6.0000 2.3000 8.3000 0 2.5000 5.0000 -1.5000 1.0000 0 2.4000 0 1.8000 -0.6000a = 10.0000 -7.0000 0 1.0000 8.0000 0 2.5000 5
13、.0000 -1.5000 1.0000 0 0 6.0000 2.3000 8.3000 0 0 -4.8000 3.2400 -1.5600a = 10.0000 -7.0000 0 1.0000 8.0000 0 2.5000 5.0000 -1.5000 1.0000 0 0 6.0000 2.3000 8.3000 0 0 0 5.0800 5.0800x = 0.0000 -1.0000 1.0000 1.0000四、(15分) 考虑利用Gauss-Seidle迭代法分别求解线性方程组和, (1)说明两者的收敛性;(2)并对收敛的迭代法写出计算程序,再由初始向量,计算结果(保留六位
14、小数)解: 先编写系数矩阵程序得M函数:a1=9 2 1;1 -8 1;2 -1 -8b1=6;-8;9a2=9 2 1;2 -1 -8;1 -8 1b2=6;9;-8运行后得:a1 = 9 2 1 1 -8 1 2 -1 -8b1 = 6 -8 9a2 = 9 2 1 2 -1 -8 1 -8 1b2 = 6 9 -8 x=nags(a1,b1,0,0,0,1e-6) 0.6667 1.0833 -1.0938 0.5475 0.9317 -1.1046 0.5824 0.9347 -1.0963 0.5808 0.9356 -1.0968 0.5806 0.9355 -1.0968 0.5
15、806 0.9355 -1.0968 0.5806 0.9355 -1.0968x = 0.5806 0.9355 -1.0968 x=nags(a1,b1,0,0,0,1e-6)五、(10分) 已知的观察数据表如下:xi-1.0-0.50.00.51.01.52.0yi-4.672-0.4250.5150.084-0.4740.5944.582(1)试构造六次插值多项式,并求(2)用最小二乘法求三次插值多项式,并求解:(1) 在窗口命令中输入: xi=-1.0:0.5:2.0y=-4.672 -0.425 0.515 0.084 -0.474 0.594 4.582xi = -1.0000
16、-0.5000 0 0.5000 1.0000 1.5000 2.0000y = -4.6720 -0.4250 0.5150 0.0840 -0.4740 0.5940 4.5820 p=polyfit(x,y,6)p = -0.1928 0.6095 -0.2203 1.3582 -2.6749 0.1314 0.5150 poly2sym(-0.1928 0.6095 -0.2203 1.3582 -2.6749 0.1314 0.5150)ans =-241/1250*x6+1219/2000*x5-2203/10000*x4+6791/5000*x3-26749/10000*x2+6
17、57/5000*x+103/200 fun=inline(-241/1250*x6+1219/2000*x5-2203/10000*x4+6791/5000*x3-26749/10000*x2+657/5000*x+103/200)fun = Inline function: fun(x) = -241/1250*x6+1219/2000*x5-2203/10000*x4+6791/5000*x3-26749/10000*x2+657/5000*x+103/200 fun(1.2)ans = -0.3481(2)程序为(nafit.m):function p=nafit(x,y,m)A=zer
18、os(m+1,m+1);for i=0:m for j=0:m A(i+1,j+1)=sum(x.(i+j); end b(i+1)=sum(x.1.*y); end a=Ab; p=fliplr(a);在窗口命令中: x=-1.0:0.5:2.0y=-4.672 -0.425 0.515 0.084 -0.474 0.594 4.582x = -1.0000 -0.5000 0 0.5000 1.0000 1.5000 2.0000y = -4.6720 -0.4250 0.5150 0.0840 -0.4740 0.5940 4.5820调用nafit函数程序:在窗口命令中输入: nafi
19、t(x,y,3)ans = -3.2239 2.7633 6.3326 0.6908 poly2sym( -3.2239 2.7633 6.3326 0.6908)ans =-32239/10000*x3+27633/10000*x2+31663/5000*x+1727/2500 fun=inline(-32239/10000*x3+27633/10000*x2+31663/5000*x+1727/2500)fun = Inline function: fun(x) = -32239/10000*x3+27633/10000*x2+31663/5000*x+1727/2500 fun(1.2)
20、ans = 6.6982六、(15分) 设矩阵(1)利用乘幂法求其最大特征值和相应的特征向量(初值);(2)求出所有的特征值与特征向量解: 在窗口命令中输入: A=7 3 -2;3 4 -1;-2 -1 3A = 7 3 -2 3 4 -1 -2 -1 3(1)(2) V,D=eig(A)V = 0.5774 -0.0988 -0.8105 -0.5774 0.6525 -0.4908 0.5774 0.7513 0.3197D = 2.0000 0 0 0 2.3944 0 0 0 9.6056七、(10分) 设计一个数值积分方法计算:解:程序:clear;n=100;r=2;e=1e-4;
21、fun=inline(x.(-2);t0=inf;t1=quadl(fun,1,n);while abs(t0-t1)e t0=t1;n=n*r;t1=quadl(fun,1,n);endt1t1 = 0.9999八、 (10分) 对于常微分方程初值问题(1) 用改进欧拉法(取h = 0.05)及四阶R-K.方法(取h = 0.1)求它的数值解,并输出xi=1+0.1 i(i = 1,2,10)的数值解yi(2) 与精确解:进行比较解:(1)欧拉法程序(naeuler2.m):functionx,y=naeuler2(dyfun,xspan,y0,h)x=xspan(1):h:xspan(2)
22、;y(1)=y0;for n=1:length(x)-1 k1=feval(dyfun,x(n),y(n); y(n+1)=y(n)+h*k1; k2=feval(dyfun,x(n+1),y(n+1); y(n+1)=y(n)+h*(k1+k2)/2;endx=x;y=y;在窗口命令中输入: dyfun=inline(-50*y+50*x2+2*x)dyfun = Inline function: dyfun(x,y) = -50*y+50*x2+2*x x,y=naeuler2(dyfun,0,1,1/3,0.05);x,yans = 1.0e+003 * 0 0.0003 0.0001 0.0005 0.0001 0.0009 0.0002 0.0015 0.0002 0.0024 0.0003 0.0039 0.0003 0.0063 0.0004 0.0102 0.0004 0.0166 0.0004 0.0269 0.0005 0.0437 0.0006 0.0709 0.0006 0.1151 0.0006 0.1868 0.0007 0.3034 0.0008 0.4928 0.0008 0.8005 0.0008 1.3004 0.0009 2.1129 0.0010 3.4330 0.0010 5.5781