# Using the function fmincon

32 views (last 30 days)
Auryn_ on 2 Sep 2019
Commented: Auryn_ on 2 Sep 2019
Hi,
I want to optimise a function that I called FUNCTION where
function [Err]=FUNCTION(coef)
data1=...
data2=...
[x1]=Function1(coef, data1, data2); %Function1 is an additional function
% % return the error
Err=1-x1;
end
I have this function FUNCTION and try to minimize Err using fmincon:
Aeq=[1 1 1 1 1 1 1 1 1 1;1 -1 1 -1 1 -1 1 -1 1 -1];
beq=[0;1];
[x] = fmincon (FUNCTION(coef),coef,[],[],Aeq,beq);
I don't know why is not working and gives me an error :
"Error using optimfcnchk (line 101)
FUN must be a function, a valid character vector
expression, or an inline function object.
Error in fmincon (line 409)

Torsten on 2 Sep 2019
[x] = fmincon (@FUNCTION,coef,[],[],Aeq,beq);

Auryn_ on 2 Sep 2019
Thanks!
Walter Roberson on 2 Sep 2019
[x] = fmincon (@(coef)FUNCTION(coef),coef,[],[],Aeq,beq);
Auryn_ on 2 Sep 2019
Hi again,
could I specify to stop fmincon if the Err is less than a certain value?

The first argument needs to be a function handle, not a call to the function, although your function is FUNCTION so I'm not sure what FUN is, but you certainly shouldn't be passing the argument to it.
@FUNCTION
would seem like what you should be passing in.

Show 1 older comment
Auryn_ on 2 Sep 2019
Hi again,
could I specify to stop fmincon if the Err is less than a certain value?
Torsten on 2 Sep 2019
Use an output function.
An example to stop fmincon if a certain condition holds is given under
https://de.mathworks.com/help/optim/ug/output-functions.html
Auryn_ on 2 Sep 2019
Thanks!