資源描述:
《matlab實(shí)現(xiàn)最速下降法_和牛頓法和共軛梯度法》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫(kù)。
1、最速下降法:題目:f=(x-2)^2+(y-4)^2M文件:function[R,n]=steel(x0,y0,eps)symsx;symsy;f=(x-2)^2+(y-4)^2;v=[x,y];j=jacobian(f,v);T=[subs(j(1),x,x0),subs(j(2),y,y0)];temp=sqrt((T(1))^2+(T(2))^2);x1=x0;y1=y0;n=0;symskk;while(temp>eps)d=-T;f1=x1+kk*d(1);f2=y1+kk*d(2);fT=[subs(j(1),x,f1),su
2、bs(j(2),y,f2)];fun=sqrt((fT(1))^2+(fT(2))^2);Mini=Gold(fun,0,1,0.00001);x0=x1+Mini*d(1);y0=y1+Mini*d(2);T=[subs(j(1),x,x0),subs(j(2),y,y0)];temp=sqrt((T(1))^2+(T(2))^2);x1=x0;y1=y0;n=n+1;endR=[x0,y0]調(diào)用黃金分割法:M文件:functionMini=Gold(f,a0,b0,eps)symsx;formatlong;symskk;u=a0+0.
3、382*(b0-a0);v=a0+0.618*(b0-a0);k=0;a=a0;b=b0;array(k+1,1)=a;array(k+1,2)=b;while((b-a)/(b0-a0)>=eps)Fu=subs(f,kk,u);Fv=subs(f,kk,v);if(Fu<=Fv)b=v;v=u;u=a+0.382*(b-a);k=k+1;elseif(Fu>Fv)a=u;u=v;v=a+0.618*(b-a);k=k+1;endarray(k+1,1)=a;array(k+1,2)=b;endMini=(a+b)/2;輸入:[R,n]
4、=steel(0,1,0.0001)R=1.999994136676423.99999120501463R=1.999994136676423.99999120501463n=1牛頓法:題目:f=(x-2)^2+(y-4)^2M文件:symsx1x2;f=(x1-2)^2+(x2-4)^2;v=[x1,x2];df=jacobian(f,v);df=df.';G=jacobian(df,v);epson=1e-12;x0=[0,0]';g1=subs(df,{x1,x2},{x0(1,1),x0(2,1)});G1=subs(G,{x1,
5、x2},{x0(1,1),x0(2,1)});k=0;mul_count=0;sum_count=0;mul_count=mul_count+12;sum_count=sum_count+6;while(norm(g1)>epson)p=-G1g1;x0=x0+p;g1=subs(df,{x1,x2},{x0(1,1),x0(2,1)});G1=subs(G,{x1,x2},{x0(1,1),x0(2,1)});k=k+1;mul_count=mul_count+16;sum_count=sum_count+11;end;kx0mul_
6、countsum_count結(jié)果::k=1x0=24mul_count=28sum_count=17共軛梯度法:題目:f=(x-2)^2+(y-4)^2M文件:functionf=conjugate_grad_2d(x0,t)x=x0;symsxiyiaf=(xi-2)^2+(yi-4)^2;fx=diff(f,xi);fy=diff(f,yi);fx=subs(fx,{xi,yi},x0);fy=subs(fy,{xi,yi},x0);fi=[fx,fy];count=0;whiledouble(sqrt(fx^2+fy^2))>ts=
7、-fi;ifcount<=0s=-fi;elses=s1;endx=x+a*s;f=subs(f,{xi,yi},x);f1=diff(f);f1=solve(f1);iff1~=0ai=double(f1);elsebreakx,f=subs(f,{xi,yi},x),countendx=subs(x,a,ai);f=xi-xi^2+2*xi*yi+yi^2;fxi=diff(f,xi);fyi=diff(f,yi);fxi=subs(fxi,{xi,yi},x);fyi=subs(fyi,{xi,yi},x);fii=[fxi,fyi
8、];d=(fxi^2+fyi^2)/(fx^2+fy^2);s1=-fii+d*s;count=count+1;fx=fxi;fy=fyi;endx,f=subs(f,{xi,yi},x),c