Undefined function or variable 'betta'

Matlab just ignore the if else statement. I tried replacing betta with different words such as x3 but it is still the same. I am able to create alpha in the workspace but not betta. Is there any way for me to fix this problem?
Thank you.

 채택된 답변

Walter Roberson
Walter Roberson 2018년 8월 15일

2 개 추천

You have
if (x2 < -28)
betta = (-0.0002.*(x2)) + 2.0952
elseif (x2 >= -28) & (x2 < 54)
betta = ((-8e-9).*((x2)^5)) + ((5e-7).*((x2)^4)) + ((1e-5).*((x2)^3)) + (0.0011.*((x2)^2)) - (0.0377.*(x2)) + 1.7084
elseif (x2 >= 54)
betta = (-0.0001.*(x2)) + 0.002
end
but your x2 is a vector.
When you write
if (x2 < -28)
the meaning is the same as
if all(x2 < -28)
but some of your values are not less than -28, so the test fails. Then
elseif (x2 >= -28) & (x2 < 54)
is the same as
elseif all((x2 >= -28) & (x2 < 54))
and that fails because it is not true for some of the values. Then
elseif (x2 >= 54)
is the same as
elseif all(x2 >= 54)
and that fails because it is not true for all of the values.
You either need to loop or to use logical indexing.
mask1 = (x2 < -28);
mask2 = (x2 >= -54);
mask3 = ~(mask1 | mask2);
betta(mask1) = (-0.0002.*(x2(mask1))) + 2.0952;
betta(mask2) = (-0.0001.*(x2(mask2))) + 0.002;
betta(mask3) = ((-8e-9).*((x2(mask3)).^5)) + ((5e-7).*((x2(mask3)).^4)) + ((1e-5).*((x2(mask3)).^3)) + (0.0011.*((x2(mask3)).^2)) - (0.0377.*(x2(mask3))) + 1.7084;

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

질문:

CAL
2018년 8월 15일

댓글:

CAL
2018년 8월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by