6、gg(x);n二1;while(norm(xl-x)>=le~6)&&(n<=10000)X二xl;xl二ggl(x);%第2问时填写gg2(x)nFn+1;endy二x;ggl.mfunctionf=ggl(x)f(1)二(x“2-exp(x)+2)/3;gg2.mfunctionf二gg2(x)f(1)二(x“3+2*x"2-20)/10;Steffensen迭代:Steff.mfunction[gen,time]二Steff(fun,xO,tol)if(nargin==2)tol=l.Oe-8endtime
7、=0;wucha=0.1gen二xO;while(wucha>tol)xl=gcn;y二subs(fun,xl)+xl;z二subs(fun,y)+y;gen=xl-(y-xl)2/(z-2*y+xl);wucha=abs(gen-xl);time=time+l;endgen;time;Steff_main.mfunctionSteff_main()[x_steff,time_steff]二Steff('(x"2-exp(x)+2)/3',0.3)%第2题时改为%[x_steff,time_steff]二Stef
8、f('(20~x3~2*x2)/10,,-2.1)x=0:0.02:1;y=(x."2-exp(x)+2)/3;%第2题时改为y=(20~x"3-2*x*2)/10;plot(x,y)grid牛顿迭代:functionroot=NewtonRoot(f,a,b,eps)%f是非线性函数为有根区间的左限%b为有根区间的右限%eps为根的精度%root为求出的函数零点if(nargin==3)eps=1・Oe-8;endfl=subs(sym(f),findsym(sym(f)),a);f2=subs(sym(f),
9、findsym(sym(f)),b);if(fl二二0)root=a;endif(f2=0)root二b;endif(fl*f2>0)dispC两端点函数值乘积大于0!');return;elsetol=l;fun=diff(svm(f));fa二subs(sym(f),findsym(sym(f)),a);fb=subs(sym(f),findsym(sym(f)),b);dfa二subs(sym(fun),findsym(sym(fun)),a);dfb=subs(sym(fun),findsym(sym(f
10、un)),b);if(dfa>dfb)root=a-fa/dfa;elseroot=b-fb/dfb;endwhile(tol>eps)rl=root;fx=subs(sym(f),findsym(sym(f)),rl);dfx二subs(sym(fun),findsym(sym(fun)),rl);root二rl-fx/dfx;tol^abs(root-rl);endencl测试数据:第1题不动点迭代测试数据:»BDD(l)牛顿迭代输入数据:>>fplotC[(x"2-exp(x)+2)/3]',[-2.52.
11、5]);grid»r=NewtonRootC(x2~exp(x)+2)/3,,1,2)第2题不动点迭代测试数据:»BDD(l)牛顿迭代输入数据:»fplotC[(20-x"3-2*x"2)/10]',[-2.52.5]);grid»r=NewtonRootC(20-x3~2*x2)/10,,1,2)实验结果:第1题实验结果:不动点迭代实验结果:»BDD(l)ans=0.2575Zil«D&racficsktopX:ndwHelpTogetstarted,selectHATLABHelporDe:toufrothe
12、Helpmenu.FhwelvAerittype*nanw*nutrt.betrcrujiatedbythenatchuvtead-tai:**・Couldnotpareethefile:d:watlab7toolboxcc«linkccslinkMnfo.xmI»BDD(l)ans=0.25?5»Steffensen迭代实验结果:»clear»