how to apply if statement with equations

9 views (last 30 days)
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

Accepted Answer

Star Strider
Star Strider on 23 Mar 2022
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')
.

More Answers (1)

Arif Hoq
Arif Hoq on 23 Mar 2022
Edited: Arif Hoq on 23 Mar 2022
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 Comments
Danish Iqhwan Rohaizad
Danish Iqhwan Rohaizad on 23 Mar 2022
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 on 23 Mar 2022
Edited: Arif Hoq on 23 Mar 2022
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])]

Sign in to comment.

Categories

Find more on Programming 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!