Undefined function or variable 'betta'

2 ビュー (過去 30 日間)
CAL
CAL 2018 年 8 月 15 日
コメント済み: CAL 2018 年 8 月 15 日
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 日
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;
  1 件のコメント
CAL
CAL 2018 年 8 月 15 日
thank you very much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDevelop Apps Using App Designer についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by