Maximum value is incorrect
3 views (last 30 days)
Show older comments
Sarah Kneer
on 23 Dec 2020
Commented: Sarah Kneer
on 23 Dec 2020
I have this code and I want to calculate the maximum value of alpha but it's giving me the minimum value:
phi = 53;
h = 1/24:1/24:1;
d = 100:100:365;
for i = 1:1:length(d)
delta(i) = -23.45 * cosd((360/365) * (d(i)+10));
for j = 1:1:length(h)
h_a(j) = 360 * (h(j)-0.5);
alpha(i,j) = asind(((sind(phi) * sind(delta(i))) + (cosd(phi) * cosd(delta(i)) * cosd(h_a(j)))));
theta(i,j) = 90 - alpha(j);
if h <= 0.5
beta(i,j) = -acosd((((sind(delta(i)) * cosd(phi)) - (cosd(h_a(j)) * cosd(delta(i)) * sind(phi)))/sind(theta(j))));
else
beta(i,j) = acosd((((sind(delta(i)) * cosd(phi)) - (cosd(h_a(j)) * cosd(delta(i)) * sind(phi)))/sind(theta(j))));
end
end
end
a = max(alpha(i,j));
fprintf('%.1f\n',a);
disp(a)
0 Comments
Accepted Answer
Walter Roberson
on 23 Dec 2020
After your for j loop, j will have a value that is the last value assigned to it in the loop; for j=1:1:length(h) will leave j=length(h) after the loop. .... quoting myself from an earlier response to you.
After your for i loop, i will have a value that is the last value assigned to it in the loop; for i=1:1:length(d) will leave i=length(d) after the loop.
So after the loop, i will be length(d) and j will be length(h) . Both of those are scalars.
So when you ask max(alpha(i,j)) you are asking for max() of a scalar. Which is just going to give you the scalar back.
You need to decide whether you want the maximum for each row, or for each column, or over the entire array. Please read the documentation for max() and pay attention to the "dim" parameter.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!