MATLAB Answers

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

47 views (last 30 days)
Johnson
Johnson on 16 Feb 2020
Commented: Cristian Duran on 9 Sep 2020 at 2:12
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));

  25 Comments

Johnson
Johnson on 20 Feb 2020
Good day Sindar, I really appreciate the effort you have put in helping resolve this problem to a great extent.
You always responded to every question I asked no matter how they appeared. Atleast, I now have the code run all thanks to you. You’re a good person and willing to help. May you find peace and favour in all your doings. Thank you very much, Sir! Let me review the formulas again but I will do with what we result I get.
Johnson
Johnson on 21 Feb 2020
Hi Sindar, there was a switch in values of cos_AOI. It was my mistake. That was the problem. The results would have been correct after your corrections.
I now have the right results. Thanks once more.
Cristian Duran
Cristian Duran on 9 Sep 2020 at 2:12
can you upload the ode with the right results? I am interested on your code

Sign in to comment.

Answers (1)

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.

  1 Comment

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.

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!