Search code examples
matlaboptimizationconstraintsminimization

Constrained minimization in MATLAB


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


Solution

  • 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.