Continuous plotting with nested for loops

2 次查看(过去 30 天)
clc
DOY = 1;
Lref = 0;
L = 4.23;
phi = 55.86;
gamma = 0; %0 is facing South
beta = 26; %26 degree tilt of front window
Area = 1.206; %not accurate
DirectNormalData = WeatherData(:,3,:);
DiffuseHorizontalData = WeatherData(:,1,:);
Q = 0;
DOY = 1;
DOYstart = 1;
if DOYstart == 1
DOYinitial = 1;
else
DOYinitial = 73*DOYstart;
end
for j = DOYinitial:DOY %20 minute iterations for one day
for i=1:72
tref = i/(3*DOY);
%Declination angle
dec = 23.5*sin((360*(284+DOY))/365);
%Hour angle
B = 360*(DOY-81)/364;
E = 9.87*sin(2*B)-7.35*cos(B)-1.5*sin(B);
tsol = tref + (4*(Lref-L)+E)/60;
h = 15*(tsol-12);
%Solar altitude
as = asin(cos(phi)*cos(h)*cos(dec)+sin(phi)+sin(dec));
%Surface Solar azimuth
gammaS = acos((sin(phi)*cos(h)*cos(dec)-cos(phi)*sin(dec))/cos(as));
w = gammaS-gamma;
%Direct Beam
IB = DirectNormalData(i);
%Direct Beam intensity in vertical direction
IBx = IB*(cos(as)+sin(beta)*cos(w)+sin(as)*cos(beta));
%diffuse horizontal component
IDh = DiffuseHorizontalData(i);
%using the isentropic sky model
IDbeta = IDh*(1+cos(beta))/2;
%Total radiation falling on the surface is
Itot = IBx + IDbeta;
Q = Itot*Area;
hold on
plot(i,Q, "-r.", 'MarkerSize',7)
title('Solar Radiation Heat Transfer against Time', 'Fontsize',16)
xlabel('Time (20 minute iterations)', 'fontsize', 14)
ylabel('Heat transfer into cabin (W)', 'fontsize', 14)
end
hold on
end
WeatherData is a 3D array with weather data for each day stored as 2D arrays with the 3rd dimension being the days in a year
I want to be able to plot Q against time
i is indicating 20 minute step sizes, hence i=1:72 is a days worth of calculations
I know plotting I against Q won't work seeing as the range of i keeps repeating for iterations of j
I initially only had one for loop indicated as:
for i = DOYinitial:72*DOY
This caused errors for tref because tref is the reference time in 24 hour time. For each new day I need tref to start at tref=1/3 and continue in increments of 1/3.
The following script is used to convert the data file into a 3D array
close all
clear
clc
%% Load Data
hours = 24;
days = 365;
content = strsplit(fileread("GBR_SCT_Glasgow.AP.031400_TMYx.2007-2021.clm"));
for i = 1:days
climateDataString(:,:,i) = content(1,89+((i-1)*29):89+((i-1)*29)+23);
end
climateDataString = permute(climateDataString, [2 1 3]);
for i = 1:days
for ii = 1:hours
climateData(ii,:,i) = str2double(strsplit(cell2mat(climateDataString(ii,1,i)), ','));
end
end
S=size(climateData);
WeatherData = interp1(climateData,linspace(1,S(1),3*S(1)));
any help is greatly appreciated. Thank you
  2 个评论
Torsten
Torsten 2022-11-24
I think without the possibility to test the code with access to the matrix "WeatherData", nobody will try to answer your question.
Daniel Lynch
Daniel Lynch 2022-11-25
@Torsten Thank you for the advise. I have edited the post to include the file and the script I used to read the data.

请先登录,再进行评论。

回答(1 个)

SANKALP DEV
SANKALP DEV 2023-9-6
Hey Daniel,
I understand you are trying to calculate the heat transfer into a cabin due to solar radiation, using Weather Data and want to plot heat transfer (Q) against time.
Please consider following steps to plot they heat transfer (‘Q’) against time:
  • Initialize arrays Q and time to store calculated values: Create empty arrays to hold the heat transfer values (Q) and the corresponding time intervals (time).
  • Adjust the loop structure to iterate through the desired range of days (DOYinitial:DOY) and within each day, iterate through the 20-minute intervals (i = 1:72).
  • Calculate ‘tref’ by dividing i by 3 to represent 20-minute intervals: Compute the time reference (‘tref’) by dividing the interval index (i) by 3, assuming each increment of 1/3 represents 20 minutes.
  • Store calculated Q values and corresponding time intervals in Q and time arrays: Append the calculated heat transfer values (Q) to the Q array and the corresponding time intervals (tref) to the time array.
  • Plot Q against time.
After examining your code, I have implemented the following modifications:
  1. Adjusted the calculation of tref to assume that 1/3 represents 20 minutes. This adjustment ensures that the time intervals align correctly with the data.
  2. Modified the indexing of DirectNormalData and DiffuseHorizontalDatato access the data for the current day (‘j’ index added). This ensures that the correct weather data is used for each iteration.
  3. Replaced the inner loop's plotting code with the code to store ‘Q’ and time values at each iteration. This allows us to collect the calculated values for later plotting.
Please refer to the following code:
% Initialize arrays to store the heat transfer values (Q) and corresponding time values
Q = [];
time = [];
% Iterate through the desired range of days
for j = DOYinitial:DOY
% Determine the number of intervals for the current day
intervals = 72;
% Iterate through the 20-minute intervals
for i = 1:intervals
% Calculate the time reference (tref) to represent 20-minute intervals
tref = i / 3;
% Calculate the heat transfer (Q) based on the given equations and weather data
Q_value = (DirectNormalData(j, i) + DiffuseHorizontalData(j, i)) * Area * (sind(theta) + sind(phi)) * (cosd(theta) + cosd(phi)) / 2;
% Store the calculated Q value and its corresponding time
Q = [Q, Q_value];
time = [time, tref];
end
end
% Plot the heat transfer (Q) against time
plot(time, Q);
xlabel('Time (20-minute intervals)');
ylabel('Heat Transfer (Q)');
title('Heat Transfer into the Cabin over Time');
Hope this helps.

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by