资源描述:
《最优化多目标优化惩罚函数法梯度法牛顿法》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、2008-12-0812:30利用梯度法和牛顿法编程求最优解(matlab)f(x)二X「2+4*x2x0二[2;2]e二0.002利用梯度法和牛顿法编程求最优解方法一•梯度法functiony二fun(xl,x2)y二x「2+4*x2"2;%定义fun.m函数clcsymsxlx2d;f二x「2+4*x2八2;fxl二diff(f,'xl');fx2=diff(f,,x2,);xl=2;x2=2;forn=l:100fO二subs(f);fl=subs(fxl);f2=subs(fx2);if(double(sqrt(fl2+f22))<
2、=0.002)nvpa(xl)vpa(x2)vpa(fO)break;elseD二fun(xl-d*fl,x2-d*f2);Dd=diff(D,,d,);dd二solve(Dd);xl二xl-dd*f1;x2二x2-dd*f2;endend%结果n二10,xl二0.2223e-3,x2二-0.1390e-4,f0=0.5021e-7.方法二•牛顿法clcsymsxlx2;f二x「2+4*x2"2;fxl=diff(f,'xl');fx2=diff(f,'x2');fxlxl=diff(fxl,'xl');fxlx2=diff(fxl,'x2
3、');fx2xl二diff(fx2,'xl');fx2x2二diff(fx2,'x2');xl=2;x2=2;forn=l:100fO二subs(f);fl二subs(fxl);f2二subs(fx2);if(double(sqrt(f12+f22))<=0.002)nxl=vpa(xl,4)x2=vpa(x2,4)f0=vpa(f0,4)break;elseX=[xlx2]'-inv([fxlxlfxlx2;fx2xlfx2x2])*[flf2],;xl=X[l,1];x2二X[2,1];endend%结果n=2,xl=0,x2=0,f0
4、=0.惩罚函数法(内点法、外点法)求解约束优化问题最优值编程matlab1用外点法求下列问题的最优解用外点法求下列问站的说优解方法一:外点牛顿法:clcm=zeros(1,50);a=zeros(1,50);b=zeros(1,50);fO=zeros(1,50);%ab为最优点坐标,fO为最优点函数值,flf2最优点梯度。symsxlx2e;m(l)=l;c=10;a(l)=0;b(l)=0;数。赋初值。f=xl2+x22+e*(l-xl)2;f0(l)=l;%c为罚因子。%c为递增系fxl=diff(f,'xf);fx2=diff(f,
5、'x2');fxlxl=diff(fxl,'xf);fxlx2=diff(fxl,‘x2');fx2xl=diff(fx2,'xT);fx2x2=diff(fx2,'x2');%求偏导、海森元素。%外k二1:100点法e迭代循环.xl=a(k);x2=b(k);e=m(k);n=l:100%梯度法求最优值。fl二subs(fxl);%求解梯度值和海森矩阵f2=subs(fx2);fll=subs(fxlxl);f12=subs(fxlx2);f21=subs(fx2xl);f22=subs(fx2x2);if(double(sqrt(fl2
6、+f22))<=0.001)%最优值收敛条件a(k+1)二double(xl);b(k+1)二double(x2);fO(k+1)=double(subs(f));break;elseX=[xlx2],-inv([fllfl2;f21f22])*[flf2]';xl=X(l,1);x2=X(2,1);endendif(double(sqrt((a(k+1)-a(k))2+(b(k+1)-b(k))2))<=0.001)&&(double(abs((fO(k+1)-fO(k))/f0(k)))<=0.001)%罚因了迭代收敛条件a(k+1)%
7、输岀最优点坐标,罚因子迭代次数,最优值b(k+l)kfO(k+l)break;elsem(k+l)=c*m(k);endend方法二:外点梯度法:clcm=zeros(1,50);a=zeros(1,50);b=zeros(1,50);fO二zeros(1,50);symsdxlx2e;m(l)=l;c=10;a(l)=0;b(l)=0;f二x「2+x2"2+e*(l-xl厂2;f0(l)=l;fxl二diff(f,'xl');fx2二diff(f,'x2');fork二1:100xl=a(k);x2=b(k);e=m(k);forn=l:
8、100fl=subs(fxl);f2二subs(fx2);if(double(sqrt(f「2+f2“2))<=0.002)a(k+1)=double(xl);b(k+1)二do