Multi case function

Hi
I have the following function to be called from another function. But it gives error, it seems cvx doesnt read such function. It is called as a part of objective in my problem.

function f=myfun(z)
ep=1e-3;
if z>= ep
f=z;
elseif -ep<=z<=ep
f=(z^2)/(4*ep)+z/2+ep/4;
else
f=0;
end
end

If the function is not called with argument z being a CVX variable or expression, this function has nothing to do with CVX, even if called in the middle of a CVX program.

If the function is called with argument z being a CVX variable or expression, it would violates CVX’s rules and generate an error message by being used as the condition in an if statement.

To implement what you want, you may have to use Big M logic modeling.using binary variables. https://or.stackexchange.com/search?q=logic+constraints and https://yalmip.github.io/tutorial/logicprogramming .

Thanks for your reply Mark, I know it can be reformulated using new variables, but the goal was not to that as it increase number of variables in model. This is kind of smoothing function used in CVaR optimization and when the number of scenarios are larrge so reformulating is a drawback.

My goal is for someone to give me a billion dollars tomorrow. When I don’t have a lot of money, it is a drawback.

You can’t always get what you want.