This question concerns using CVX within a custom MatLab function (i.e. an .m file). Specifically, I’m wondering if there’s a way to pass optimization constraints as an argument of the custom function.

For simplicity, consider something such as $$\min_{\beta} | X\beta|2^2 - 2\langle y, X\beta\rangle \text{ subject to } |
\beta |{\infty} \leq 1, \beta \succeq 0$$ which would be coded as perhaps

function [ b ] = HS(X, y, C)
d = min(size(X));
cvx_begin
variable b(d)
minimize( (X*b)'*(X*b) - 2*(X*b)'*y )
subject to
C
cvx_end
end

where C = [ norm(b, inf) <= 1 ; b >= 0]. I’ve tried creating C using MatLab’s symbolic toolbox, but this didn’t work within the function itself. CVX either ignored the constraint (which happens if when the constraint is in the line below subject to) or returned an error message along the lines of function of too many variables when the constraint was expressed as subject to C.

Generally, I think its important for CVX to be able to be easily embedded into functions such as this so that complicated functions can be packaged by practitioners and specialists and passed onto decision-makers and other non-technical managers.

CVX is not compatible in any way with Matlab’s symbolic toolbox. But why didn’t this work?

function [ b ] = HS(X, y, C)
d = min(size(X));
cvx_begin
variable b(d)
minimize( (X*b)'*(X*b) - 2*(X*b)'*y )
subject to
norm(b,inf) <= 1
b >= 0
cvx_end
end

I mean, that’s how you would build the model outside of a function, I don’t see why it has to be different inside one.

Perhaps I was not clear in my initial question. What I’m asking is if it’s possible for arbitrary constraints (that abide by the CVX ruleset), specified as an argument of the function, can be passed to the CVX program embedded in the function. The fact that I chose the two constraints I chose was just simply for example. I want to be able to pass any constraint through C so that an individual not familiar with CVX can simply use a function pre-built by a practitioner.

Please let me know if you’d like me to elaborate more.

And to be clear, the purpose of this thread is to inquire as to which way one would be able to specify these constraints in a function call so that CVX acknowledges them and implements them successfully.

Perhaps you can do what you want using MATLAB strings. So pass a string into your function and/or modify/build up the string as needed (for instance with numerical values), using strcat, sprintf, or whatever. . Then eval the string inside CVX. Here is a simple example showing that it works.

k = 4.14587;
joeblow = sprintf(‘x >= %g’,k);
cvx_begin
variable x
minimize(x)
eval(joeblow)
cvx_end

OK, I understand what you’re saying now; and unfortunately that is not going to be possible with CVX, unless you use the string approach that Mark is advocating. It is not likely something that we are going to be able to add for architectural reasons.