資源描述:
《MATLAB最速下降法(梯度法)程序.doc》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、查看文章?matlab最速下降法2010-08-1817:13functionx=fsxsteep(f,e,a,b)%fsxsteep函數(shù)最速下降法%x=fsxsteep(f,e,a,b)為輸入函數(shù)f為函數(shù)e為允許誤差(a,b)為初始點(diǎn);%fsxTJPU2008.6.15x1=a;x2=b;Q=fsxhesse(f,x1,x2);x0=[x1x2]';fx1=diff(f,'x1');%對(duì)x1求偏導(dǎo)數(shù)fx2=diff(f,'x2');%對(duì)x2求偏導(dǎo)數(shù)g=[fx1fx2]';%梯度g1=subs(g);%把符號(hào)變量轉(zhuǎn)為數(shù)值d=-g1;wh
2、ile(abs(norm(g1))>=e)t=(-d)'*d/((-d)'*Q*d);t=(-d)'*d/((-d)'*Q*d);%求搜索方向x0=x0-t*g1;%搜索到的點(diǎn)v=x0;a=[10]*x0;b=[01]*x0;x1=a;x2=b;g1=subs(g);d=-g1;end;x=v;functionx=fsxhesse(f,a,b)%fsxhesse函數(shù)求函數(shù)的hesse矩陣;%本程序僅是簡單的求二次函數(shù)的hesse矩陣??;%x=fsxhesse(f)為輸入函數(shù)f為二次函數(shù)x1,x2為自變量;%fsxTJPU2008.6.1
3、5x1=a;x2=b;fx=diff(f,'x1');????%求f對(duì)x1偏導(dǎo)數(shù)fy=diff(f,'x2');????%求f對(duì)x2偏導(dǎo)數(shù)fxx=diff(fx,'x1');??%求二階偏導(dǎo)數(shù)對(duì)x1再對(duì)x1fxy=diff(fx,'x2');??%求二階偏導(dǎo)數(shù)對(duì)x1再對(duì)x2fyx=diff(fy,'x1');??%求二階偏導(dǎo)數(shù)對(duì)x2再對(duì)x1fyy=diff(fy,'x2');??%求二階偏導(dǎo)數(shù)對(duì)x2再對(duì)x2fxx=subs(fxx);??????%將符號(hào)變量轉(zhuǎn)化為數(shù)值fxy=subs(fxy);fyx=subs(fyx);fyy=sub
4、s(fyy);x=[fxx,fxy;fyx,fyy];%求hesse矩陣symsx1x2;X=[x1,x2];fx=X(1)^2+2*X(2)^2;z=fsxsteep(fx,0.001,1,1)