How do I optimize a variable and its derivative on CVX?

I’m trying to do some trajectory optimization for a UAV, and it’s been successful when assuming that velocity would be constant. However now, I’m trying to set velocity as a variable, and hence make the problem more real-life like by also maximizing and minimizing its acceleration.

Here’s just a part of the code:

for T = [100 101 105 120 160];
cvx_begin quiet
variable x(T) complex
variable V(T) complex
variable a(T) complex
minimize(norm(x-xB))
V = (x(T)-x(T-1))/(T-(T-1)) %not really a condition
norm(x(1) - x0) <= V;

        for t = 2:T
            norm(x(t)-x(t-1)) <= V;
            norm(x(t) - xH) <= V*(T-t);
            a = (V(t)-V(t-1))/(t-(t-1));
cvx_end 

Later, I want to maximize and minimize the acceleration at specific position ranges, however, CVX says this (below) once I introduce “a” as the derivative of V
(Index exceeds array bounds.)

I would really appreciate any help :slight_smile:

1 Like

V is a scalar, which is not what you want. I think you need to move the calculation of V into the (a) for loop. And then you will need to chop off one more value of t (start at = 3) for the calculation of a in order to keep the double differencing within bounds.

a should be declared as an expression, not as a variable. Read http://cvxr.com/cvx/doc/basics.html#assignment-and-expression-holders to see how to properly handle an expression vector.

In general, you need to make sure your code runs in MATLAB without any dimension, index, non-conformal, etc. errors presuming that all CVX variables are replaced with the same dimensioned numerically populated variables. That is a matter of MATLAB syntax, not really CVX. Once that executed without error, simply insert the variables declarations, and then the indexing, dimensions, etc. should be correct in CVX.

Do you really want the variables to be complex? I suspect not. As it is now, your inequalities will not be accepted by CVX because the right-hand sides are complex.

1 Like

Thank you for your reply!

I seem to still have a persistent issue with my code. Would you be free to talk briefly maybe over Skype where you could see my code?

Thanks a lot in advance! :slight_smile:

All communication for this forum is handled by forum posts.

I understand.
How do I declare “a” as an expression not a variable? I moved V into the for loop, yet I’m still getting the same error

See the link I provided previously http://cvxr.com/cvx/doc/basics.html#assignment-and-expression-holders

You will probably benefit from a careful re-reading of the entire CVX Users’ Guide now that you have some experience.

Hello,
I’m still kinda stuck on my CVX problem. What I wanna do is to optimize the trajectory given two main constraints (1 ensure spacing between each two points with respect to time and velocity, and 2nd ensure that the UAV has enough time to come back home). All the aforementioned, I was able to do, however, now that I wanna maximize/minimize acceleration at certain parts of the trajectory, hence from there I get the new value of velocity and based on which, I again do the same trajectory optimization problem. I’ve tried playing around with the code, but I keep getting Disciplined Convex Programming Error. Also, I’ve tried making all my variables non complex and just vectors of 2 dimensions, but it’s not working. Like you said before, I defined acceleration as an expression but still I seem to be getting some sort of CVX error.

Here’s my code with some comments to know what Im doing exactly

x0 = 100 + 2001j;
xB = 0;
xH = 100 -200
1j;
p = 1;
for T = 111
cvx_begin quiet
variable x(T) complex
variable V(T) complex
expression a(T) complex
minimize(norm(x-xB)) %main cvx optimization
%Here I wanna define what V is before I put it in the constraints
for t = 1:T
if ((xB-x0)/2) - x(t) <= 0
cvx_begin quiet
variable x(T) complex
variable V(T) complex
expression a(T) complex %I guess expressions can’t be complex
V = (x(T)-x(T-1))/(T-(T-1));
a = (V(t)-V(t-1))/(t-(t-1));
maximize(a) %a>0
subject to
(x(T)-x(T-1))/(T-(T-1)) <= 5; %e.g.V<5
cvx_end
V = int(a,t); %from the maximization problem get me the new value for V
end
end
%Now I have V, hence I can apply my constraints to the main trajectory optimization problem
subject to
norm(x(1) - x0) <= V;
for t = 2:T
norm(x(t)-x(t-1)) <= V; %ensures correct spacing
norm(x(t) - xH) <= V*(T-t); %ensures enough time to get back home
%V = (x(T)-x(T-1))/(T-(T-1));
%a = (V(t)-V(t-1))/(t-(t-1));
end

cvx_end %End the main CVX optimization problem
Path(p).X = x;
p = p + 1;
plot(x,'.r','MarkerSize',16)  
axis equal 
axis([0 200 -200 200])
a = zeros(T);

end

I would really appreciate any kind of help with the code itself :slight_smile:

Could any one please give me any tips?

I know I have two mistakes, the if statement
Invalid Constraint: {convex} > {constant}

And the expression a(T)

Why are you declaring anything complex?

I don’t even know what your program is. You have cvx_begin, cvx_begin <code? cvx_end cvx_end . That doesn’t make any sense.

Where is the {convex} > {constant} constraint? Are you sure the constrraint triggering the error is really convex? if so, how are you sure?

Don;t use quiet until everything is running correctly.

Sorry for my late reply. I work on this beside other work.

I put them as scalars from the start as 2D vectors didnt work.
I have a main cvx problem which maximizes the distance between points, its main constraints require another optimization problem (maximizing the velocity) hence I have another cvx problem embedded in a big one. Later when I get the v from the smaller cvx problem, I wanna use this v to disperse my points. What I’m asking about is why cvx isn’t accepting my if statement? and please dont tell me go read the manual as I’ve read it but it isn’t really answering this very question. Second why is CVX not accepting a derivative?
It gives me errors whenever I write V(t)-V(t-1)/(t-t-1).

Here’s the code (now modified) and a bit simpler again:

x0 = 100 + 2001j;
%x0 = [100,200];
xB = 0;
xH = 100 -200
1j;
%xH = [100, -200];

p = 1;
for T = 111
cvx_begin quiet
variable x(T) complex
variable V(T) complex
%expression a(T)
minimize(norm(x-xB)) %main cvx optimization
%Here I wanna define what V is before I put it in the constraints
for t = 1:T
if (xB-x0)/2 <= x(t)
cvx_begin quiet
variable x(T) complex
variable V(T) complex
V = (x(T)-x(T-1))/(T-(T-1));
maximize(V)
subject to
V <= 5
(V(t)-V(t-1))/(t-(t-1)) <= 0.3125 %or (5-0)/(T-t)/6
cvx_end
end
end
%Now I have V, hence I can apply my constraints
%to the main trajectory optimization problem
subject to
norm(x(1) - x0) <= V;
for t = 2:T
norm(x(t)-x(t-1)) <= V; %ensures correct spacing
norm(x(t) - xH) <= V*(T-t); %ensures enough time to get back home
end

cvx_end %End the main CVX optimization problem
Path(p).X = x;
p = p + 1;
plot(x,'.r','MarkerSize',16)  
axis equal 
axis([0 200 -200 200])
a = zeros(T);

end

Please help me if you can.

Here’s a code as a screenshot as indentation gets messed up when I copy and paste it here.

You are trying to do things which are not described in the CVX Users’ Guide, and not allowed.

You can not embed a CVX program within another, except in the very limited way described in http://cvxr.com/cvx/doc/advanced.html#new-functions-via-partially-specified-problems .

CVX doesn’t know anything about derivatives. If you want to use derivatives, you will have to explicitly introduce whatever variables or expressions which are needed, and they must comply with CVXs DCP rules.

The only if statements allowed in a CVX program must be conditional only on MATLAB variables having valid numerical values, not on CVX variables or expressions.

I still don’t know what you are trying to do, but perhaps your problem is not convex and can not be handled by CVX. I don’t know.