how to apply if statement with equations
9 次查看(过去 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
0 个评论
采纳的回答
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')
.
0 个评论
更多回答(1 个)
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)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!