# Modelling the Convex Optimization problem

Hi, I am a new user to CVX. I am trying to model the particular objective function `0.5 * (u(t)^2 + u(t + delta)^2 + ... + u(t + N * delta)^2`. There are N variables.

I have written my code as

``````   function acceleration = control(x0,v0,brake, acc, N, delta, minDist)

cvx_begin
variable x(N);
variable v(N);
variable u(N);
whos
minimize(1/2 * u' * u);
subject to
brake <= u;
u <= acc;
x >= minDist;

x(1) == x0;
v(1) == v0;

for i = 1:N-1
x(i+1) == x(i) + delta * v(i)  + 0.5 * delta^2 * u(i);
v(i+1) == v(i) + delta * u(i);
end

cvx_end
end
``````

Upon running the code, I get `Error using norm First argument must be single or double. Error in computeControl>control (line 12) norm(brake <= u);` But, `whos` is giving the value as `double` for `brake`.

I removed `norm` and I get this
`sqlp stop: dual problem is suspected of being infeasible`

I couldn’t give the give the objective function as `minimize(0.5 * u.^2)` since I got that the objective function is not scalar error. Any help would be greatly appreciated. Thanks.

Where in your code is `norm(brake <= u)` ? Perhaps that is a typo in your post and is supposed to be `norm(brake) <= u` . But your code has `brake <= u` .

`u.^2 i`s an N by 1 vector. The objective function must evaluate to a real scalar. Perhaps you want `sum(u.^2)` ? And that can be written more simply as `u'*u`

Thank you so much for the reply. I initially had `norm(brake <= u)` and then I removed it since I faced that error. After making the changes like this,

``````cvx_begin
variable x(N);
variable v(N);
variable u(N);
whos
minimize(1/2 * sum(u.^2));
subject to
norm(brake) <= u;
u <= norm(acc);
x >= norm(minDist);

x(1) == x0;
v(1) == v0;

for i = 1:N-1
x(i+1) == x(i) + delta * v(i)  + 0.5 * delta^2 * u(i);
v(i+1) == v(i) + delta * u(i);
end

cvx_end
``````

I get, `sqlp stop: primal or dual is diverging, 1.9e+15. Status: Failed Optimal value (cvx_optval): NaN`

Which solver were you using? You can try a different solver. if you have them available, Gurobi and Mosek are likely more numerically robust than SeDuMi or SDPT3. you haven;t shown your input data, it is possible that is scaled poorly, which could contribute to numerical difficulties for the solver. If you show solver output (from each iteration), that might assist readers in assessing your situation.

`control(1,2,-5,5,10,1,5)` is my input data.

I am using SDPT3 solver.

``````Calling SDPT3 4.0: 65 variables, 25 equality constraints
For improved efficiency, SDPT3 is solving the dual problem.
------------------------------------------------------------

num. of constraints = 25
dim. of sdp    var  = 20,   num. of sdp  blk  = 10
dim. of linear var  = 30
dim. of free   var  =  5
20 linear variables from unrestricted variable.
SDPT3: Infeasible path-following algorithms

version  predcorr  gam  expon  scale_data
HKM      1      0.000   1        0
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
0|0.000|0.000|4.0e+01|5.5e+00|4.0e+04| 2.950000e+02  0.000000e+00| 0:0:00| chol  1  1
1|0.762|0.835|9.6e+00|9.2e-01|2.6e+03|-2.715327e+02 -5.311421e+01| 0:0:00| chol  1  1
2|0.450|0.137|5.3e+00|8.2e-01|1.8e+03|-1.696343e+03 -6.342143e+01| 0:0:00| chol  1  1
3|0.435|0.014|3.0e+00|8.3e-01|5.1e+03|-2.364538e+06 -6.561209e+01| 0:0:00| chol  1  1
4|0.239|0.001|2.3e+00|8.6e-01|3.8e+07|-2.488475e+11 -6.654093e+01| 0:0:00| chol  1  1
5|0.000|0.000|2.3e+00|8.8e-01|8.0e+10|-1.036885e+15 -4.291467e+02| 0:0:00| chol  1  1
6|0.000|0.000|2.3e+00|9.0e-01|1.5e+12|-1.930079e+16 -7.273780e+02| 0:0:00| chol  1  1
7|0.000|0.000|2.3e+00|9.3e-01|5.7e+12|-7.418475e+16 -2.296116e+03| 0:0:00| chol  1  2
8|0.000|0.000|2.3e+00|9.5e-01|2.4e+13|-3.062409e+17 -5.982579e+03| 0:0:00|
sqlp stop: primal or dual is diverging, 1.4e+15
-------------------------------------------------------------------
number of iterations   =  8
Total CPU time (secs)  = 0.12
CPU time per iteration = 0.01
termination code       =  3
DIMACS: 3.9e+00  0.0e+00  1.4e+00  0.0e+00  -1.0e+00  7.7e-05
-------------------------------------------------------------------

------------------------------------------------------------
Status: Failed
Optimal value (cvx_optval): NaN
``````

Thank you so much. Let me know if the input is scaled poorly. I’ll try changing the solver and check if it works fine.

You haven’t provided all the input data. Can you provide all the commands which need to be run starting from a new MATLAB session to execute your problem?

Obviously, SDPT3 failed horribly on your problem.

``````control(1,1,-5,5,10,1,5)

function acceleration = control(x0,v0,brake, acc, N, delta, minDist)

cvx_begin
variable x(N);
variable v(N);
variable u(N);
whos
minimize(1/2 * sum(u.^2));
subject to
norm(brake) <= u <= norm(acc);
x >= norm(minDist);

x(1) == x0;
v(1) == v0;

for i = 1:N-1
x(i+1) == x(i) + delta * v(i)  + 0.5 * delta.^2 * u(i);
v(i+1) == v(i) + delta * u(i);
end

cvx_end
end
``````

Is my entire program. I am running once now. Just by passing the parameters to the control function.

I have no idea what the control function is. You need to provide values for all variables in the CVX program, other than those declared as variables in the CVX program.

The control function is what that has been defined in the second line. All the code of cvx is present inside that function.

``````control(1,1,-5,5,10,1,5)

function acceleration = control(x0,v0,brake, acc, N, delta, minDist)

cvx_begin
variable x(N);
variable v(N);
variable u(N);
whos
minimize(1/2 * sum(u.^2));
subject to
norm(brake) <= u <= norm(acc);
x >= norm(minDist);

x(1) == x0;
v(1) == v0;

for i = 1:N-1
x(i+1) == x(i) + delta * v(i)  + 0.5 * delta.^2 * u(i);
v(i+1) == v(i) + delta * u(i);
end

cvx_end
end
``````

The confusion might have been because of the indentation? The control function gets all the variables that are required.

Your problem is infeasible. It would be feasible for any value of minDist <= 1, and infeasible for any value of minDist > 1. If any value of minDist <=1 is used, the optimal objective value = 125., and even SDPt3 can solve that problem.

You can easily see this is due to the constraint `x(1) == x0`, which in your case is `x(1) == 1`, which is “incompatible” with, i.e., infeasible given the constraint `x >= norm(minDist)`, which in your case is `x >= 5`. So alternatively, you could change the value of `x0` to get a feasible problem.

1 Like

Thank you so much. The constraints were causing the problem. I am now able to get the optimal solution.