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

Stephen23
Stephen23 2017년 12월 5일
@Martin Harris: please show the complete error message. This means all of the red text.
Martin Harris
Martin Harris 2017년 12월 5일
>> f = [revenue(:) - fuelCost(:);-starts]; % f is the objective function vector Matrix dimensions must agree.
Martin Harris
Martin Harris 2017년 12월 5일
Matrix dimensions must agree.
Surely just looking at
size( revenue(:) )
size( fuelCost(:) )
size( starts )
on command line from a breakpoint will help you easily find the problem. Far easier than someone looking at a large amount of unformatted code.
Guillaume
Guillaume 2017년 12월 5일
편집: Guillaume 2017년 12월 5일
A full error message will start with error using ... then follow on with Matrix dimensions must agree and then possibly with a stack trace. We're asking you the full error message as it makes it much easier to help you.
What is
size(revenue(:))
size(fuelCost(:))
size(starts)
Clearly, one of them is not the same size as the others as the error message tells you.
>> size( revenue(:) ) size( fuelCost(:) ) size( starts )
ans =
192 1
ans =
4 1
ans =
96 1
I have followed Adams instructions and received these results. I still can't see a problem, am I right in saying this is not a coding issue and is a issue with my values?
Adam
Adam 2017년 12월 5일
편집: Adam 2017년 12월 5일
Clearly trying to subtract a 4x1 vector from a 192x1 vector will give a matrix dimensions error.
The error message itself clearly states 'Matrix dimensions must agree'.
Depending on what you intend:
revenue(:) - fuelCost(:)'
would be valid syntax in recent Matlab versions using implicit expansion, but will give you a 192x4 matrix which you clearly cannot concatenate with a 96x1 vector afterwards.
Guillaume
Guillaume 2017년 12월 5일
I still can't see a problem
Really? You're trying to subtract a column of 4 values from a column of 192 values and you're trying to stick the whole lot next to a column of 96 values, and you don't see a problem?
am I right in saying this is not a coding issue and is a issue with my values?
I'd say there's something very wrong with the code. There's no way that last line is ever going to work, regardless of the input size.
Martin Harris
Martin Harris 2017년 12월 5일

Wow thanks Guillaume for that hilarious reply. I am new to Matlab and am trying to teach myself but thanks for the support. As explained earlier, I have opened an example and have altered a few values to suit my project. I am assuming the examples work hence the name example and so was after some advice and not sarcasm. I did Really think I may get some help on here but thanks for clarifying you have not spent much time to understand the example code. Much appreciated!

Stephen23
Stephen23 2017년 12월 5일
편집: Stephen23 2017년 12월 5일
@Martin Harris: It is great that you are teaching yourself, and found some examples to work through. To be honest the best place to start to learn MATLAB is by doing the introductory tutorials. I would recommend these even if you now have some experience with MATLAB, as they teach many fundamental MATLAB concepts that beginners sometimes miss:
If you have some coding experience then just forget it all. MATLAB is nothing like C++/Java/..., and is much more like real mathematics, which is why matrices, sizes and orientations are important (and are the cause of some of the problems that you have in your code).
In general you have made a mistake that many beginners make: not actually looking at what your code is doing. Just slow down with writing so many lines! The point of writing code is not to write code, but to perform some task. So you first need to understand that task and also whatever algorithms that are required. Then take it slowly implementing them: define one line at a time, read the documentation for every operator and function (yes, even simple things like multiplication and addition), and check each line as you write it. Checking means running it with values, and confirming that you get the required outputs (compare with values you calculate by hand, or any other method). Only when you understand the line and are happy that it does what you need should you move on to writing the next line.
Have fun and good luck learning!
Martin Harris
Martin Harris 2017년 12월 5일
Thank you Stephen. Much appreciate!
Guillaume
Guillaume 2017년 12월 5일
편집: Guillaume 2017년 12월 5일
@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.
Martin Harris
Martin Harris 2017년 12월 5일
@Guillaume, Not to worry, Stephen Cobeldick has given me some good advice. I have gone through the example and found the error. The coding does work and am now experimenting with various data. No need for anyone to hold their breath.

댓글을 달려면 로그인하십시오.

답변 (1개)

Guillaume
Guillaume 2017년 12월 5일

0 개 추천

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.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2017년 12월 5일

댓글:

2017년 12월 5일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by