MATLAB: Array division giving incorrect answer

array divisionMATLAB

Hello, I am working on a code that reads in an excel sheet and creates arrays from the columns. These arrays are then manipulated together as seen below.
if true
filename = 'Book1.xlsx';
n = xlsread(filename,'B:B');
hour_angle = xlsread(filename,'D:D');
lat = 40;
slope = 32;
azimuth_angle=0;
declination=23.45*sind(360/365*(284+n));
cos_angleofincidence=sind(declination).*sind(lat).*cosd(slope)-sind(declination).*cosd(lat).*sind(slope).*cosd(azimuth_angle)+cosd(declination).*cosd(lat).*cosd(slope).*cosd(hour_angle)+cosd(declination).*sind(lat).*sind(slope).*cosd(azimuth_angle).*cosd(hour_angle)+cosd(declination).*sind(slope).*sind(hour_angle);
cos_zenithangle=cosd(lat).*cosd(declination).*cosd(hour_angle)+sind(lat).*sind(declination);
Rb=cos_angleofincidence./cos_zenithangle
end
(To simplify my explanation, I am just going to use the last number in the array as an example)
From cos_angleofincidence, the last number in the array is given as -0.9655. The last number in cos_zenithangle comes out to be -0.9567. As I understand it, Rb should have them dividing as an array because of the "./", so it should be -.9655/-.9567. The answer I get instead is 0.0010.
If I leave all lines in the code the same, but change Rb to look at only that last couple as below:
if true
% code
Rb=cos_angleofincidence(8760)./cos_zenithangle(8760)
end
I instead get an answer of Rb = 1.0092 for said couple (which is the answer I am looking for).
Can anyone please explain what is going on? I really don't understand what is changing between the two. I would really appreciate any help you guys can give.
Thank you so much!
Caitlin

Best Answer

I got rid of one xlsread() to save time and extracted the arrays from the single array.
filename = 'Book1.xlsx';
cols2to4 = xlsread(filename,'B:D');
n = cols2to4(:, 1);
hour_angle = cols2to4(:, 3);
lat = 40;
slope = 32;
azimuth_angle=0;
declination= 23.45 * sind(360 / 365 * (284+n));
cos_angleofincidence = sind(declination) .* sind(lat) .* cosd(slope) - ...
sind(declination) .* cosd(lat) .* sind(slope) .* cosd(azimuth_angle) + ...
cosd(declination) .* cosd(lat) .* cosd(slope) .* cosd(hour_angle)+...
cosd(declination) .* sind(lat) .* sind(slope) .* cosd(azimuth_angle) .* cosd(hour_angle)+...
cosd(declination) .* sind(slope) .* sind(hour_angle);
cos_zenithangle=cosd(lat) .* cosd(declination) .* cosd(hour_angle)+sind(lat) .* sind(declination);
Rb = cos_angleofincidence ./ cos_zenithangle
Rb(isinf(Rb)) = 0;
plot(Rb, 'b-');
numerator=cos_angleofincidence(end)
denominator=cos_zenithangle(end)
lastRatio=cos_angleofincidence(end)./cos_zenithangle(end)
I get this:
numerator =
-0.965536659588656
denominator =
-0.956742071639009
lastRatio =
1.00919222454029
So I am not able to reproduce your problem of getting 0.0010. Try issuing these commands and see if that fixes it:
format long g
format compact