Reputation: 2299
I want to use Matlab to solve a constrained minimisation problem:
min wrto (gamma,delta) the function f(gamma,delta):=[gamma,delta]
such that
A(gamma,delta)-B(gamma,delta)<=0
Here a working example:
%main file
gamma0=0.4;
delta0=0.5;
x=fmincon(@(x) [x(1) x(2)], [gamma0 delta0], ...
[],[],[],[],[],[], ...
@(x) mycon(x(1),x(2)));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%function for constraints
function [C,Ceq]=mycon(gamma,delta)
A=randn(1)+gamma+delta;
B=randn(1)+gamma+delta;
C=A-B;
Ceq=[];
end
However, I get as error
Error using fmincon (line 607)
User supplied objective function must return a scalar value.
Could you help me to understand how to modify the code so that fmincon
accepts also the desired non-scalar output function?
Upvotes: 1
Views: 416
Reputation: 11072
Calculating the minimum of a non scalar objective function is mathematically not defined. As an alternative, you can minimise one of the following options:
x(1)
or x(2)
aloneOther considerations
Variables that are not part of the objective function may change in order to minimise the other variables further if they are part of the constraint function.
Using a random generator such as randn
inside the objective/constraint function is (often) a bad idea, because fmincon
calls them multiple times for different input arguments resulting in a randomly changing objective/constraint function. You should generate the random variables before the call to fmincon
as pass these variables as arguments to your functions.
Upvotes: 1