问题
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):
f1.m
function [y1, y2, y3]=f1(x1, x2, a) ... end
f2.m
function w1=f2(x1, x2, y2, y3, b) ... end
Problem that I want to code:
min
y1
w.r.tx1
,x2
such thaty1<=w1
回答1:
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.
来源:https://stackoverflow.com/questions/44464008/constrained-minimization-in-matlab