MATLAB: How to impose that the solution takes zeros values in some elements using fmincon

fmincon optimization matlabMATLAB

Hello !
My initial value is of size n and my objective function is :
objective=@(f) 0
Let us assume that :
m<=n.
I would like to impose that the solution f takes m non null values and the rest are 0.
I tried out the following non-linear constraint :
size(find(f~=0),1)-m
But I get the following message :
Converged to an infeasible point.
fmincon stopped because the size of the current step is less than
the default value of the step size tolerance but constraints are not
satisfied to within the default value of the constraint tolerance.
<stopping criteria details>

Best Answer

It looks like you simply need f(1:m) >=0 in order for the log expressions to work. Why not just put lower bounds on them lb(1:m)=0?