how to apply if statement with equations

8 ビュー (過去 30 日間)
Danish Iqhwan Rohaizad
Danish Iqhwan Rohaizad 2022 年 3 月 22 日
回答済み: Star Strider 2022 年 3 月 23 日
Hey everyone !
I would like to request assisstance on this coding of mine. What im trying to achieve here is that for my value of beta2, by using the if statement I would like Matlab to run the codes to achieve different values for beta2. Lets say if Wtheta2 > 0, then beta2 will be calculated using formula beta2 = -(abs(acosd(Cm2./W2))). Meanwhile, if Wtheta < 0, hence beta2 will be calculated using the formula beta2 = abs(acosd(Cm2./W2)). However, when i run the code, it seems matlab does not recognize the beta2 variable as it is supposed to.
Cm2 = [80.105 81.868 97.935 116.429];
W2 = [82.564 83.693 98.029 119.103]; %Relative Velocity Inlet Rotor
Wtheta2 = [19.99 17.382 -4.308 -25.094] ; %Tangential relative velocity inlet rotor
% Relative Flow Angle @ Inlet Rotor, beta2
if (Wtheta2 > 0)
beta2 = -(acosd(Cm2./W2));
elseif (Wtheta2 < 0)
beta2 = abs(acosd(Cm2./W2));
end

採用された回答

Star Strider
Star Strider 2022 年 3 月 23 日
Using a ‘logical indexing’ approach instead of the if block —
Cm2 = [80.105 81.868 97.935 116.429];
W2 = [82.564 83.693 98.029 119.103]; %Relative Velocity Inlet Rotor
Wtheta2 = [19.99 17.382 -4.308 -25.094] ; %Tangential relative velocity inlet rotor
beta2 = (Wtheta2<0) .* (-acosd(Cm2./W2)) + (Wtheta2 > 0) .* (abs(acosd(Cm2./W2))); % One-Line Statement
beta2_fcn = @(Wtheta2) (Wtheta2<0) .* (-acosd(Cm2./W2)) + (Wtheta2 > 0) .* (abs(acosd(Cm2./W2))); % Anonymous Function
figure
plot(Wtheta2, beta2)
grid
xlabel('Wtheta2')
ylabel('beta2')
figure
plot(Wtheta2, beta2_fcn(Wtheta2))
grid
xlabel('Wtheta2')
ylabel('beta2')
.

その他の回答 (1 件)

Arif Hoq
Arif Hoq 2022 年 3 月 23 日
編集済み: Arif Hoq 2022 年 3 月 23 日
you have only 2 conditions. greater/equal to 0 or less than 0. so try to use if-else function, not if-elseif.
Cm2 = [80.105 81.868 97.935 116.429];
W2 = [82.564 83.693 98.029 119.103]; %Relative Velocity Inlet Rotor
Wtheta2 = [19.99 17.382 -4.308 -25.094] ; %Tangential relative velocity inlet rotor
% Relative Flow Angle @ Inlet Rotor, beta2
if Wtheta2 >= 0
beta2 = -(acosd(Cm2./W2));
else
beta2 = abs(acosd(Cm2./W2));
end
disp(beta2)
14.0186 11.9872 2.5093 12.1639
  2 件のコメント
Danish Iqhwan Rohaizad
Danish Iqhwan Rohaizad 2022 年 3 月 23 日
First of all thanks Arif for responding. If lets say the value vector of 2.5093 and 12.1639 should be negative, how should i progress from there ?
Thanks in advance !
Arif Hoq
Arif Hoq 2022 年 3 月 23 日
編集済み: Arif Hoq 2022 年 3 月 23 日
i don't now why you are expecting that. but simply you can do that
output=[beta2(1) beta2(2) -beta2(3) -beta2(4)]
or
output=[beta2([1 2]) -beta2([3 4])]

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

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by