0 svar
255 visningar
Hmowed är nöjd med hjälpen
Hmowed 63
Postad: 5 okt 2022 22:43

Nonlinear optimization!

Hej!

 

Jag håller på med att lösa ett min problem med 2 inequality constraints med hjälp av fmincon i Matlab. Men när jag försöker att köra koden så får jag följande 

fmincon2
Your initial point x0 is not between bounds lb and ub; FMINCON
shifted x0 to strictly satisfy the bounds.

Unable to perform assignment because the indices on the left side are not compatible with the size of the right
side.

Error in fmincon2>exerg (line 71)
   dC(1) = [2*x(1); -1];

Error in fmincon (line 672)
      [ctmp,ceqtmp,initVals.gnc,initVals.gnceq] = feval(confcn{3},X,varargin{:});

Error in fmincon2 (line 31)
  [x,z,ef,output,lambda] = fmincon( @exerf, x0, ...

Caused by:
    Failure in initial nonlinear constraint function evaluation. FMINCON cannot continue.
 
>> 

 

Här är koden

 

% The optimization problem is to
% maximize     x(1) - 2*x(1)^2 + 2*x(2) - x(2)^2 + x(1)*x(2)
% such that    x(1)^2 - x(2) <= 0
%              2*x(1) - x(2) >= 0
%==>
% minimize    -x(1) + 2*x(1)^2 - 2*x(2) + x(2)^2 -x(1)*x(2)
% such that    x(1)^2 - x(2) <= 0
%             -2*x(1) + x(2) <= 0

function fmincon2
  LB = [0; -inf];     % lower and upper bounds on the variables
  UB = [inf; inf];    % inf means infinity

  A = []; b = [];
  Aeq = []; beq = []; % we don't have any linear inequality
                      % or equality constraints

  x0 = [0; 0];        % starting point

  % now, set some options of the solver
  % for more options, type 'help optimset' in Matlab
  options = optimset( 'LargeScale', 'off', ...
                      'Display', 'iter', ...
	              'GradObj', 'on', ...
                      'GradCon', 'on', ...
                      'DerivativeCheck', 'on' );

  
  % run the solver
  [x,z,ef,output,lambda] = fmincon( @exerf, x0, ...
                                    A, b, ...
                                    Aeq, beq, ...
                                    LB, UB, ...
                                    @exerg, options );
  
  % if success, print out the solution and multipliers
  if( ef > 0 )
    disp( sprintf( '============================================================' ) );
    disp( sprintf( 'Optimal solution: ') ); disp( x' );
    disp( sprintf( 'Optimal value:    %g', z ) );
    disp( 'Lagrange multipliers: ' );
    disp( sprintf( 'Upper bounds    :' ) ); disp( lambda.lower' );
    disp( sprintf( 'Lower bounds    :' ) ); disp( lambda.upper' );
    disp( sprintf( 'Lin. ineq.      :' ) ); disp( lambda.ineqlin' );
    disp( sprintf( 'Lin. eq.        :' ) ); disp( lambda.eqlin' );
    disp( sprintf( 'Nonlin. ineq.   :' ) ); disp( lambda.ineqnonlin' );
    disp( sprintf( 'Nonlin. eq.     :' ) ); disp( lambda.eqnonlin' );
  end;

  return;

%------------------------------------------------------------
function [f, df] = exerf(x)
  % Calculate the objective function value f and
  % its gradient df

  f  = -x(1) + 2*x(1)^2 - 2*x(2) + x(2)^2 -x(1)*x(2);
  df = [-1+4*x(1)-x(2); -2+2*x(2)-x(1)];
  return;

%------------------------------------------------------------
function [C, Ceq, dC, dCeq] = exerg(x)
  % Calculate the values of the nonlinear inequality
  % ineq constraints C(x) <= 0, equality constraints Ceq(x) = 0
  % and the corresponding gradients.

   C(1)  = x(1)^2 - x(2); % x_1^2 - x_2 >= 0
   C(2)  = -2*x(1) + x(2);
   Ceq   = []; % no equality constraints
   dC(1) = [2*x(1); -1];
   dC(2) = [-2; 1];
   dCeq  = [];

   return;

Tack på förhand, 

Svara Avbryt
Close