I’m trying to minimize afunction in matlab like this:
function [c, ceq] = const_slot( x )
c = [];
% Nonlinear equality constraints
ceq = [sum(x)-1];
end
[x,fval] = fmincon(@func_slot, x0,[],[],[],[],lb,ub,@const_slot,options)
But, I need to value fval that was within the specified value, or the positive. How can I do that?
As I understand your question you want to put constraints on your function
@func_slot(which I assume is non-linear).In the Matlab help for fmincon we find:
Non-linear constraints can be set using the
nonlconparameter (in the question you use@const_slot). These constraints should be defined as:So for example, when you want your function
@func_slotto be greater than zero, you can define the inequality constraintcin@const_slotas the negative of your function.Edit
If I understand you correctly, you need the function value to be greater than zero but less than a specified limit. In that case you could try this.