Array division giving incorrect answer?

5 Ansichten (letzte 30 Tage)
ck120812
ck120812 am 22 Nov. 2015
Kommentiert: Stephen23 am 24 Nov. 2015
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
  2 Kommentare
Image Analyst
Image Analyst am 22 Nov. 2015
Are you sure you're not overlooking a 1e3 in the display somewhere? Please attach book1.xlsx so we can run your code.
ck120812
ck120812 am 22 Nov. 2015
Oh, I am sorry. Here is the book. As for the overlooking, it's definitely possible... but why would it give the correct answer when just looking at the last couple?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 22 Nov. 2015
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
  3 Kommentare
Image Analyst
Image Analyst am 24 Nov. 2015
Please post a screenshot of your command window. Make sure you include enough of the command window above the variable values so I can see what came before it.
Stephen23
Stephen23 am 24 Nov. 2015
"The numbers displayed in my command window were incorrect" is very unlikely. Most likely you missed seeing the 1e3 multiplier just above the values. Post a screenshot of your whole command window for us to look at.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Identification finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by