Hello, I have this problem written in python .
I want to convert it to matlab just the part for state space and the initial and final conditions
N = 30
A = [[-1. 0.4 0.8]
[ 1. 0. 0. ]
[ 0. 1. 0. ]]
b = [[ 1. ]
[ 0. ]
[ 0.3]]
Xdes = [ 7. 2. -6.]

Thanks for your help, but I am wondering how should I put the state space as a constraint itself…shall I put it inisde a loop and the whole CVX inside another big loop or what?

There should not be a for loop around (outside) cvx_begin … cvx_end, because you only want to solve one optimization problem. All CVX statements about the optimization problem you are solving needs to be inside cvx_begin … cvx_end Only use a for loop around cvx_begin … cvx_end if you are solving more than one optimization problem (for instance, with different input data), or where the results of one optimization problem become input data for the next optimization problem.

Your last for loop should have for k = 1:N-1. As it is now, you not only go past the end of the array, you are setting up a conflicting equality constraint for X(N+1), given you also have a constraint just on X(N+1). The image you show does seem to have that conflict; so I will leave that to you to sort out; I suspect that is carelessness (or typo) on that part of the paper or book authors.

If you look at the help for max, you will see that you can vectorze the calculation and avoid the for loop, which saves time in CVX’s processing. But using a for loop is not “wrong”.

I think you are right after I went over it again regarding the last piece of code I sent . But, after viewing ‘X’ in Matlab, I realized that the 30th column of X is set to the desired value as I set it in X(:,N)==Xdes’; and the 31st column is [0 0 0]’.

It looks like CVX does not overwrite the constraints in the same way of overwriting a variable in Matlab.

In other words, although what you mentioned is totally true, it seems that CVX, when the 30th column of constraints was called, didn’t change the already-set value of the (X(:,N)==Xdes’; ) somehow inside the for-loop.

here is a print of the columns of X ( in red : the 30th column)