I have the code below in which I am trying to start the “number_panels” and “number_turbines” for loop from a non zero number.
More specifically I am trying for 3000 to 4000 “number_panels” with 500 panel interval
and from 5 to 8 “number_turbines” with one turbine interval
i.e
number_of_days = 2;
for number_panels = 3000:500:4000 % range of PV panel units examined
for number_turbines = 5:8 % range of wind turbine units examined
for h=1:24 %# hours
for d = 1:number_of_days %# which day
n = h + 24*(d-1);
% hourly_deficit_1(...,..., h, d)= Demand(n)-(PV_supply(n)... %
hourly_deficit(number_panels + 1, number_turbines + 1, h,d) = hourly_annual_demand(n) - (hourly_annual_PV(n)*number_panels) - (hourly_annual_WT(n)*number_turbines);% hourly power deficit (RES supply with demand)
if hourly_deficit(number_panels + 1, number_turbines + 1, h,d)< 0 % zero out negative hourly deficit values (this is power surplus from RES)
hourly_deficit(number_panels + 1, number_turbines + 1, h,d) = 0;
end
When I do this I get a size(hourly_deficit) = 4001,9,24,2 whereas I am expecting and trying to achieve a 3,4,24,2 size. Does anyone know where I am going wrong?
The value of the variable
number_panelsstarts at 3000 instead of 0 or 1. Thus when you index a matrix with that variable as the index value Matlab thinks you are wanting the 3001st index and thus gives you a matrix that is 3000 zeros with the 3001st being set to what you ask.If you follow the loop into it’s next cycle the value of
number_panelsbecomes 3500. You now are indexing at 3501, based on your code. This means that all of the places from 3002 to 3500 will be filled with zeros and 3501 will be set to whatever value you give it.The same logic applies to
number_turbinesThe only difference is that you’ll be indexing by 1 instead of by 500 like you are withnumber_panels.If you want to get back to the size matrix you are expecting you’ll need to modify the way you call the index values. This could be done a number of ways. You could have a counter within the for-loop or you could use modulus math. Modulus math doesn’t work well when you’re using a step size that isn’t
1. It also doesn’t work when you get to the point where you have an index value that is a multiple of your starting index.You’ll have to work out what will work best for you in that arena. Especially since you want to use a step size that isn’t 1. But for the
number_turbinesthat goes from 5 to 8, you can simple index usingnumber_turbines - 5 + 1or more concisenumber_turbines-4.For clarity, here is the code you provided with the necessary tweaks to show the use of what was mentioned in the comments. Please take note that you will need to modify the
-4for thenumber_turbinesindex value should you start at something other than5. Also note that you need to index thenumber_panelsvector now since it’s not a looped value.Hope this helps!