Using the length() in a for loop

Hi I have the following code which I am trying to loop. the loop isn't working however and I don't know why
PVsupply = [0 0 0 0 0 0.05 0.1 0.11 0.13 0.13 0.15 0.15 0.15 0.15 0.145 0.145 0.14 0.135 0.08 0.05 0 0 0 0];
hourly_daily_PV = PVsupply(:);
hourly_annual_PV = repmat(hourly_daily_PV,365,1);
WTsupply = [10 10 10 10 25 25 25 15 15 15 15 15 15 15 35 35 35 35 35 10 10 10 10 10 ];
hourly_daily_WT = WTsupply(:);
hourly_annual_WT = repmat(hourly_daily_WT,365,1);
number_panels = 1:10:100;
number_turbines = 1:1:5;
for idx_number_panels = 1:length(number_panels) % range of PV panel units examined
for idx_number_turbines = 1:length(number_turbines) % range of wind turbine units examined
for number_batteries = 1:5 % range of battery units examined
for h=2:25 %# hours
hourly_total_RES(idx_number_panels,idx_number_turbines,number_batteries, h) = hourly_annual_PV(h)*number_panels(idx_number_panels) + hourly_annual_WT(h)*number_turbines(idx_number_turbines);
end
end
end
end

5 个评论

I suggest the following names for the variables:
panels = 1:10:100;
turbines = 1:5;
% range of PV panel units examined
for np = 1:length(panels)
% range of wind turbine units examined
for nt = 1:length(turbines)
% range of battery units examined
for nb = 1:5
%# hours
for h = 2:25
hourly_total_RES(np,nt,nb,h) = hourly_annual_PV(h) * panels(np) + hourly_annual_WT(h) * turbines(nt);
end
end
end
end
Saying that the loop doesn't work, means nothing to me. Did you get an error?
Also, you loop for number of batteries but it doesn't get into the inner equation.
Another thing, what is hourly_annual_PV and hourly_annual_WT?
If those are functions aren't they vectorized?
Oleg, thanks for the reply. I get an error at h=6 but I don't know why. The only thing happening at h=6 is that the hourly_annual_PV value goes stops being a non zero. I have added the vectors for the hourly_annual_PV and hourly_annual_WT in the original question above
what is your error message
The error I am getting is that the "hourly_total_RES" variable only calculates up to h = 5, whereas it should be going to h = 25 as shown above. The size(hourly_total_RES) is 1 1 1 5, wheras it should be size(hourly_total_RES) = 10 5 5 24
I don't get any error, nor the size is [1 1 1 5] with the data and script you supplied.

请先登录,再进行评论。

回答(1 个)

i am not getting any errors; before running your code use
clear
if it does'nt work check if any of your variables are not shadowed by any function or file with the same name in your working folder

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by