MATLAB: Undefined function or variable ‘betta’

undefined function or variable

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.

Best Answer

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;