Your objective function is not a scalar?

Hello Everyone,
I am trying to implement one of Prof. Boyd example.

When I run the code, it gives an error and I am very new user of cvx software. I attached the code that I created and also screenshot of example. Any feedback is appriciated. Thank you

N = 4; % number of vehicles
T = 90; % number of time periods
Cmax = 3;
Q(1,T)=20;
Q(2,T)=0;
Q(3,T)=30;
Q(4,T)=25;
lambda=[0.5,0.3,2,0.6];
Qdes(1,T) = 60;
Qdes(2,T) = 100;
Qdes(3,T) = 75;
Qdes(4,T) = 125;

cvx_quiet(true);
cvx_begin
variables C(N,T) Q(N,T) Qtar(N,T);
subject to
C(N,T)>=0 ;
sum(C(N,T))<=Cmax;
Q(N,T)==Q(N:T-1)+C(N:T-1);
Qtar(N,T)==(1:T+1).^lambda(N)*Qdes(N,T)
Q(1,90)==60;
Q(2,90)==100;
Q(3,90)==75;
Q(4,90)==125;
s(N,T)=(Qtar(N,T)-Q(N,T))
S=1/((T+1)*N)*sum_square(s)
minimize S
cvx_end

s is 4 by 90. So S is 1 by 90, which is not a scalar. Hence the error message. You need another sum around sum_square .

Actually, I think you want
s = Qtar - Q;

Them you would have
S=1/((T+1)*N)*sum(sum_square(s))

Perhaps you have a bunch of other stuff messed up as well - I haven’t checked what it should be. A constraint such as Q(N,T)==Q(N:T-1)+C(N:T-1); is interpreted as Q(4,90)==Q(4:89)+C(4:89); and does not impose a constraint for any other index values. That constraint is constraining 86 elements of the vector on the RHS to equal the scalar Q(4,90). Is that what you want?. You need to make use of : and if necessary, for loops, if you want constraints to hold for multiple index values.

Mu code was
s = Qtar - Q;

As for the error,read http://cvxr.com/cvx/doc/basics.html#assignment-and-expression-holders

I recommend you carefully read the entirety of the CVX Users’ Guide, and start with the examples there. I doubt you have correctly implemented the constraints, although I don’t know whether the problem would be feasible if you did, using your input data. Perhaps you also need to brush up on MATLAB fundamentals, such as indexing and : .

Your code gives off red flags that you don’t know the basics. C(N,T) >= 0 is constraining just the element C(4,90). sum(C(N,T))<=Cmax is not summing anything (except one element).