Matrix dimensions must agree.
6 views (last 30 days)
Show older comments
Hi, I have opened the following example in Matlab - openExample('optim/OptimalDispatchExample'). I have followed the coding and altered values where necessary for my project. I have nearly completed the coding however am now getting a "Matrix dimensions must agree." message appear on one of the commands. I can't seem to find the issue. I have copied and pasted the script below - the error seems to be with the final line? Any help would be much appreciated. Regards Martin
load dispatchPrice; % Get poolPrice, which is the revenue per MWh
bar(poolPrice,.5)
xlim([.5,48.5])
xlabel('30 Minute Intervals')
ylabel('Power Consumption (MWh)')
title('Average Daily Power Consumption')
fuelPrice = 3;
totalfuel = 3.95e4;
nPeriods = length(poolPrice); % 48 periods
nGens = 2; % Two generators
gen = [1.185,1.580;1.185,1.580]; % Generator 1 low = 1.185 MW, high = 1.580 MW
fuel = [427,806;325,765]; % Fuel consumption for generator 2 is low = 325, high = 765
startCost = 1e4; % Cost to start a generator after it has been off
efficiency = gen./fuel; % Calculate electricity per unit fuel use
rr = efficiency'; % for plotting
h = bar(rr);
h(1).FaceColor = 'g';
h(2).FaceColor = 'c';
legend(h,'Generator 1','Generator 2','Location','NorthEastOutside')
ax = gca;
ax.XTick = [1,2];
ax.XTickLabel = {'Low','High'};
ylim([.01,0.5])
ylabel('Efficiency')
lby = zeros(nPeriods,nGens,2); % 0 for the y variables
lbz = zeros(nPeriods,nGens); % 0 for the z variables
lb = [lby(:);lbz(:)]; % Column vector lower bound
ub = ones(size(lb)); % Binary variables have lower bound 0, upper bound 1
cleary = zeros(nPeriods,nGens,2);
clearz = zeros(nPeriods,nGens);
A = spalloc(nPeriods*nGens,length(lb),2*nPeriods*nGens); % nPeriods*nGens inequalities
counter = 1;
for ii = 1:nPeriods
for jj = 1:nGens
temp = cleary;
temp(ii,jj,:) = 1;
addrow = [temp(:);clearz(:)]';
A(counter,:) = sparse(addrow);
counter = counter + 1;
end
end
b = ones(nPeriods*nGens,1); % A*x <= b means no more than one of x(i,j,1) and x(i,j,2) are equal to 1
yFuel = lby; % Initialize fuel usage array
yFuel(:,1,1) = fuel(1,1); % Fuel use of generator 1 in low setting
yFuel(:,1,2) = fuel(1,2); % Fuel use of generator 1 in high setting
yFuel(:,2,1) = fuel(2,1); % Fuel use of generator 2 in low setting
yFuel(:,2,2) = fuel(2,2); % Fuel use of generator 2 in high setting
addrow = [yFuel(:);clearz(:)]';
A = [A;sparse(addrow)];
b = [b;totalfuel]; % A*x <= b means the total fuel usage is <= totalfuel
tempA = spalloc(nPeriods*nGens,length(lb),2*nPeriods*nGens);
counter = 1;
for ii = 1:nPeriods
for jj = 1:nGens
temp = cleary;
tempy = clearz;
temp(ii,jj,1) = -1;
temp(ii,jj,2) = -1;
if ii < nPeriods % Intervals 1 to 47
temp(ii+1,jj,1) = 1;
temp(ii+1,jj,2) = 1;
else % Interval 1 follows interval 48
temp(1,jj,1) = 1;
temp(1,jj,2) = 1;
end
tempy(ii,jj) = -1;
temp = [temp(:);tempy(:)]'; % Row vector for inclusion in tempA matrix
tempA(counter,:) = sparse(temp);
counter = counter + 1;
end
end
A = [A;tempA];
b = [b;zeros(nPeriods*nGens,1)]; % A*x <= b sets z(i,j) = 1 at generator startup
filledfraction = nnz(A)/numel(A)
generatorlevel = lby; % Generation in MW, start with 0s
generatorlevel(:,1,1) = gen(1,1); % Fill in the levels
generatorlevel(:,1,2) = gen(1,2);
generatorlevel(:,2,1) = gen(2,1);
generatorlevel(:,2,2) = gen(2,2);
revenue = generatorlevel; % Allocate revenue array
for ii = 1:nPeriods
revenue(ii,:,:) = poolPrice(ii)*generatorlevel(ii,:,:);
end
fuelCost = fuel*fuelPrice;
starts = (clearz + 1)*startCost;
starts = starts(:); % Generator startup cost vector
f = [revenue(:) - fuelCost(:);-starts]; % f is the objective function vector
13 Comments
Guillaume
on 5 Dec 2017
Edited: Guillaume
on 5 Dec 2017
@Martin Harris,
I have not spent much time trying to understand your code because:
a) I've got other things to do with my spare time than trying to decipher uncommented code that does not even explain what it is trying to achieve.
b) The code is clearly not going to work and again, I've no idea what it is trying to achieve, so why try to understand the bits that don't work?
c) it's something you should have done yourself
d) I've understood enough to give you a detailed and lengthy explanation of why there is an error.
That explanation ends with advice and link to help you debug your code efficiently. If it's not good enough for you, I wouldn't hold my breath that somebody else is going to come along with more.
There was no sacarsm at all in my answer.
Answers (1)
Guillaume
on 5 Dec 2017
I've not spent much time trying to understand your code. There's no comment at all explaining what it is it's doing. I've understood enough to see that it is never going to work.
We start with
nPeriod = length(poolPrice);
nGens = 2;
Ok. We'll assume poolPrice is a vector (otherwise using length is a bug) with nPeriod elements. We have:
lby = zeros(nPeriods,nGens,2);
So lby is nPeriods x nGens x 2.
We then have some loops that calculate some A and b that are never used anywhere. We then have:
generatorlevel = lby; % Generation in MW, start with 0s
generatorlevel(:,1,1) = gen(1,1); % Fill in the levels
generatorlevel(:,1,2) = gen(1,2);
generatorlevel(:,2,1) = gen(2,1);
generatorlevel(:,2,2) = gen(2,2);
So generatorlevel is the same as lby (which has never been filled by anything other than 0s, by the way). And then we copy some values. Note that the size of the 2nd dimension is now hardcoded so if nGens is anything other than 2, then of course the above is not going to work properly. Poor coding, but not the issue.
Then we copy generatorlevel into revenue and change adjust the values:
revenue = generatorlevel; % Allocate revenue array
for ...
end
So in the end, revenue(:) has nperiod x nGen x 2 elements into one column.
Now for fuelCost, we have:
fuelCost = fuel*fuelPrice;
with
fuelPrice = 3;
fuel = [427,806;325,765]
So clearly fuelCost has as many elements as fuel which is only 4. For the subtraction to work fuel would need nperiod x nGen x 2 shaped however you want.
Finally, starts. starts is the same size as clearz which is defined as
clearz = zeros(nPeriods,nGens);
so is nPeriods x nGens. That's always going to be half as many elements as revenue so you're never going to be able to concatenate them together.
Learn to debug your own code. Step through the program, see what it is doing and look at the variable values. Debugging through forum is rarely efficient.
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!