# Using Functions within CVX

Hi,
I am new to CVX and I am using it solve the problem:

minimize((x1 - x_next)'P(x1 - x_next) + (y - d)'R(y - d))
subject to
x_next >= 0;

where ‘d’ is a function of ‘x_next’. I need to know how can I do this with CVX i.e. how can I perform minimization where every time the value of ‘x_next’ is passed to a function that computes ‘d’. I was thinking of using a function handle in place of ‘d’ but one of the previous posts suggests this is not possible with CVX. Any ideas, suggestions or way around this??

I doubt you can do that. Since how should CVX be able to analyze the convexity when d() is black box function. In any case I do not think any optimizer connected to CVX can solve such problems.

So you have to find another tool or state your problem in another way.

First of all: is your problem convex? I suspect that it is not, given your use of a presumably continuous nonlinear function in a quadratic form. If your model is not convex, CVX cannot solve it.

If `d` is affine, then your model is indeed convex, assuming both P and R are PSD. But since CVX doesn’t support opaque linear operators, only matrices, you might as well be expressing `d(x_next)` as `A_d * x_next + b_d` for some appropriate choice of `A_d` and `b_d`.

It is certainly possible to construct new functions in CVX. However, the expressions used to calculate those functions must themselves satisfy the disciplined convex programming ruleset, and they in turn can only draw from the function set listed here in the users guide.

This section of the users guide describes two ways to add new functions that are compatible with CVX. However, this truly is an advanced topic, because of the restrictions I listed in the previous paragraph. In my experience very few users even attempt to do this, and we are unable to support this, generally.

Thanks Michael and Erling for your answers. I guess I just have to use some sort of approximation for my function.