# How to minimize a self-defined function?

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

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?

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

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.

`pos(x) + square_pos(pos(-x))`
{"ft_lang_label":"__label__en","ft_lang_prob":0.816535,"math_prob":0.96641266,"size":711,"snap":"2019-43-2019-47","text_gpt3_token_len":200,"char_repetition_ratio":0.10608204,"word_repetition_ratio":0.0,"special_character_ratio":0.26722926,"punctuation_ratio":0.14457831,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.997803,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-11-21T10:47:33Z\",\"WARC-Record-ID\":\"<urn:uuid:66293198-0924-4fcf-9bcc-38be6c72f723>\",\"Content-Length\":\"14789\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:dafdca5a-da9d-4690-be12-a2a1ae9b63de>\",\"WARC-Concurrent-To\":\"<urn:uuid:ebf83856-86ce-4ab6-8e02-a32202963db9>\",\"WARC-IP-Address\":\"104.131.20.137\",\"WARC-Target-URI\":\"http://ask.cvxr.com/t/how-to-minimize-a-self-defined-function/6501\",\"WARC-Payload-Digest\":\"sha1:WWMG6R7S42Q6FCDE5XTPWCR3V4CRQZUZ\",\"WARC-Block-Digest\":\"sha1:BMSZCMOFRVAYT4ROT2FLWYZQMAQOTQPR\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-47/CC-MAIN-2019-47_segments_1573496670770.21_warc_CC-MAIN-20191121101711-20191121125711-00124.warc.gz\"}"}