If you are here because CVX is rejecting your model, or if you do not know if CVX will accept your model, please read this page before posting a question. Many of the questions posed in this forum are addressed fully by the general answers given here.
I know CVX looks easy to use—we designed it that way—but when used improperly, it’s surprisingly frustrating. So it is extremely important that users understand CVX’s inherent requirements and limitations to avoid wasting time.
The first thing we must ask you is this:
Is your model convex?
No, my model is not convex.
You should not be trying to use CVX. CVX is not a general purpose optimization framework. It is designed specifically for certain kinds of convex optimization problems, as well as certain mixed-integer convex models and geometric programs. If you do not already know that your problem fits into one of CVX’s supported problem classes, then CVX is not the correct tool to use.
I do not know if my model is convex.
Then the answer is the same as above: You should not be using CVX. Save yourself considerable frustration and stop now. CVX is a framework for disciplined convex programming. What that means is that you have to know in advance that your model is convex, and you have to be able to prove that fact by building your model according to a specific set of rules.
CVX is not particularly useful as a convexity verification system. That is, if you do not know that your model is convex, don’t just enter it into CVX “to see what happens”. You really do need to do the advance work yourself.
Yes, it is convex!
Are you sure? Have you rigorously proven that it is convex? Unfortunately, it is very common for people to ask why CVX cannot solve their convex model, only to discover that their model is not actually convex. For example, ratios f(x)/g(x) of affine or convex/concave expressions are almost never convex, and yet countless people have come to this forum and others claiming that they are.
Do not trust your intuition alone on convexity. Prove it. In fact, in order to use CVX, for reasons we explain below, CVX only accepts models that can be proven convex using only the rules in CVX’s DCP ruleset. If you have not yet done that, go do that now and come back to this FAQ.
Yes, I am sure my model is convex!
Wonderful. Well done. (I know there’s a “but” coming, though…) What error message did you get?
Matlab returned this error: Function “so-and-so” undefined for objects of type “cvx”.
This is because you’ve tried to use a function that is not on CVX’s list of supported functions. If you cannot express your model in terms of these functions, CVX cannot handle it. It is not straightforward to add new functions to CVX, due to the unique way that it operates; more on this below.
Matlab gave me a “Disciplined convex programming error”.
It is definitely possible to construct convex expressions that violate the DCP ruleset. For instance, consider
sqrt(sum(square(x)). This is just a representation of the convex Euclidean norm, but CVX rejects this expression as a violation of the rules. Fortunately, you can just use
norm(x) in that case instead.
Similarly, if you want CVX to solve your model, you must find a way to express it in a manner that complies with the DCP ruleset.
But I can’t use the DCP rules alone to prove it; I had to use other principles, like a derivative test or a secant test, as well.
Unfortunately, if you cannot rewrite your model to avoid a DCP error, that means that CVX cannot solve your model. We do not claim that CVX can solve every convex model, and we do not consider this a bug.
Can’t I just tell CVX somehow that my model is convex?
No, you can’t. To understand why, you must understand that the DCP ruleset is not for your benefit. Yes, we want you to be able to prove that your model is convex, and the rules can certainly help. But in fact, the rules are needed by CVX. The real purpose of the rules is that CVX relies upon them to convert problems to a solvable form.
Each individual rule corresponds to a specific step in the process of converting your model into the so-called conic form demanded by SeDuMi, SDPT3, etc. In mathematical terms, each rule corresponds to a specific equivalency- and convexity-preserving transformation. This conversion process is by no means straightforward, as you know if you have tried to do it by hand. We’ve accumulated a library of these transformations in CVX in order to automate that process for you—and each of those steps is tied to one of the DCP rules.
So whether you realize it or not, by ensuring sure that your model is expressed according to the rules, you are giving CVX not just a proof of convexity, but also a recipe for solving your problem. If your model violates the rules, then CVX is unable to construct the necessary transformation. If you want a more technical explanation, please read the papers listed here.
Can I add my convex/concave function to the CVX function library?
Possibly, but it’s not straightforward. We have documented the approach in the advanced section of the users’ guide, Keep in mind that this involves building up a description of the function using the existing set of DCP rules. Unless you are an advanced CVX user, I would not recommend trying this.
Unfortunately, some of CVX’s limitations are dictated by the specific solvers that it targets (SeDuMi, SDPT3, Gurobi, MOSEK, etc.). Even if you were try and bypass CVX completely, you would still find that many problems cannot be handled by these solvers. However, if you have studied up on how CVX represents functions, and you think that there’s a good chance that your function should be compatible with these solvers, go ahead and post it here on the forum!
But my function is smooth. Other modeling systems just let me provide code to compute the function and its derivatives. Why can’t I do that with CVX?
This makes perfect sense for traditional smooth NLP solvers. But in fact, CVX does not consider function derivatives at all. The solvers do not either: they are conic solvers which rely not on functions but on convex cones to describe constraints. CVX uses an entirely different way of describing functions to these conic solvers than the traditional value/derivative-based approach found in a traditional modeling system like AMPL or GAMS. For more information, you’ll need to read one of these papers.
OK, OK, I give! I accept that CVX cannot actually solve my model. But what can I do?
Well, if your model is smooth, a traditional NLP solver, coupled with a traditional modeling framework like AMPL or GAMS, may be useful. YALMIP is a modeling framework in MATLAB that supports both convex and non-convex problems, and it connects to a wider variety of solvers than CVX. So that might be worth a try if you want to stay in MATLAB. And of course, MATLAB has an optimization toolbox of its own. Unfortunately, I do not use any of these tools regularly, so I cannot offer more specific advice.