why it does not go to if statement and not only in else?(variable 'omega' might be set by a nonscalar operator)

1 回表示 (過去 30 日間)
clc
Longitude=39.1925; % East
Latitude=21.4858; %North
LST=[6:18]
for d=[21 52 80 111 141 172 202 233 264 294 325 355]
omega=15.*(LST-12)
delta=23.45.*sind((360/365).*(284+d))
alpha=asind(cosd(Latitude).*cosd(delta).*cosd(omega)+sind(Latitude).*sind(delta))
thetaz=90-alpha
if omega>0
gammas=360-acosd((sind(delta).*cosd(Latitude)-cosd(delta).*sind(Latitude).*cosd(omega))./cosd(alpha))
else
gammas=acosd((sind(delta).*cosd(Latitude)-cosd(delta).*sind(Latitude).*cosd(omega))./cosd(alpha))
end
plot(gammas,alpha,'linewidth',2);
hold on
xlabel('Solar Azimuth','fontsize',14)
ylabel('Solar Elevation (Altitude)','fontsize',14)
title('Sun Path Chart','fontsize',14)
fh = figure(1);
set(fh, 'color', 'white');
end

回答 (2 件)

Geoff Hayes
Geoff Hayes 2022 年 3 月 15 日
@Abdulmuti Ehsan omega is an array
omega=15.*(LST-12)
where
LST=[6:18]
. So for the condition
if omega>0
what do you mean this to be? Should all elements in omega be greater than zero (which won't be the case) or do you want to be iterating over omega (somehow)?
  2 件のコメント
Abdulmuti Ehsan
Abdulmuti Ehsan 2022 年 3 月 15 日
I have two equations for gammas when omega is greater then zero (positive) the program should use the the first equation otherwise the program will use the other equatuion
Geoff Hayes
Geoff Hayes 2022 年 3 月 15 日
@Abdulmuti Ehsan - but where in your code do you iterate over omega?

サインインしてコメントする。


Steven Lord
Steven Lord 2022 年 3 月 15 日
From the documentation page for the if keyword: "if expression, statements, end evaluates an expression, and executes a group of statements when the expression is true. An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric). Otherwise, the expression is false."
That means if your expression results in a non-scalar value the body of the if statement is only executed if the expression results in a non-empty result and all the elements in that result are true. In your case, with if omega > 0, if any of the values of omega are not strictly greater than 0, the if statement is not satisfied.
What are your omega values? Are any of them not greater than 0?
LST=[6:18]
LST = 1×13
6 7 8 9 10 11 12 13 14 15 16 17 18
omega=15.*(LST-12)
omega = 1×13
-90 -75 -60 -45 -30 -15 0 15 30 45 60 75 90
Yes, there are several that are not greater than 0.
You could iterate over the elements in omega or you could use logical indexing to operate first on those values of omega that are positive and then those that aren't.
y = NaN(size(omega));
positive = omega > 0;
y(positive) = omega(positive).^2;
negative = omega < 0;
y(negative) = sind(omega(negative));
format longg
y.' % Transpose to make it easier to see more of y at once
ans = 13×1
1.0e+00 * -1 -0.965925826289068 -0.866025403784439 -0.707106781186548 -0.5 -0.258819045102521 NaN 225 900 2025

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by