필터 지우기
필터 지우기

how to apply if statement with equations

조회 수: 7 (최근 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 CenterFile Exchange에서 Pulse and Transition Metrics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by