1、%delta 为允许误差x=xa;fa=eval(fx);x=xb;fb=eval(fx);disp( n xa xb xc fc );for i=1:n xc=(xa+xb)/2;x=xc;fc=eval(fx); X=i,xa,xb,xc,fc; disp(X), if fc*fa xb=xc; else xa=xc; end if (xb-xa)delta,break,endend(二)、简单迭代法程序:function x0,k=iterate (f,x0,eps,N)if nargineps & k fplot(x5-3*x3-2*x2+2,-3,3);grid 得下图: 由上图可得
2、知:方程在-3,3区间有根。(2)、二分法输出结果 f=x5-3*x3-2*x2+2f =x5-3*x3-2*x2+2 bisect(f,-3,3,20,10(-12) 2.0000 -3.0000 0 -1.5000 0.0313 3.0000 -3.0000 -1.5000 -2.2500 -31.6182 4.0000 -2.2500 -1.5000 -1.8750 -8.4301 5.0000 -1.8750 -1.5000 -1.6875 -2.9632 6.0000 -1.6875 -1.5000 -1.5938 -1.2181 7.0000 -1.5938 -1.5000 -1.
3、5469 -0.5382 8.0000 -1.5469 -1.5000 -1.5234 -0.2405 9.0000 -1.5234 -1.5000 -1.5117 -0.1015 10.0000 -1.5117 -1.5000 -1.5059 -0.0343 11.0000 -1.5059 -1.5000 -1.5029 -0.0014 12.0000 -1.5029 -1.5000 -1.5015 0.0150 13.0000 -1.5029 -1.5015 -1.5022 0.0068 14.0000 -1.5029 -1.5022 -1.5026 0.0027 15.0000 -1.5
4、029 -1.5026 -1.5027 0.0007 16.0000 -1.5029 -1.5027 -1.5028 -0.0003 17.0000 -1.5028 -1.5027 -1.5028 0.0002 18.0000 -1.5028 -1.5028 -1.5028 -0.0001 19.0000 -1.5028 -1.5028 -1.5028 0.0001 20.0000 -1.5028 -1.5028 -1.5028 -0.00002、迭代法求方程:迭代法输出结果: f=inline( x0,k=iterate(fun1,2)x0 = 2k = 1 x0,k=iterate(fun
5、1,1.5) NaN 6 x0,k=iterate(fun1,2.5) 5(3)、误差分析:由二分法和迭代法输出结果可知,通过定点迭代法得出方程的解误差比二分法大,而利用二分法求出的结果中,可以清楚看出方程等于零时的解,其误差比迭代法小。b、g(x)=cos(sin(x)cos(sin(x),-4,4);方程在-4,4区间无根。f=cos(sin(x)cos(sin(x) bisect(f,-4,4,20,10(-12) 2.0000 0 4.0000 2.0000 0.6143 3.0000 2.0000 4.0000 3.0000 0.9901 4.0000 3.0000 4.0000 3
6、.5000 0.9391 5.0000 3.5000 4.0000 3.7500 0.8411 6.0000 3.7500 4.0000 3.8750 0.7842 7.0000 3.8750 4.0000 3.9375 0.7554 8.0000 3.9375 4.0000 3.9688 0.7412 9.0000 3.9688 4.0000 3.9844 0.7341 10.0000 3.9844 4.0000 3.9922 0.7305 11.0000 3.9922 4.0000 3.9961 0.7288 12.0000 3.9961 4.0000 3.9980 0.7279 13.0
7、000 3.9980 4.0000 3.9990 0.7275 14.0000 3.9990 4.0000 3.9995 0.7273 15.0000 3.9995 4.0000 3.9998 0.7271 16.0000 3.9998 4.0000 3.9999 0.7271 17.0000 3.9999 4.0000 3.9999 0.7271 18.0000 3.9999 4.0000 4.0000 0.7270 19.0000 4.0000 4.0000 4.0000 0.7270 20.0000 4.0000 4.0000 4.0000 0.7270 x0,k=iterate(f,0
8、.5) 0.7682 15 x0,k=iterate(f,1) x0,k=iterate(f,1.5) 16 x0,k=iterate(f,2) x0,k=iterate(f,2.5)14(3) 、由于该方程无解,所以无法比较误差。c、g(x)=x2-sin(x+0.15)x2-sin(x+0.15),-10,10);x2-sin(x+0.15)x2-sin(x+0.15) bisect(f,-3,3,30,10(-12) 1.0000 -3.0000 3.0000 0 -0.1494 2.0000 -3.0000 0 -1.5000 3.2257 3.0000 -1.5000 0 -0.75
9、00 1.1271 4.0000 -0.7500 0 -0.3750 0.3637 5.0000 -0.3750 0 -0.1875 0.0726 6.0000 -0.1875 0 -0.0938 -0.0474 7.0000 -0.1875 -0.0938 -0.1406 0.0104 8.0000 -0.1406 -0.0938 -0.1172 -0.0191 9.0000 -0.1406 -0.1172 -0.1289 -0.0045 10.0000 -0.1406 -0.1289 -0.1348 0.0029 11.0000 -0.1348 -0.1289 -0.1318 -0.0008 12.0000 -0.1348 -0.1318 -0.1333 0.0011 13.0000 -0.1333 -0.1318 -0.1326 0.0001 14.0000 -0.1326 -0.1318 -0.1322 -0.0003 15.0000 -0.1326 -0.1322 -0.1324 -0.0001 16.0000 -0.1326 -0.1324 -0.
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1