Array division giving incorrect answer?
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
ck120812
el 22 de Nov. de 2015
Comentada: Stephen23
el 24 de Nov. de 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 comentarios
Image Analyst
el 22 de Nov. de 2015
Are you sure you're not overlooking a 1e3 in the display somewhere? Please attach book1.xlsx so we can run your code.
Respuesta aceptada
Image Analyst
el 22 de Nov. de 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 comentarios
Image Analyst
el 24 de Nov. de 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
el 24 de Nov. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Data Type Identification en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!