資源描述:
《最優(yōu)化方法源程序1》由會員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、最優(yōu)化方法源程序一、線性規(guī)劃問題例解線性規(guī)劃函數(shù)linprog()線性規(guī)劃規(guī)劃矩陣形式:格式為:x=linprog(c,A,B,Aeq,Beq,LB,UB)例解線性規(guī)劃即%線性規(guī)劃求解clear;c=[-100,-200]';A=[1,1;1,0];B=[500,200]';Aeq=[2,6];Beq=1200;LB=[0,0]';UB=[];[x,fval,exitflag,output]=linprog(c,A,B,Aeq,Beq,LB,UB)執(zhí)行結(jié)果:20Optimizationterminated.x=200.0000133.333
2、3fval=-4.6667e+004exitflag=1output=iterations:4algorithm:'large-scale:interiorpoint'cgiterations:0message:'Optimizationterminated.'二、無約束一元非線性規(guī)劃問題例10.618法function[s,phis,k,G,E]=golds(phi,a,b,delta,epsilon)%功能:0.618法精確線搜索%輸入:phi是目標(biāo)函數(shù),a,b是搜索區(qū)間的兩個端點(diǎn)%delta,epsilon分別是自變量和函數(shù)值的容許誤
3、差%輸出:s,phis分別是近似極小點(diǎn)和極小值,G是nx4矩陣,%其第k行分別是a,p,q,b的第k次迭代值[ak,pk,qk,bk],%E=[ds,dphi],分別是s和phis的誤差限.%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%t=(sqrt(5)-1)/2;h=b-a;phia=feval(phi,a);phib=feval(phi,b);p=a+(1-t)*h;q=a+t*h;phip=feval(phi,p);phiq=feval(phi,q);k=1;G(k,:)=[a,p,q,b];while(abs(p
4、hib-phia)>epsilon)
5、(h>delta)if(phip6、iq;end20E=[ds,dphi];程序調(diào)用:>>[s,phis,k,G,E]=golds(inline('2*x^2-x-1'),-1,1,0.16,3)s=0.236067977499790phis=-1.124611797498107k=7G=-1.000000000000000-0.2360679774997900.2360679774997901.000000000000000-0.2360679774997900.2360679774997900.5278640450004211.000000000000000-0.2360
7、679774997900.0557280900008410.2360679774997900.5278640450004210.0557280900008410.2360679774997900.3475241575014720.5278640450004210.0557280900008410.1671842700025240.2360679774997900.3475241575014720.1671842700025240.2360679774997900.2786404500042060.3475241575014720.16718
8、42700025240.2097567425069400.2360679774997900.278640450004206E=0.1114561800016830.012076339517143例2最速降法function[x,val,k]=grad(fun,gfun,x0)%功能:用最速下降法求解無約束問題:minf(x)%輸入:x0是初始點(diǎn),fun,gfun分別是目標(biāo)函數(shù)和梯度%輸出:x,val分別是近似最優(yōu)點(diǎn)和最優(yōu)值,k是迭代次數(shù).maxk=5000;%最大迭代次數(shù)rho=0.5;sigma=0.4;k=0;epsilon=1e-5;
9、while(k