用Powell法编程求解无约束优化问题题目:用Powell法编程求解无约束优化问题min,要求:(1)程序流程图(2)源程序清单(3)运行结果求解过程:流程图....程序清单源程序由以下几个部分组成:(1)目标函数程序清单:建立函数f()以f.m存盘%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%函数名:f(),参数2个%函数作用:带入参数,求解目标函数f()%可以更改目标函数为任意二维函数,结果由m返回%作者:WYH学号:xxxxxxxx%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%functionm=f(x1,x2)m=x1^2+x2^2-x1*x2-10*x1-4*x2+60;%目标函数(2)关于α的目标函数清单:建立函数y()以y.m存盘%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%函数名:y(),参数5个%函数作用:带入参数,求解关于α的一元函数y(α),根据原目标函数更改此函数,%其中原目标函数变量X1=x1+alpha*d1,X2=x2+alpha*d2结果由m返回%作者:WYH学号:xxxxxxxx%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%functionm=y(x1,x2,d1,d2,alpha)m=(x1+alpha*d1)^2+(x2+alpha*d2)^2-(x1+alpha*d1)*(x2+alpha*d2)-10*(x1+alpha*d1)-4*(x2+alpha*d2)+60;%含α的目标函数,其形式与原目标函数f(x)=x1^2+x2^2-x1*x2-10*x1-4*x2+60一致(3)外推法求解一元函数最小值区间:建立函数section()以sextion.m存盘%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%函数名:section(),参数4个%函数作用:利用外推法求解关于α的函数y(α)的极小点α*所在的区间【a,b】,%结果由a、b返回%作者:WYH学号:xxxxxxxx%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function[a,b]=section(x1,x2,d1,d2)x11=x1;%给出起始点坐标x1,x2和搜索方向d1,d2x22=x2;d11=d1;d22=d2;h0=1;%初始化h=h0;alpha1=0;y1=y(x11,x22,d11,d22,alpha1);%代入α1求解y1alpha2=h;y2=y(x11,x22,d11,d22,alpha2);t=0;ify2y1h=-h;alpha3=alpha1;y3=y1;t=1;%如果y2y1,则改变搜索方向endwhile(1)ift==1alpha1=alpha2;y1=y2;%实现交换alpha2=alpha3;y2=y3;elset=1;endalpha3=alpha2+h;y3=y(x11,x22,d11,d22,alpha3);ify3y2h=2*h;%改变搜索步长,将其加倍elsebreak;endendifalpha1alpha3tem=alpha1;alpha1=alpha3;alpha3=tem;%比较大小,保证输出区间为【a,b】a=alpha1;b=alpha3;elsea=alpha1;b=alpha3;end(1)黄金分割法求解区间已知的一元函数最小值:建立函数ALPHA()以ALPHA.m存盘%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%函数名:ALPHA(),参数6个%函数作用:利用黄金分割法求解关于α的函数y(α)的极小点α*,已知α所在的%区间【A,B】,结果由α返回%作者:WYH学号:xxxxxxxx%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%functionalpha=ALPHA(x1,x2,d1,d2,A,B)x11=x1;x22=x2;%给出起始点坐标x1,x2和搜索方向d1,d2d11=d1;d22=d2;a=A;b=B;%获取区间epsilon=0.000001;%初始化,给定进度r=0.618;alpha1=b-r*(b-a);y1=y(x11,x22,d11,d22,alpha1);%代入α1求解y1alpha2=a+r*(b-a);y2=y(x11,x22,d11,d22,alpha2);while(1)ify1=y2%根据区间消去法原理缩短搜索空间a=alpha1;alpha1=alpha2;y1=y2;alpha2=a+r*(b-a);y2=y(x11,x22,d11,d22,alpha2);elseb=alpha2;alpha2=alpha1;y2=y1;alpha1=b-r*(b-a);y1=y(x11,x22,d11,d22,alpha1);endifabs(b-a)epsilon&abs(y2-y1)epsilon%判断是否满足进度要求break;%满足进度要求则退出循环迭代过程endendalpha=0.5*(a+b);%返回值(2)主程序,建立Powell.m存盘,运行程序%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%程序名:powell法求解二维函数的极小值,主程序%程序作用:根据无约束优化方法中的Powell法原理,在二维空间求解f(x1,x2)的%最小点,结果返回最小点变量坐标(x1,x2)及最小值fmin%作者:WYH学号:xxxxxxxx%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%clearallclck=0;n=2;x=[0;0;];ff(1)=f(x(1),x(2));%初始化epsilon=0.00001;d=[1;0;0;1;];while(1)x00=[x(1);x(2);];fori=1:n[a(i),b(i)]=section(x(2*i-1),x(2*i),d(2*i-1),d(2*i));%调用section()函数求解一元函数有y(α)最小值时的区间alpha(i)=ALPHA(x(2*i-1),x(2*i),d(2*i-1),d(2*i),a(i),b(i));%调用ALPHA()函数求解y(α)最小值点α*x(2*i+1)=x(2*i-1)+alpha(i)*d(2*i-1);%沿di方向搜索x(2*i+2)=x(2*i)+alpha(i)*d(2*i);ff(i+1)=f(x(2*i+1),x(2*i+2));%搜索到的点对应的函数值endfori=1:nDelta(i)=ff(i)-ff(i+1);enddelta=max(Delta);%求出函数值之差的最大值fori=1:n%寻找函数值之差最大值的下标ifdelta==Delta(i)m=i;break;endendd(2*n+1)=x(2*n+1)-x(1);%求出反射点搜索方向dn+1d(2*n+2)=x(2*n+2)-x(2);x(2*n+3)=2*x(2*n+1)-x(1);%搜索到反射点Xn+1x(2*n+4)=2*x(2*n+2)-x(2);ff(n+2)=f(x(2*n+3),x(2*n+4));%反射点所对应的函数值f0=ff(1);f2=ff(n+1);f3=ff(n+2);k=k+1;%记录迭代次数R(k,:)=[k,x',d',ff];%保存迭代过程的中间运算结果iff3f0&(f0-2*f2+f3)*(f0-f2-delta)^20.5*delta*(f0-f3)^2%判断是否需要对原方向组进行替换[a(n+1),b(n+1)]=section(x(2*n+1),x(2*n+2),d(2*n+1),d(2*n+2));alpha(n+1)=ALPHA(x(2*n+1),x(2*n+2),d(2*n+1),d(2*n+2),a(n+1),b(n+1));x(1)=x(2*n+1)+alpha(n+1)*d(2*n+1);%沿反射方向进行搜索,将搜索结果作为下一轮迭代的始点x(2)=x(2*n+2)+alpha(n+1)*d(2*n+2);fori=m:n%根据函数值之差最大值的下标值m,对原方向组进行替换d(2*i-1)=d(2*i+1);d(2*i)=d(2*i+2);endelseiff2f3%如果不需要对原方向组进行替换,选取终点及反射点中函数值较小者作为下一轮迭代的始点x(1)=x(2*n+1);x(2)=x(2*n+2);elsex(1)=x(2*n+3);x(2)=x(2*n+4);endendRR(k,:)=alpha;%保存迭代过程的中间运算结果ff(1)=f(x(1),x(2));%计算下一轮迭代过程需要的f0值if(((x(2*n+1)-x00(1))^2+(x(2*n+2)-x00(2))^2)^(1/2))epsilon%判断是否满足精度要求break;%满足进度要求则退出循环迭代过程endendxx=[x(1);x(2)]fmin=f(x(1),x(2))%显示最小值及其所对应的坐标二、运行结果xx=8.00006.0000fmin=8.0000