# Interpolation error

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.

Hi,Mark.

There is a piecewise function:

``````y=6.961e-05*x+4.433e-04;%0<=x<=63.505
y=1.657e-06*x^2-1.322e-04*x+0.0065761;%63.505<=x<=87
``````

(This piece function is successive at 63.505 beceuse I approximate some coefficients to express easily. )
And I do not know how to express it in cvx.
and it can not be expressed by the form of max(6.961e-05x+4.433e-04，y0)+max(1.657e-06x^2-1.322e-04*x+0.0065761,y0)-y0,because the quadratic function have some values that exceed y0 when x is between 0 and 63.505.(y0 is the value of the piece function in 63.505.)
Thank you.

The quadratic piece is concave, so I assume you will maximize the piecewise function or use it in a `>= constant` inequality. Therefore, model the hypograph, `y >= t` of the piecewise function, and then use `t` in place of `y` in your program. In order to do this, you can implement

``````if x <= 63.505t
then
t <=1st formula for y
else
t  <= 2nd formula for y
``````

Implement this logic construct by introducing binary with big (enough) M, using something similar to the Expressing Logical Constraints slides of https://www.cs.upc.edu/~erodri/webpage/cps/theory/lp/milp/slides.pdf .

I leave you to make sure this is correct, and to work out the details.

Note:The small magnitude coefficients in your formulation might cause numerical difficulties, and are best avoided by re-scaling or some other means.

The quadratic piece is convex,because 1.657e-06>0.And the piecewise function is convex.

Sorry, I misread that. Just change
`t <=`
to
`t >=`

By the way, if you have a better solution, you are welcome to use it.

Thank you,Mark.
In fact,the piecewise function is obtained by many data such as (xi,yi),and I just want to fit these data to a function.
In addition,I found a better way that is from

So I have solved this question.
Thank you very much.

Hi,Mark.
z=0.005*abs(y) exp(14.4 abs(y)/x) and x>100,-3<=y<=3, so that cvx can accept it?
Thank you very much.

This appears to be convex, but I don’t see how ti implement it in CVX. Your last hope probably is that one of the Mosek guys can figure out how to do it, and if not, it’s a candidate for @Erling’s challenge.

Note that the related function, `y*exp(y)` is not convex for `y < -2`, and therefore is not convex over its “natural” domain. I do not know any way of implementing that in CVX even for `y` restricted to be `>= -2` (or 0).

The function `abs(y)*exp(abs(y)/x)`, although apparently convex for all `y` and `x`, would appear to be no easier, and perhaps more difficult, to implement in CVX than `y*exp(y)` for `y >= -2`.

If y\geq 0 then a model for t\geq y\exp(y) is essentially in https://docs.mosek.com/modeling-cookbook/expo.html#lambert-w-function, equation (5.10), up to renaming variables. Throwing in x we get the following model for t\geq y\exp(y/x), x,y\geq 0:

t\geq y\exp(u/y)
ux\geq y^2

The first one is an exponential cone the other is rotated quadratic.

Now in general it should be OK to replace y with a positive z with z\geq |y| but frankly I haven’t checked all the correct monotonicity details. Either it works or I don’t see another way.

1 Like

Son of a bitch. i don’t know how I missed that. And I just posted a comment on the Lambert W function a couple of weeks ago, linking to a post on this forum by @Erling. .

Suggestion, include Lambert W in the next version of Conic Modeling Cheatsheet.

In the function of z=0.005*abs(y) exp(14.4 abs(y)/x),x,y and z are N-dimensional vectors(such as N=100). And I want to know how to use exponential cone when variables are N-dimensional vectors.
Thank you.

Id x(i), y(i), z(i) are constrained in an exponential cone for each value of i, use a for loop

``````for i=1:N
{x(i),y(i),z(i)} == exponential(1)
end
``````

Thank you,Mark.
Using a for loop may lead to increase the running time of the program,but there seems to be no better way.