# Error using * (line 41) Inner matrix dimensions must agree

I wrote the sample code with variable length of 10 and the solver solved it. Using the same code I then increased my data points to 8760 and now i get
’Error using * (line 41)
Inner matrix dimensions must agree
Error in minimize (line 14)
x = evalin( ‘caller’, sprintf( '%s ', varargin{:} ) );

Error in tempimport (line 95)
minimize Price*P’
Tamb, Price are 8760x1 data set
The code is as

``````Q=-100.5*(1-exp(-15/434));Q1=exp(-15/434);Q2=(1-exp(-15/434)); % Check 100.5 value for C/W
Price= reshape(price,[8760,1]);
P_ac=5;
To=Tamb; % Temp outside
Tmin=71;Tmax=75;Tint=[74;73;72];
cvx_begin
cvx_solver gurobi
variables T1(8761) T2(8761) T3(8761) P(8760);
variable W1(8760) integer;
variable W2(8760) integer;
variable W3(8760) integer;
minimize Price*P

for Wn=1:1:1
T1((Wn-1)*0+1)==Tint(1);
T2((Wn-1)*0+1)==Tint(2);
T3((Wn-1)*0+1)==Tint(3);

for i=2:8761;

T1(i)==To*Q2+W1(i-1)*Q+T1(i-1)*Q1;
T2(i)==To*Q2+W2(i-1)*Q+T2(i-1)*Q1;
T3(i)==To*Q2+W3(i-1)*Q+T3(i-1)*Q1;
end
end

Tmin<=T1<=Tmax;
Tmin<=T2<=Tmax;
Tmin<=T3<=Tmax;
P==[W1]*P_ac+[W2]*P_ac+[W3]*P_ac;
0<=W1<=1;
0<=W2<=1;
0<=W3<=1;

cvx_end``````

You have two different versions:

``````minimize Price*P'
``````

and

``````minimize Price*P
``````

Which is it? Neither of these are correct. Price and P are both 8760 by 1, so you need

``````Price'*P
``````

or

``````P'*Price
``````

which are equivalent, and which are scalars resulting from a 1 by 8760 vector multiplied by a 8760 by 1 vector.

I haven’t checked the rest of what you’ve done.

@Mark_L_Stone
Price’*P or P’*Price,
the part *Price’P or *P’Proce becomes red and throws error saying
Error: File: tempimport.m Line: 95 Column: 14
String is not terminated properly.

Post your entire MATLAB session, to include CVX code, and exact output.

@Mark_L_Stone Here is the matlab script and two files are here on these links

Price is a cell array. I believe you need it as a double array to use in the objective function. And then do as I suggested in my first post.

After fixing the Price to double, I am getting some other errors which are within cvx functions
Error in minimize (line 14)
x = evalin( ‘caller’, sprintf( '%s ', varargin{:} ) );

Error in tempimport (line 76)
minimize Price*P Where is the ’ , i.e.,

``````Price' * P
``````

If instead of there being CVX variables, everything were MATLAB variables, you need everything to be valid MATLAB code, including all dimensions being conformal. You need to get that correct before you start worrying about the CVX matters.

If you need help with general MATLAB coding, you will probably do better at MATLAB Central http://www.mathworks.com/matlabcentral/answers/ .

Sorry.
I did try all the possible PriceP, Price’P, PriceP’, while the later too were giving errors as
Error: File: tempimport.m Line: 76 Column: 14
String is not terminated properly.
The line minimize **Price
P’** or *Price’P in the editor changes to red and is not correct to Matlab either.

Issue the
whos
command just before the minimize command. What does it show?

Name Size Bytes Class Attributes

P 8760x1 211513 cvx
P_ac 1x1 8 double
Price 8760x1 70080 double
Q 1x1 8 double
Q1 1x1 8 double
Q2 1x1 8 double
T1 8761x1 211537 cvx
T2 8761x1 211537 cvx
T3 8761x1 211537 cvx
Tamb 8760x1 1051200 cell
Tint 3x1 24 double
Tmax 1x1 8 double
Tmin 1x1 8 double
To 8760x1 1051200 cell
W1 8760x1 211513 cvx
W2 8760x1 211513 cvx
W3 8760x1 211513 cvx
cvx_problem 1x1 544 cvxprob
price 365x24 70080 double

Error using * (line 41)
Inner matrix dimensions must agree.

Error in minimize (line 14)
x = evalin( ‘caller’, sprintf( '%s ', varargin{:} ) );

Error in tempimport (line 81)
minimize Price*P

You are showing

`````` Price*P
``````

This is 8760 by 1 times 8760 by 1, which is incompatible (non-conformal) as I previously pointed out.
Have you actually tried

``````Price' * P
``````

which is 1 by 8760 times 8760 by 1, which is compatible (conformal), with Price being a double array as now, but not originally?

when i use Price’*P, it gives me another error. With every suggestion of yours I try all combinations:-)

Error: File: tempimport.m Line: 81 Column: 14
String is not terminated properly.

Try this: `transpose(Price) * P`

transpose worked and the code ran for long time to return this output
Redundant equality constraints detected.
CVX cannot solve this problem; but it is likely infeasible.
Status: Overdetermined
Optimal value (cvx_optval): NaN

Elapsed time is 7680.223863 seconds.

This code worked well for 10 step values, Am I doing something wrong. Can I make it run for 24*365 steps instead-how?

@mcg @Mark_L_Stone
I tried to simplify the problem with 24 steps

clc
clear all
tic
filename = ‘weatherdatafile.CSV’;
delimiter = ‘,’;
startRow = 3;
formatSpec = ‘%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%f%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%[^\n\r]’;
fileID = fopen(filename,‘r’);
dataArray = textscan(fileID, formatSpec, ‘Delimiter’, delimiter, ‘EmptyValue’ ,NaN,‘HeaderLines’ ,startRow-1, ‘ReturnOnError’, false);
fclose(fileID);
Tamb = [dataArray{1:end-1}];
clearvars filename delimiter startRow formatSpec fileID dataArray ans;

%% Solve optimization
n=24;
P=zeros(n);
Q=-100.5*(1-exp(-15/434));Q1=exp(-15/434);Q2=(1-exp(-15/434)); % Check 100.5 value for C/W
Price= 100.price(1:24);
P_ac=5;
To=Tamb(1:24); % Temp outside
Tmin=71;Tmax=75;Tint=[74;73;72];
cvx_begin
cvx_solver gurobi
variables T1(n+1) T2(n+1) T3(n+1) P(n);
variable W1(n) integer;
variable W2(n) integer;
variable W3(n) integer;
minimize Price
P

``````    T1(1)==Tint(1);
T2(1)==Tint(2);
T3(1)==Tint(3);
``````

for i=2:25;

`````` T1(i)==To(i-1)*Q2+W1(i-1)*Q+T1(i-1)*Q1;
T2(i)==To(i-1)*Q2+W2(i-1)*Q+T2(i-1)*Q1;
T3(i)==To(i-1)*Q2+W3(i-1)*Q+T3(i-1)*Q1;
``````

end

``````Tmin<=T1<=Tmax;
Tmin<=T2<=Tmax;
Tmin<=T3<=Tmax;
P==[W1]*P_ac+[W2]*P_ac+[W3]*P_ac;
0<=W1<=1;
0<=W2<=1;
0<=W3<=1;
``````

cvx_end
toc

## The output come to Calling Gurobi 6.00: 366 variables, 294 equality constraints

Gurobi optimizer, licensed to CVX for CVX
Optimize a model with 294 rows, 366 columns and 720 nonzeros
Coefficient statistics:
Matrix range [9e-01, 3e+00]
Objective range [7e+01, 7e+01]
Bounds range [0e+00, 0e+00]
RHS range [8e-02, 7e+00]
Presolve removed 6 rows and 261 columns
Presolve time: 0.00s

Explored 0 nodes (0 simplex iterations) in 0.00 seconds
Thread count was 1 (of 8 available processors)

## Model is infeasible or unbounded Best objective -, best bound -, gap -

Status: Failed
Optimal value (cvx_optval): NaN