Piecewise function

(zhangQK) #1

There is a piecewise function,
f(x)=x^2(x>=0) and f(x)=-x (x<0) and I want to solve minimize(f(x)).
I want to know I can let this piecewise function become the objective function or not. and could you tell me how to express it.
Thank you.

(Mark L. Stone) #2

minimize(square_pos(max(x,0)) - min(x,0))

(zhangQK) #3

Thank you,Mark.
But in fact,I have another piecewise function,
f(x)=x^2+3 (0<=x<=2^0.5), f(x)=2*x^2+1 (2^0.5<=x<=3), f(x)=3*x^2-8 (x>=3).
Could you tell me how to express it to let it become the objective function in cvx.

Because f(x) is an even function, it is feasible when x<=0.

(Mark L. Stone) #4

I’ve done one, now it’s your turn. Perhaps there is nicer way, but if not, you should b e able to model this using Big M logic with binary variables.; It may get a bit messy, so I leave you to deal with that. You should be able to use the building blocks in https://www.msi-jp.com/xpress/learning/square/10-mipformref.pdf . You won’t be able to just specify SOS however, because CVX does not support that; so you will have to handle that by brute force.