Python code vs Matlab code

Hi everybody. I need your help.

I wrote this scripts in Matlab, and it works nicely.

cvx_begin quiet
cvx_precision default
variables v(3,1) real;
expression MM(length(M));

   MM = sum(reshape(Mstack*v+repmat(pos',[512,1]),[3,512]).^2,1); 
   minimize max(MM)



  • pos is a [3,1] vector
  • Mstack is a [1536,3] matrix composed by 512 [3,3] matrix (in columned) rappresenting each vertex of a [3,3] interval matrix.

I need to translate it in python code. I try with the code below, but it doesn’t work.

import numpy as np
from numpy import linalg as LA
import numpy.matlib
import scipy  
import cvxpy as cvx

# V is my Mstack in Matlab script, a [1536,3] array.

v1 = cvx.Variable()
v2 = cvx.Variable()
v3 = cvx.Variable()
v = np.array([v1,v2,v3])
v.shape = (3,1)

exp = np.sum(np.square(np.reshape(,512,1),(3,512), order='F')),axis=0)
obj = cvx.Minimize(np.amax(exp))

prob = cvx.problem(obj)

I can’t find a way to solve it. But everytime I try to change the expression I’ve got a different error, for example:

"ValueError: setting an array element with a sequence."
"Exception: Cannot evaluate the truth value of a constraint or chain constraints, e.g., 1 >= x >= 0"
"Forming a nonconvex expression (affine)*(affine)."

I’m sure it has to work, in matlab it’s all fine.
Thank for your time


CVXPY is not CVX, nor is it a port to Python of CVX. I think you should ask for help on getting your CVXPY program to run at!forum/cvxpy .

1 Like

Mark is correct. Thanks for redirecting the post!