# Interpolation error

cvx_begin
variable x
expression y
y=interp1(a,b,x); %a and b are known
minimize(…)
subject to
1<=x<=10;
cvx_end

I want to know why it can not execute interpolation.

Misuse of interp1 (line 87)
Invalid input parameters

`interp1` is not listed at http://cvxr.com/cvx/doc/funcref.html#built-in-functions , therefore it is not supported by CVX. You will have to implement your own functionality.

Thank you,Mark.
I have another question.

cvx_begin
variable x(n)
expression y(n)
y=(x);
%y is the function of x, and there is a constraint about y which is y(find(y<=-30))=-30.
minimize(…)
subject to

cvx_end

So I want to know how to add the constraint of y(find(y<=-30))=-30.
Thank you.

Can you do
`y = max(f(x),30)`
Presuming `f(x)` is a convex expression, then `y` will also be a convex expression.

Thank you,Mark.
the expression of y is an affine function.

And can I use the expression of y=max(y,-30) because that is y(find(y<=-30)=-30?
(y is a n-dimension vector)

Yes. I didn’t notice the minus sign.

``````cvx_begin
variable x;%x>0
expression y;
y=0.9483/x*exp(-14.3841/x);
PP=polyfit(x,y,2);
minimize(...)
subject to
...
cvx_end
``````

And I want to execute ‘polyfit’ to produce a quadratic function,but I do not know how to express ‘polyfit’.
Thank you.

I am not clear on what you want to do, because you do not show the objective function or constraints… You seem to have “too many” things declared as variable and expression. Until you show how the results of the quadratic fit are used in your objective function and constraints, I don;t know what you need to do.

You can use CVX to solve (possibly constrained) least squares problems, so long as they are convex and follow CVX’s DCP rules.

Are trying to fit a quadratic to `0.9483/x*exp(-14.3841/x)` ? If so, choose an interval, and choose data points x within it, then evaluate `0.9483/x*exp(-14.3841/x);` at those points and solve a linear least squares problem. or use polyfit. You don’t need CVX to do any of that. Or you can use CVX to solve a least squares problem (fitted coefficients are the CVX variables), but I don’t know how that feeds into your current CVX problem in which objective and constraints are not shown.

Hi,Mark.

``````xx=-1.15:0.001:0.6;
cvx_solver sedumi
cvx_begin
variable nb;
variable dE(N);%N is known
expression yy(length(xx));
expression Jsunhao(N);
yy=0.9483/nb*abs(xx).*exp(-14.3841/nb*xx);
PP=polyfit(xx(1151:1751),yy(1151:1751),1);
PPP=polyfit(xx(1:1151),yy(1:1151),2);
Jsunhao=max(PPP(1)*(dE).^2+PPP(2)*dE,0)+max(PP(1)*dE,0);
minimize(sum(Jsunhao))
subject to
-1.2<=dE<=0.6;
100<=nb<=200;
cvx_end
``````

I want to convert the function called’ `yy=0.9483/nb*abs(xx).*exp(-14.3841/nb*xx)` to a quadratic function and a linear function for different values of nb(a value of nb corresponding to a fit function).

But I do not know how to express because nb is a variable.
Or could you please tell me other ways to deal with it.

I don’t completley understand what you are trying to do, nor do I know whether it can be done in CVX.

My suggestion is that you not use CVX for this problem. Unless you have a good reason, I suggest not fitting a piecewise quadratic and linear approximation. Rather, use a non-convex nonlinear solver applied to your original unfitted function. There are global optimization solvers available, which may or may not succeed depending on the dimension and difficulty of the problem.

Hi,Mark.

``````xx=-1.15:0.001:0.6;
cvx_solver sedumi
cvx_begin
variable nb;
variable dE(N);%N is known
expression yy(length(xx));
expression Jsunhao(N);
yy=0.9483*abs(xx).*exp(14.3841*inv_pos(nb)*xx);%inv_pos(nb)=1/nb(nb>40)
PP=polyfit(xx(1151:1751),yy(1151:1751),2);
PPP=polyfit(xx(1:1151),yy(1:1151),2);
Jsunhao=max(PPP(1)*(dE).^2+PPP(2)*dE,0)+max(PP(1)*(dE).^2+PP(2)*dE,0);
minimize(sum(Jsunhao))
subject to
-1.2<=dE<=0.6;
nb>=40;
cvx_end
``````

And I just want to convert an expression that has a variable(such as expression yy(length(xx)) and it is convex) to a quadratic function.
But cvx can not accept polyfit(I think so).And I have to deal with it in cvx.
So could you tell me how I can do about expression yy(length(xx)).
Thank you.

I still recommend what I wrote in my previous reply.

Nevertheless don’t know whether this would do what you need or whether it will work. Use polyfit separately on the two regions of xx to fit the quadratic and linear approximations, but do so outside CVX, i.e., just use polyfit as intended in MATLAB. Then take the two fitted pieces, and implement them in CVX via use of Big M modeling to define the piecewise function (if xx <=… then fitted_quadratic. else fitted_linear). https://www.fico.com/en/resource-download-file/3217 will show you how to do that… I don;t understand what you are doing with `Jsunhao`, so I don’t know if it will work out and even be accepted by CVX, but it looks to me like it won’t work.

In a word, I can not use polyfit in cvx directly to convert a expression which contains variables to a quadratic function(such as yy=0.9483*abs(xx).exp(14.3841inv_pos(nb)*xx).
Is it?

A CVX variable or expression cannot be an argument of polyfit.

As far as the approach of using polyfit outside of CVX, as mentioned in my immediately preceding post, the reason I am not sure it would not work, is because if your piecewise function, ffitted outside CVX, were implemented in CVX using `xx` as a CVX variable, and then multiplied by terms in another CVX variable, `dE`, that would be non-convex and not allowed in CVX. But if your final function to be minimized is a function of only a single CVX variable, then perhaps it could be done. As I have said, I am confused what you are doing with `Jsunhao`.

If I do not polyfit expression yy to a quadratic function in advance in cvx,the objective function Jsunhao(N) will contain two variables(dE(N) and nb) which is the form of ‘yy=0.9483*abs(dE).exp(14.3841inv_pos(nb)*dE)’.

And as I wrote, multiplying CVX expressions is non-convex and is not allowed. So that would not work.

I will end this thread by repeating my advice to abandon CVX for this problem, and use a nonlinear non-convex optimizer, in which case you are probably better off letting the optimizer handle the original function, than fitting a piecewise quadratic, linear function. CVX has its place, but not for this problem.

Hi,Mark.

cvx_begin
variable X(N+1); %N is known
minimize(sum((X(2:N+1)-X(1:N)).^2));
subject to

cvx_end

The above is just one example.And I want to know whether sum((X(2:N+1)-X(1:N)).^2) is convex or not.
Thank you.

That is a convex quadratic and is accepted by CVX as written.

Thank you very much.