-1

I want to solve a constrained minimization problem and I am asking for some help on how to structure the code.

I understand that fmincon is what I should use by playing with the argument @mycon but I am struggling in adapting it to my case. Any suggestion would be extremely appreciated.

These are my files (a and b are predefined parameters):

  1. f1.m

    function [y1, y2, y3]=f1(x1, x2, a)
    ...
    end
    
  2. f2.m

    function w1=f2(x1, x2, y2, y3, b)
    ...
    end
    

Problem that I want to code:

min y1 w.r.t x1, x2
such that y1<=w1

Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
TEX
  • 2,249
  • 20
  • 43

1 Answers1

2

You could use fmincon as follows:

x = fmincon(@(x) f1(x(1), x(2), a), [x1_start x2_start], [], [], [], [], [], [], @(x) mycon(x(1), x(2), a, b));
x1 = x(1)
x2 = x(2)

with mycon defined as:

% C <= 0 and Ceq == 0
function [C, Ceq] = mycon(x1, x2, a, b)
    [y1, y2, y3] = f1(x1, x2, a);
    C = y1 - f2(x1, x2, y2, y3, b);
    Ceq = [];
end

and x1_start and x2_start the starting values for x1 and x2 in the iterative solver used by Matlab.

Please take a look at the matlab documentation and examples for more information.

m7913d
  • 10,244
  • 7
  • 28
  • 56
  • Thank you! One question: how does Matlab know that, when writing `fmincon(@(x) f1(x(1), x(2), a),...)`, it has to minimize wrto `x(1), x(2)` only the first output `y1` of `f1`? – TEX Jun 20 '17 at 16:34
  • 1
    Because `fmincon` expects only one return argument. Matlab automatically discards extra return arguments if you do not need them, i.e. `y1 = f1(x1, x2, a)` will indeed store the first returned argument to `y1`. – m7913d Jun 20 '17 at 16:40