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是非線性函數(shù)為有根區(qū)間的左限%b為有根區(qū)間的右限%eps為根的精度%root為求出的函數(shù)零點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兩端點函數(shù)值乘積大于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測試數(shù)據(jù):第1題不動點迭代測試數(shù)據(jù):?BDD(l)牛頓迭代輸入數(shù)據(jù):>>fplotC[(x"2-exp(x)+2)/3]',[-2.52.
11、5]);grid?r=NewtonRootC(x2~exp(x)+2)/3,,1,2)第2題不動點迭代測試數(shù)據(jù):?BDD(l)牛頓迭代輸入數(shù)據(jù):?fplotC[(20-x"3-2*x"2)/10]',[-2.52.5]);grid?r=NewtonRootC(20-x3~2*x2)/10,,1,2)實驗結(jié)果:第1題實驗結(jié)果:不動點迭代實驗結(jié)果:?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迭代實驗結(jié)果:?clear?