%线性无关的初始向量组: P; %自变量向量: var %精度: eps; %目标函数取最小值时的自变量值: x; %目标函数的最小值: minf;format long; if nargin==3 eps=1.0e-6;endn=length(var)+1; syms l;while 1 y=zeros(size(P));y(:,1)=x0;for i=1:n-1 %在每个搜索方向上进行一维搜索 yv=y(:,i)+l*P(:,i); fy=Funval(f,var,yv); [a,b]=minJT(fy,0,0.1); tl=minHJ(fy,a,b); y(:,i+1)=y(:,i)+tl*P(:,i);endP(:,n)=y(:,n)-y(:,1); if norm((P(:,n))<=eps) %精度判断x=y(:,n);break;elsefor j=1:nFY(j)=Funval(f,var,y(:,j));endmaxDF=-inf;m=0;for j=1:n-1 %求出算法中对应的m df=FY(j)-FY(j+1); if df>maxDFmaxDF=df;m=j+1;endendtmpF=Funval(f,var,2*y(:,n)-y(:,1)); fl=FY(1)-2*FY(n)+tmpF; if fl<2*maxDF yv=y(:,n)+l*P(:,n); fy=Funval(f,var,yv); [a,b]=minJT(fy,0,0.1); tl=minHJ(fy,a,b); x0=y(:,n)+tl*P(:,n); P(:,m:(n-1))=P(:,(m+1):n); %重新设置搜索方向elsex0=y(:,n);endendendminf=Funval(f,var,x); format short;powell方法解决无约束最优化问题
%线性无关的初始向量组: P; %自变量向量: var %精度: eps; %目标函数取最小值时的自变量值: x; %目标函数的最小值: minf;format long; if nargin==3 eps=1.0e-6;endn=length(var)+1; syms l;while 1 y=zeros(size(P));y(:,1)=x0;for i=1:n-1 %在每个搜索方向上进行一维搜索 yv=y(:,i)+l*P(:,i); fy=Funval(f,var,yv); [a,b]=minJT(fy,0,0.1); tl=minHJ(fy,a,b); y(:,i+1)=y(:,i)+tl*P(:,i);endP(:,n)=y(:,n)-y(:,1); if norm((P(:,n))<=eps) %精度判断x=y(:,n);break;elsefor j=1:nFY(j)=Funval(f,var,y(:,j));endmaxDF=-inf;m=0;for j=1:n-1 %求出算法中对应的m df=FY(j)-FY(j+1); if df>maxDFmaxDF=df;m=j+1;endendtmpF=Funval(f,var,2*y(:,n)-y(:,1)); fl=FY(1)-2*FY(n)+tmpF; if fl<2*maxDF yv=y(:,n)+l*P(:,n); fy=Funval(f,var,yv); [a,b]=minJT(fy,0,0.1); tl=minHJ(fy,a,b); x0=y(:,n)+tl*P(:,n); P(:,m:(n-1))=P(:,(m+1):n); %重新设置搜索方向elsex0=y(:,n);endendendminf=Funval(f,var,x); format short;powell方法解决无约束最优化问题