Passing Matlab Functions as Objective & Constraints within CVX


Can anyone please post an example where the objective function and constraints are defined within respective matlab function (.m) files such that these functions are called within a “cvx_begin {Optimization Problem } cvx_end” structure posed in a separate matlab script ?


See my answer in Building Functions using CVX . Does that allow you to do what you want?

Hello Mark,

I looked into the link that you directed me to. I guess what I want is similar to the example code you gave. If I follow the same strategy I need to pass the objective/constraint function call with appropriate arguments as a string and use the “eval( )” within cvx context.

One question here: within the objective and constraint function files themselves do I need to be careful of the cvx ruleset ? I already have such files that works with Matlab’s custom optimizers such as “fmincon( )”. I want to know where I should make changes within these files to run the problems in CVX.

I believe the key matter to keep in mind is that when the string is eval’d within CVX, it should eval to valid CVX code, i.e., which follows the cvx ruleset. The string approach does not provide a magic “license” to let you get away with CVX code which would not be allowable if it were entered directly without use of string. So I think you just need to figure out what the valid CVX code will be, then get it into a string and eval it within CVX. Your CVX model and code will obviously be different than FMINCON.