《数值计算方法上机实习题要点.doc》由会员分享,可在线阅读,更多相关《数值计算方法上机实习题要点.doc(25页珍藏版)》请在三一办公上搜索。
1、上海电力学院数值计算上机报告 课 程: 现代数值计算 题 目: 数值计算方法上机实习题报告 院 系: 自动化工程学院 专业年级: 电机与电器 学生姓名: 黄 丽 学号:ys1310401009 指导教师: 黄建雄 2013年12月20日数值计算方法上机实习题1 设,(1) 由递推公式,从的几个近似值出发,计算;(2) 粗糙估计,用,计算;(3) 分析结果的可靠性及产生此现象的原因(重点分析原因)。程序:(1)由递推公式,从的几个近似值出发,计算;I(0) =0.1820;I(1)= 0.0900;I(2)= 0.0500;I(3)= 0.0833;I(4)= -0.1667; I=0.182;
2、 for n=1:20 I=(-5)*I+1/n; end故计算结果= -3.0666e+10(2)粗糙估计,用,计算; I=0.008; for n=20:-1:1 I=(-1/5)*I+1/(5*n); end I = 0.1823(3)分析结果的可靠性及产生此现象的原因(重点分析原因)。 假设的真值为,误差为,即。对于真值也有。综合2个递推等式,有,即意味着只要n足够大,按照这种每计算一步误差增长5倍的方式,所得的结果总是不可信的,因此整个算法是数值不稳定的。而第二种方式的误差会以每计算一步缩小到1/5的方式进行,这样的计算结果和实际是很相近的。2 求方程的近似根,要求,并比较计算量。(
3、1) 在0,1上用二分法;(2) 取初值,并用迭代;(3) 加速迭代的结果;(4) 取初值,并用牛顿迭代法;(5) 分析绝对误差。(1)在0,1上用二分法; 程序: a=0;b=1.0;i=0;while abs(b-a)5*1e-4 c=(b+a)/2; if exp(c)+10*c-20 b=c; else a=c; end i=i+1;endc方程的近似根为:x*=(a+b)/2 = 0.0906 。步长为i=11 。(2)取初值,并用迭代;程序: x=0;y=0.1;i=0;while abs(y-x)5*1e-4 y=x; x=(2-exp(x)/10; i=i+1;end方程的近似
4、根为:x=0.0905 。步长为i=4 。(3)加速迭代的结果;程序:x=0;xx=1;i=0;while abs(xx-x)5*1e-4 y=exp(x)+10*x-2; z=exp(y)+10*y-2; xx=x; x=x-(y-x)2/(z-2*y+x); i=i+1;endx方程的近似根为:x= 0.0995。步长为i=3 。(4) 取初值,并用牛顿迭代法;程序:x=0;y=0.1;i=0;while abs(y-x)5*1e-4 y=x; x=x-(exp(x)+10*x-2)/(exp(x) + 10);%diff(exp(x)+10*x-2)=exp(x) + 10 i=i+1;
5、end xi方程的近似根为:x= 0.0905。步长为i=2 。 (6) 分析绝对误差。solve(exp(x)+10*x-2=0)方程的精确解为x= 0.0905。3钢水包使用次数多以后,钢包的容积增大,数据如下:x23456789y6.428.29.589.59.7109.939.991011121314151610.4910.5910.6010.810.610.910.76试从中找出使用次数和容积之间的关系,计算均方差。(注:增速减少,用何种模型)解:(1)设y=f(x)具有指数形式(a0,b0)。对此式两边取对数,得。记A=lna,B=bx=2 3 4 5 6 7 8 9 10 11
6、12 13 14 15 16;for i=1:15 X(i)=1/x(i);endy=6.42 8.2 9.58 9.5 9.7 10 9.93 9.99 10.49 10.59 10.60 10.8 10.6 10.9 10.76;for i=1:15 Y(i)=log(y(i);endpolyfit(X,Y,1)经计算ans = -1.1107 2.4578。故方程为故原方程的系数为 故原方程为:(2) 计算均方差:x=2:16; y=6.42 8.2 9.58 9.5 9.7 10 9.93 9.99 10.49 10.59 10.60 10.8 10.6 10.9 10.76;f(x)
7、=11.6791*exp( -1.1107./x);c=0;for i=1:15 a=y(i); b=x(i); c=c+(a-f(b)2; endaverge=c/15结果:averge =0.05944设,分析下列迭代法的收敛性,并求的近似解及相应的迭代次数。(1) JACOBI迭代;以文件名math4a.m保存。function math4aA=4 -1 0 -1 0 0;-1 4 -1 0 -1 0;0 -1 4 -1 0 -1;-1 0 -1 4 -1 0;0 -1 0 -1 4 -1;0 0 -1 0 -1 4;b=0 5 -2 5 -2 6;x0=0 0 0 0 0 0;imax
8、=100;tol=10-4;tx=jacobi(A,b,imax,x0,tol);for j=1:size(tx,1) fprintf(%d %f %f %f %f %f %fn,j,tx(j,1),tx(j,2),tx(j,3),tx(j,4),tx(j,5),tx(j,6)end function tx=jacobi(A,b,imax,x0,tol) %初始值x0,次数imax,精度tol del=10-10; tx=x0;n=length(x0);for i=1:n dg=A(i,i); if abs(dg)del disp(对角元太小); return endendfor k=1:im
9、ax %jacobi循环 for i=1:n sm=b(i); for j=1:n if j=i sm=sm-A(i,j)*x0(j); end end x(i)=sm/A(i,i); end tx=tx;x; if norm(x-x0)tol return else x0=x; end end近似解y为:1 0.000000 0.000000 0.000000 0.000000 0.000000 0.0000002 0.000000 1.250000 -0.500000 1.250000 -0.500000 1.5000003 0.625000 1.000000 0.500000 1.000
10、000 0.500000 1.2500004 0.500000 1.656250 0.312500 1.656250 0.312500 1.7500005 0.828125 1.531250 0.765625 1.531250 0.765625 1.6562506 0.765625 1.839844 0.679688 1.839844 0.679688 1.8828137 0.919922 1.781250 0.890625 1.781250 0.890625 1.8398448 0.890625 1.925293 0.850586 1.925293 0.850586 1.9453139 0.
11、962646 1.897949 0.948975 1.897949 0.948975 1.92529310 0.948975 1.965149 0.930298 1.965149 0.930298 1.97448711 0.982574 1.952393 0.976196 1.952393 0.976196 1.96514912 0.976196 1.983742 0.967484 1.983742 0.967484 1.98809813 0.991871 1.977791 0.988895 1.977791 0.988895 1.98374214 0.988895 1.992415 0.98
12、4831 1.992415 0.984831 1.99444815 0.996208 1.989639 0.994820 1.989639 0.994820 1.99241516 0.994820 1.996462 0.992923 1.996462 0.992923 1.99741017 0.998231 1.995167 0.997583 1.995167 0.997583 1.99646218 0.997583 1.998349 0.996699 1.998349 0.996699 1.99879219 0.999175 1.997745 0.998873 1.997745 0.9988
13、73 1.99834920 0.998873 1.999230 0.998460 1.999230 0.998460 1.99943621 0.999615 1.998948 0.999474 1.998948 0.999474 1.99923022 0.999474 1.999641 0.999282 1.999641 0.999282 1.99973723 0.999820 1.999509 0.999755 1.999509 0.999755 1.99964124 0.999755 1.999832 0.999665 1.999832 0.999665 1.99987725 0.9999
14、16 1.999771 0.999886 1.999771 0.999886 1.99983226 0.999886 1.999922 0.999844 1.999922 0.999844 1.99994327 0.999961 1.999893 0.999947 1.999893 0.999947 1.99992228 0.999947 1.999964 0.999927 1.999964 0.999927 1.99997329 0.999982 1.999950 0.999975 1.999950 0.999975 1.999964(2) GAUSS-SEIDEL迭代;以文件名math4b
15、.m保存。function math4bA=4 -1 0 -1 0 0;-1 4 -1 0 -1 0;0 -1 4 -1 0 -1;-1 0 -1 4 -1 0;0 -1 0 -1 4 -1;0 0 -1 0 -1 4;b=0 5 -2 5 -2 6;x0=0 0 0 0 0 0;imax=100;tol=10-4;tx=gs (A,b,imax,x0,tol);for j=1:size(tx,1)fprintf(%d %f %f %f %f %f %fn,j,tx(j,1),tx(j,2),tx(j,3),tx(j,4),tx(j,5),tx(j,6)endfunction tx=gs (A
16、,b,imax,x0,tol) %初始值x0,次数imax,精度toldel=10-10;tx=x0;n=length(x0);for i=1:n dg=A(i,i); if abs(dg)del disp(对角元太小); return endend for k=1:imax %gs循环 x=x0; for i=1:n sm=b(i); for j=1:n if j=i sm=sm-A(i,j)*x(j); end end x(i)=sm/A(i,i); end tx=tx;x; if norm(x-x0)tol return else x0=x; endend近似解y为:1 0.000000
17、 0.000000 0.000000 0.000000 0.000000 0.0000002 0.000000 1.250000 -0.187500 1.203125 0.113281 1.4814453 0.613281 1.384766 0.517334 1.560974 0.606796 1.7810334 0.736435 1.715141 0.764287 1.776880 0.818263 1.8956385 0.873005 1.863889 0.884102 1.893843 0.913342 1.9493616 0.939433 1.934219 0.944356 1.949
18、283 0.958216 1.9756437 0.970875 1.968362 0.973322 1.975603 0.979902 1.9883068 0.985991 1.984804 0.987178 1.988268 0.990344 1.9943819 0.993268 1.992698 0.993837 1.994362 0.995360 1.99729910 0.996765 1.996490 0.997038 1.997291 0.997770 1.99870211 0.998445 1.998313 0.998577 1.998698 0.998928 1.99937612
19、 0.999253 1.999189 0.999316 1.999374 0.999485 1.99970013 0.999641 1.999610 0.999671 1.999699 0.999752 1.99985614 0.999827 1.999813 0.999842 1.999855 0.999881 1.99993115 0.999917 1.999910 0.999924 1.999931 0.999943 1.99996716 0.999960 1.999957 0.999964 1.999967 0.999973 1.999984(3)SOR迭代()。以文件名math4c.
20、m保存。function math4cA=4 -1 0 -1 0 0;-1 4 -1 0 -1 0;0 -1 4 -1 0 -1;-1 0 -1 4 -1 0;0 -1 0 -1 4 -1;0 0 -1 0 -1 4;b=0 5 -2 5 -2 6;x0=0 0 0 0 0 0;imax=100;tol=10-4;w0=1.334 1.95 0.95; for i=1:3w=w0(i);tx=sor(A,b,imax,x0,tol,w);for j=1:size(tx,1)fprintf(%d %f %f %f %f %f %fn,j,tx(j,1),tx(j,2),tx(j,3),tx(j,
21、4),tx(j,5),tx(j,6)endfprintf(n)end function tx=sor(A,b,imax,x0,tol,w) %初始值x0,次数imax,精度tol,松弛因子wdel=10-10;tx=x0;n=length(x0);for i=1:n dg=A(i,i); if abs(dg)del disp(对角元太小); return endend for k=1:imax % SOR循环 x=x0; for i=1:n sm=b(i); for j=1:n if j=i sm=sm-A(i,j)*x(j); end end x(i)=sm/A(i,i); x(i)=w*x
22、(i)+(1-w)*x0(i); end tx=tx;x; if norm(x-x0)tol return else x0=x; end endw=1.334时的近似解y:1 0.000000 0.000000 0.000000 0.000000 0.000000 0.0000002 0.000000 1.667500 -0.110889 1.630519 0.432889 2.1083873 1.099889 1.584755 1.145478 2.016105 1.092449 2.0431474 0.833524 2.162521 1.025372 1.978394 1.030507 2
23、.0042245 1.102598 1.998570 0.985252 2.046688 1.006313 1.9957766 0.980826 1.991270 1.016176 1.985512 0.988740 2.0030507 0.998661 2.004109 0.992153 1.998020 1.005488 1.9981948 1.001157 1.998227 1.000767 2.003133 0.998018 2.0001989 1.000067 2.000210 1.000925 1.998623 1.000339 2.00035510 0.999588 2.0002
24、14 0.999422 2.000243 1.000158 1.99974111 1.000290 1.999885 1.000149 2.000118 0.999862 2.00009012 0.999904 2.000010 1.000023 1.999890 1.000043 1.99999213 0.999999 2.000018 0.999959 2.000037 1.000001 1.99999014 1.000019 1.999987 1.000018 2.000000 0.999992 2.000007w=1.95时的近似解y:1 0.000000 0.000000 0.000
25、000 0.000000 0.000000 0.0000002 0.000000 2.437500 0.213281 2.541475 1.452250 3.7369473 2.427250 2.117106 2.915202 3.335517 2.125277 1.8321354 0.352266 3.055213 0.264204 0.605361 -0.316318 1.1590665 1.449877 0.216457 -0.260312 2.288115 1.111526 2.2388536 -0.156404 2.570585 2.732354 2.061435 1.318601
26、2.7729307 2.406694 3.143548 0.318497 2.450485 1.851223 1.3484558 0.440732 0.723724 0.927227 1.678891 -0.905016 1.6547969 0.752580 2.127673 0.806547 1.161433 2.294917 2.86490710 0.888488 2.361313 1.372761 3.555270 1.125805 1.42138911 2.040270 2.406936 1.320379 1.247140 0.429774 2.42788012 -0.156894 0
27、.927625 0.014429 1.392780 0.931503 1.07965613 1.280247 2.641519 1.504345 2.925956 1.380548 3.30571214 1.497909 2.064673 1.640338 1.860755 1.238660 1.18808515 0.490633 2.118756 -0.014118 1.505930 0.194499 1.88425416 1.300934 1.146823 1.250203 2.345363 1.461241 2.45678817 0.466554 2.897293 1.590786 1.
28、924713 1.185233 1.94436118 1.907502 1.968288 0.359467 2.291971 0.923781 1.70344119 0.264750 1.322276 1.275879 1.461527 0.334939 2.09200520 1.105592 2.505588 0.766736 2.125092 1.984117 2.27863621 1.207144 1.986715 1.411942 2.662724 0.517526 1.70091122 1.119814 2.036646 0.803792 1.097964 0.890667 2.13
29、518323 0.464299 1.555081 0.595659 2.345363 1.121235 1.73356224 1.460383 2.509095 1.670784 2.282451 1.140817 2.64877225 0.948515 1.886916 0.761598 1.659000 0.961134 1.24849926 0.827545 1.888190 0.639380 2.045129 0.638059 2.36167827 1.131325 1.817992 1.452178 2.065139 1.463189 2.10264828 0.818267 2.53
30、0554 0.910872 2.031878 0.884197 1.80258129 1.446832 1.613900 0.815747 2.041269 0.845666 2.02248730 0.407405 1.912844 1.163633 1.676437 0.957354 2.03761931 1.362740 2.318616 0.860476 2.395412 1.406941 2.09462832 1.003486 1.829381 1.288265 1.964971 0.559283 1.83578333 0.896435 2.037280 0.647190 1.5959
31、45 1.159823 2.06192434 0.919585 1.831300 1.086140 2.464556 1.022586 1.99417635 1.220624 2.320824 1.298201 1.822610 1.045628 2.17314936 0.860331 1.794745 0.614580 1.934783 0.909209 1.60335537 1.000831 1.963245 1.123073 2.078099 0.913042 2.39441938 1.019366 2.061965 1.143640 1.962880 1.287001 1.835240
32、39 0.993714 2.148007 0.837278 2.092786 0.764415 1.96234840 1.123358 1.725356 1.047575 1.880335 1.013225 2.06540941 0.690584 2.139711 0.996464 1.967565 1.071620 1.97105242 1.346242 2.069258 1.007199 2.238031 1.067652 2.06399043 0.820874 1.883370 1.083539 1.760253 0.793192 1.87911644 0.996436 2.048967
33、 0.768701 2.012446 1.167476 2.08372645 1.033324 1.938613 1.236691 2.201454 0.949997 2.01147146 1.036624 2.167183 0.960446 1.782814 1.028718 1.98382047 0.940830 1.807049 0.829747 2.108483 0.923652 1.89515348 1.015033 2.070414 1.197840 1.963497 1.037949 2.21455249 1.002250 2.049151 0.922812 2.016646 1
34、.100618 1.80759850 1.029938 1.979324 0.977568 2.036897 0.818524 2.08337751 0.979466 1.910227 1.036180 1.884106 1.112786 1.99341252 0.919244 2.118537 0.963706 2.108021 1.000089 1.98860853 1.187164 1.960983 1.062565 2.019167 0.984685 2.03385754 0.812517 1.968703 0.951155 1.859115 0.947115 1.91824355 1
35、.094170 2.026047 0.950563 2.129867 1.086392 2.09568556 0.986546 1.986712 1.150443 1.985525 0.951040 1.95857257 0.999247 2.061729 0.859919 1.921226 1.018007 1.97984558 0.992407 1.878145 1.025445 2.092316 0.958667 2.01140159 0.992813 2.104514 1.077340 1.926350 1.059870 2.05605960 1.021874 1.978265 0.9
36、07355 2.064654 0.991375 1.89737561 1.000143 1.971349 1.055534 1.961517 0.925436 2.08821662 0.967136 2.001920 0.972424 1.970745 1.100515 1.95175263 1.017895 2.042457 1.009112 2.089960 0.945543 2.02373064 1.047553 1.960742 1.027629 1.924642 1.007427 1.99454665 0.898949 2.005122 0.936854 1.995165 0.990
37、426 1.96973066 1.096138 2.006550 1.046068 2.069252 1.031292 2.06647067 0.945622 2.004981 1.024828 1.935060 0.973447 1.93601268 1.022429 2.005361 0.916175 2.018818 1.005819 2.02276069 0.990479 1.952238 1.076619 2.017670 0.990898 2.01129270 0.994375 2.075546 0.978160 1.965387 1.034107 1.99525271 1.025
38、299 1.946544 0.975500 2.049899 0.963550 1.97479772 0.974232 2.008508 1.039462 1.941503 0.997971 2.04219173 1.000110 2.010219 0.959544 2.034915 1.044498 1.96188974 1.021899 2.002938 1.038307 2.017875 0.949293 2.03016175 0.989342 1.985969 0.980185 1.943444 1.028464 1.97556376 0.975714 2.005707 0.98212
39、2 2.047049 0.986765 2.00804777 1.048790 2.003197 1.045402 1.994770 1.015505 2.02204778 0.952658 2.003576 0.966810 1.973268 0.984730 1.95543179 1.033686 1.989400 0.991604 2.030280 1.002374 2.03940580 0.977592 1.996210 1.040100 1.981016 1.005852 1.98496781 1.010185 2.030968 0.960419 2.006557 1.005405
40、1.99762082 1.008618 1.958121 1.019222 2.009978 0.978153 2.00098283 0.976261 2.026933 1.000211 1.968401 1.018959 2.00841384 1.020277 1.993644 0.985397 2.042028 1.003480 1.98658685 0.998127 1.999703 1.027677 1.974349 0.977505 2.01526986 0.989130 1.997509 0.967432 1.992226 1.023810 1.98122587 1.005322
41、2.000691 1.018334 2.030525 0.983445 2.01870488 1.010162 2.005164 1.009100 1.972321 1.013869 1.99342989 0.979370 1.996234 0.972822 2.009750 0.986538 1.98643190 1.022516 1.994742 1.021394 2.005581 1.006331 2.02640791 0.978767 2.008160 0.999248 1.987067 1.004532 1.97675692 1.017844 2.002790 0.984439 2.015608 0.993332 2.01124593 0.992017 1.982621 1.019402 1.987488 0.997245 1.99743394 0.993012 2.021219 0.984561