end end end
minx = min(x1,x3); maxx = x1+x3 - minx; format short;
% syms x1 x2 x3 ;
% f=x1^2+x1*x2+2*x2^2+2*x3^2+2*x2*x3+4*x1+6*x2+12*x3; % [x,mf]=minRosen(f,[1 1 1 ;1 1 -2],[6;-2],[1 1 3],[x1 x2 x3])
% syms x1 x2;
%f=x1^3+x2^2-2*x1-4*x2+6;
% [x,mf]=minRosen(f,[2,-1;1,1;-1,0;0,-1],[1;2;0;0],[1 2],[x1 x2])
% syms x1 x2 x3; % f=-x1*x2*x3;
% [x,mf]=minRosen(f,[-1,-2,-2;1,2,2],[0;72],[10 10 10],[x1 x2 x3])
% syms x1 x2;
%f=2*x1^2+2*x2^2-2*x1*x2^3-4*x1^7-6*x2;
% [x,mf]=minRosen(f,[1 1;1 5;-1 0;0 -1],[2;5;0;0],[-1 -1],[x1 x2])
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Main.m
syms x1 x2 x3;
% f=2*x1^2+2*x2^2-2*x1*x2^3-4*x1^7-6*x2; % var=[x1,x2]; % valst=[-1,-1];
% A=[1 1;1 5;-1 0;0 -1]; % b=[2 5 0 0]';
% f=x1^3+x2^2-2*x1-4*x2+6; % var=[x1,x2]; % valst=[0 0];
% A=[2,-1;1,1;-1,0;0,-1]; % b=[1 2 0 0]'; var=[x1,x2,x3]; valst=[10,10,10]; f=-x1*x2*x3; A=[-1,-2,-2;1,2,2]; b=[0 72]';
[x,mimfval]=MinRosenGradientProjectionMethod(f,A,b,valst,var) [x2,fval]=fmincon('confun',valst,A,b)
MinRosenGradientProjectionMethod.m
function [x,minf]=MinRosenGradientProjectionMethod(f,A,b,x0,var,eps) %f is the objection function;
%A is the constraint matrix; 约束矩阵
%b is the right-hand-side vector of the constraints; %x0 is the initial feasible point; 初始可行解
%var is the vector of independent variable; 自变量向量 %eps is the precision; 精度
%x is the value of the independent variable when the objective function is minimum; 自变量的值是当目标函数最小
%minf is the minimum value of the objective function; 目标函数的最小值
format long; ifnargin == 5 eps=1.0e-6; end
syms l;
x00=transpose(x0); n=length(var); sz=size(A);
m=sz(1);% m is the number of rows of A 行数
gf=jacobian(f,var);êlculate the jacobian matrix of the objective function 计算目标函数的雅可比矩阵
bConti=1;
whilebConti k=0; s=0; A1=A; A2=A; b1=b; b2=b; fori=1:m
dfun=A(i,:)*x00-b(i); %separate matrix A and b 分离矩阵A和b
if abs(dfun)<0.0000001 %find matrixs that satisfy A1 x_k=b1 找到满足的矩阵 k=k+1; A1(k,:)=A(i,:); b1(k,1)=b(i);
else%findmatrixs that satisfy A2 x_k A2(s,:)=A(i,:); b2(s,1)=b(i); end end if k>0 A1=A1(1:k,:); b1=b1(1:k,:); end if s>0 A2=A2(1:s,:); b2=b2(1:s,:); end while 1 P=eye(n,n); if k>0 tM=transpose(A1); P=P-tM*inv(A1*tM)*A1; êlculate P; end gv=Funval(gf,var,x0); gv=transpose(gv); d=-P*gv; êlculate the searching direction 计算搜索方向 % flg=1; % if(P==zeros(n)) % flg =0; % end % if flg==1 % d=d/norm(d); %normorlize the searching direction 搜索方向 % end % 加入这部分会无止境的循环 if d==0 if k==0 x=x0; bConti=0; break; else w=-inv(A1*tM)*A1*gv; if w>=0 x=x0; bConti=0; break; else [u,index]=min(w);%find the negative component in w sA1=size(A1); if sA1(1)==1 k=0; else k=sA1(2); A1=[A1(1:(index-1),:);A1((index+1):sA1(1),:)]; Tlete corresponding row in A1 删除对应的行A1 end end end else break; end end d1=transpose(d); y1=x0+l*d1; %new iteration variable 新的迭代变量 tmpf=Funval(f,var,y1); bb=b2-A2*x00; dd=A2*d; ifdd>=0 [tmpI,lm]= ForwardBackMethod(tmpf,0,0.001); %find the searching interval 找到搜索区间 else lm=inf; %find lambda_max fori=1:length(dd) % if(dd(i)>0) % % ifdd(i)<0 % % if bb(i)/dd(i) if lm==inf lm=1e9; end [xm,minf]=GoldenSectionSearch(tmpf,0,lm,1.0e-14); %guarantee lambda>0 保证λ> 0 %find the minimizer by one dimension searching method 找到由一维搜索方法得到目标 tol=norm(xm*d); iftol x=x0; break; end x0=x0+xm*d1; disp('x0'); x0 end minf=Funval(f,var,x); GoldenSectionSearch.m 0.618搜索法确定局部最优值 function [x,minf]=GoldenSectionSearch(f,a,b,eps) %0.618 search method to find minimum value of function f 0.618搜索方法找到函数f的最小值 format long; ifnargin==3 eps=1.0e-6; end l=a+0.382*(b-a); u=a+0.618*(b-a); k=1; tol=b-a;
相关推荐: