how to apply if statement with equations

7 次查看(过去 30 天)
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
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])]

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by