How to minimize a self-defined function?

#1

I want to minimize a funcition that is defined by myself, how should I do?

(Mark L. Stone) #2

You must follow all of CVX’s DCP rules. Please read http://cvxr.com/cvx/doc/advanced.html#adding-new-functions-to-the-atom-library .

#3

thank you very much!
But why can’t the following example work?
cvx_begin
variable x;
minimize(fun(x));
cvx_end

function [z] = fun(x)
z = x^2;
end

doesn’t it follow the so called DCP rules?

(Mark L. Stone) #4

Place fun in fun.m in your MATLAB path. Then it should work.

#5

oh, yes, thank you for your kind help.
And could you please just give some hint on how to deal with a function like this:
function [z]=fun(x)
if(x>=0)
z=x;
else
z=x^2;
end
end

Thanks.

(Mark L. Stone) #6

pos(x) + square_pos(pos(-x))