Indexing and iteration problem. Few lines holding me back. Please, HELP!

8 views (last 30 days)
I have attached the Delft.mat file. which contains the vectors listed in the code. I removed my progress in the coding so as not to make any complications.
Please note these following formula that will be inputted in the code
alpha = 0.2
am = 90 - theta
cos_AOI = cos(a) x cos (as) x cos (b - As) + sin (a) x sin (As)
Gdirect = DNI x cos_AOI
SVF = (1 + cosd(theta))/2
Gdiffuse = SVF x DHI
Galbedo = GHI x alpha x (1-SVF)
Gm = Gdirect + Gdiffuse + Galbedo
Em = Gm/1000
Em(a,b) is the sum of all values in Matrix Em.
I will be very glad if I am assisted.
% First we load the '.mat' file containing the hourly meteorological data of an entire year per hour into MATLAB's workspace
% The following lines are used to load the meterological data. The variable location_filename can be modified to experiment with data for other locations.
location_filename = 'Delft.mat'; %You can replace 'Delft.mat' by: 'Buenos_Aires.mat'; 'New_Dehli.mat'; or 'Vancouver.mat'
load(location_filename,'As','as','DHI','DNI','GHI')% Loading vectors 'As', 'as', 'DHI', 'DNI' and 'GHI'
alpha = ...; % Albedo coefficient
Am = 0:2:360; % Initializing PV Module Azimuth range from 0-360 degrees in steps of 2 degrees as a row vector
theta = 0:2:90; % Initializing PV Module tilt angle range from 0-90 degrees in steps of 2 degrees as a row vector
am = ...; % PV Module altitude range as a row vector
% Create
for a = 1:1:... %'a' is the index to iterate through vector 'am'
for b = 1:1:... %'b' is the index to iterate through vector 'Am'
% Direct Irradiance
cos_AOI = ... % Calculate the cosine of the angle of incidence for every hour of the year for a specific module tilt and orientation
Gdirect = ...
Gdirect(Gdirect<0) = 0; % This makes all the negative Direct Irradiance on the PV module as zero.
% Isotropic Diffuse Irradiance
SVF = ...
Gdiffuse = ...
% Irradiance due to effect of albedo
Galbedo = ...
% Total Irradinace
Gm = ...
% Total Energy incident on PV module surface for the whole year
Em(a,b) = ...
end % end for loop 2
end % end for loop 1
% Optimum tilt and orientation for PV module
% Hint: find Em_max to get the optimum tilt and orientation angle
Em_max = ...
% Opt_Am is the optimum orientation/Azimuth of the PV module, Opt_theta is the optimum tilt angle of the PV module
Opt_Am = ...
Opt_theta = ...
Below is my attepted code with your suggestion too.
alpha = 0.2; % Albedo coefficient
Am = 0:2:360; % Initializing PV Module Azimuth range from 0-360 degrees in steps of 2 degrees as a row vector
theta = 0:2:90; % Initializing PV Module tilt angle range from 0-90 degrees in steps of 2 degrees as a row vector
am = 90-theta; % PV Module altitude range as a row vector
% Create
for ind_am = 1:1:90 %'a' is the index to iterate through vector 'am'
for ind_Am = 1:1:360 %'b' is the index to iterate through vector 'Am'
a = am(ind_am);
b = Am(ind_Am);
% Direct Irradiance
cos_AOI = (cosd(a).*cosd(as).*cosd(b-As)) + (sind(a).*sind(As)); % Calculate the cosine of the angle of incidence for every hour of the year for a specific module tilt and orientation
Gdirect = DNI .* cos_AOI;
Gdirect(Gdirect<0) = 0; % This makes all the negative Direct Irradiance on the PV module as zero.
% Isotropic Diffuse Irradiance
SVF = (1 + cosd(theta))/2;
Gdiffuse = SVF .* DHI;
Gdiffuse(Gdiffuse<0)= 0;
% Irradiance due to effect of albedo
Galbedo = GHI * alpha * (1-SVF);
% Total Irradinace
Gm = Gdirect + Gdiffuse + Galbedo;
Em = Gm/1000;
% Total Energy incident on PV module surface for the whole year
Em(a,b) = sum(sum(Em));
end % end for loop 2
end % end for loop 1
The response I get is-
Index in position 2 is invalid. Array indices must be positive integers or logical values.
Error in solution (line 33)
Em(a,b) = sum(sum(Em));
  30 Comments
Varun Saini
Varun Saini on 31 Aug 2021
The row for December month is from 8017 to 8760. Shall be we have to make loop for this or we can load the date of file and mak For loop from 01 Dec to 31 Dec
Walter Roberson
Walter Roberson on 2 Oct 2021
If the data were read using readtable() and the DateT column were a datetime object, then
[y, m, d] = ymd(TheTable.DateT);
isdecember = m == 12;
DecemberTable = TheTable(isdecember, :);

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 16 Feb 2020
Em(a,b) your a and b are floating point numbers which cannot be used as indices.
Em(ind_am, ind_Am)
It is confusing that you use two names that differ just in the a vs A, by the way.
  2 Comments
Johnson
Johnson on 16 Feb 2020
Thank you everyone for the help. I have been able to run the whole code just now without errors but just that my "Em_max", "Opt_Am" and "Opt_theta" is wrong. I do not know where I may be getting it wrong. Could it be in wrong scripting of my formula?
I have updated it with the latest attempt which ran well but gave me wrong results. Thank you for your usual assistance.
% First we load the '.mat' file containing the hourly meteorological data of an entire year per hour into MATLAB's workspace
% The following lines are used to load the meterological data. The variable location_filename can be modified to experiment with data for other locations.
location_filename = 'Delft.mat'; %You can replace 'Delft.mat' by: 'Buenos_Aires.mat'; 'New_Dehli.mat'; or 'Vancouver.mat'
load(location_filename,'As','as','DHI','DNI','GHI')% Loading vectors 'As', 'as', 'DHI', 'DNI' and 'GHI'
alpha = 0.2; % Albedo coefficient
Am = 0:2:360; % Initializing PV Module Azimuth range from 0-360 degrees in steps of 2 degrees as a row vector
theta = 0:2:90; % Initializing PV Module tilt angle range from 0-90 degrees in steps of 2 degrees as a row vector
am = 90-theta; % PV Module altitude range as a row vector
% Create
for a = 1:1:length(am) %'a' is the index to iterate through vector 'am'
for b = 1:1:length(Am) %'b' is the index to iterate through vector 'Am'
%a = am(ind_am);
%b = Am(ind_Am);
% Direct Irradiance
cos_AOI = cosd(a).*cosd(as).*cosd(b-As) + sind(a).*sind(As); % Calculate the cosine of the angle of incidence for every hour of the year for a specific module tilt and orientation
Gdirect = DNI .* cos_AOI;
Gdirect(Gdirect<0) = 0; % This makes all the negative Direct Irradiance on the PV module as zero.
% Isotropic Diffuse Irradiance
SVF = (1 + cosd(theta))/2;
Gdiffuse = SVF .* DHI;
%Gdiffuse(Gdiffuse<0) = 0;
% Irradiance due to effect of albedo
Galbedo = GHI .* alpha .* (1-SVF);
%Galbedo(Galbedo<0) = 0;
% Total Irradinace
Gm = Gdirect + Gdiffuse + Galbedo;
% Total Energy incident on PV module surface for the whole year
Em(a,b) = (sum(sum(Gm)))/(1000);
end % end for loop 2
end % end for loop 1
% Optimum tilt and orientation for PV module
% Hint: find Em_max to get the optimum tilt and orientation angle
[Em_max,I] = max(Em(:))
% Opt_Am is the optimum orientation/Azimuth of the PV module, Opt_theta is the optimum tilt angle of the PV module
[am_row, Am_col] = ind2sub(size(Em),I)
Opt_Am = Am_col
Opt_theta = 90 - am_row

Sign in to comment.


Yonten
Yonten on 24 Oct 2022
alpha = 0.2; % Albedo coefficient
Am = 0:2:360; % Initializing PV Module Azimuth range from 0-360 degrees in steps of 2 degrees as a row vector
theta = 0:2:90; % Initializing PV Module tilt angle range from 0-90 degrees in steps of 2 degrees as a row vector
am = 90-theta; % PV Module altitude range as a row vector
% Create
for ind_am = 1:1:90 %'a' is the index to iterate through vector 'am'
for ind_Am = 1:1:360 %'b' is the index to iterate through vector 'Am'
a = am(ind_am);
b = Am(ind_Am);
% Direct Irradiance
cos_AOI = (cosd(a).*cosd(as).*cosd(b-As)) + (sind(a).*sind(As)); % Calculate the cosine of the angle of incidence for every hour of the year for a specific module tilt and orientation
Gdirect = DNI .* cos_AOI;
Gdirect(Gdirect<0) = 0; % This makes all the negative Direct Irradiance on the PV module as zero.
% Isotropic Diffuse Irradiance
SVF = (1 + cosd(theta))/2;
Gdiffuse = SVF .* DHI;
Gdiffuse(Gdiffuse<0)= 0;
% Irradiance due to effect of albedo
Galbedo = GHI * alpha * (1-SVF);
% Total Irradinace
Gm = Gdirect + Gdiffuse + Galbedo;
Em = Gm/1000;
% Total Energy incident on PV module surface for the whole year
Em(a,b) = sum(sum(Em));
end % end for loop 2
end % end for loop 1

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!