Info

此问题已关闭。 请重新打开它进行编辑或回答。

undefined function inside two if statements

1 次查看(过去 30 天)
%Currently I have the code below and Y isn't returning any values. When I try displaying Y outside of the statements Matlab returns that Y is an undefined function.
%How can I output a value for Y? I'm sorry for the basic question, new to Matlab.
%X_1 is just a number and RoundedBoverT & RoundedBeta are matricies
if RoundedBoverT == 2
if RoundedBeta == 0.5
Y = 1.47*(X_1*X_1*X_1*X_1*X_1*X_1) - 5.5755*(X_1*X_1*X_1) + 7.8174*(X_1*X_1) - 4.9808*(X_1) + 1.8211;
disp(Y)
elseif RoundedBeta == 0.6
Y = 7.373*(X_1*X_1*X_1*X_1*X_1*X_1) - 37.301*(X_1*X_1*X_1*X_1*X_1) + 75.133*(X_1*X_1*X_1*X_1) - 77.111*(X_1*X_1*X_1) + 43.138*(X_1*X_1) - 13.169*(X_1) + 2.454
disp(Y)
elseif RoundedBeta == 0.9
Y = -1.5143*(X_1*X_1*X_1*X_1*X_1) + 7.4603*(X_1*X_1*X_1*X_1) - 14.377*(X_1*X_1*X_1) + 13.637*(X_1*X_1) - 6.232*(X_1) + 1.8101
disp(Y)
end
end

回答(2 个)

David Hill
David Hill 2020-2-27
Do not know what you want when you say if RoundedBoverT == 2 ...
if nnz(RoundedBoverT == 2)%do you mean any element of matrix ==2?
if nnz(RoundedBeta == 0.5)%do you mean any element of matrix == .5?
Y = 1.47*X_1^6 - 5.5755*X_1^3 + 7.8174*X_1^2 - 4.9808*X_1 + 1.8211%no ; will print value of y to work space
elseif nnz(RoundedBeta == 0.6)
Y = 7.373*X_1^6 - 37.301*X_1^5 + 75.133*X_1^4 - 77.111*X_1^3 + 43.138*X_1^2 - 13.169*X_1 + 2.454
elseif nnz(RoundedBeta == 0.9)
Y = -1.5143*X_1^5 + 7.4603*X_1^4 - 14.377*X_1^3 + 13.637*X_1^2 - 6.232*X_1 + 1.8101
end
end
  4 个评论
David Hill
David Hill 2020-2-27
Then do what with the calculations? Store them into matrix Y? I think this might be what you are looking for.
y=zeros(size(RoundedBoverT));
y(RoundedBeta(RoundedBoverT==2)==.5)=polyval([1.47,0,0,-5.5755,7.8174,-4.9808,1.8211],X_1);
y(RoundedBeta(RoundedBoverT==2)==.6)=polyval([7.373,-37.301,75.133,-77.111,43.138,-13.169,2.454],X_1);
y(RoundedBeta(RoundedBoverT==2)==.9)=polyval([-1.5143,7.4603,-14.377,13.637,-6.232,1.8101],X_1);
Joshua Prosper
Joshua Prosper 2020-2-27
thank you very much, i appologise for the poor explination, i havent fully figured out my logic yet

Guillaume
Guillaume 2020-2-27
"RoundedBoverT is a matrix and I want the if statement to use the correct equation depending on each value in the matrix"
You need to loop over each element of RoundedBoverT:
Y = zeros(size(RoundedBoverT)); %always preallocate the output if you're filling it in a loop
for idx = 1:numel(RoundedBoverT)
if RoundedBoverT(idx) == 2
%I'm assuming that RoundedBeta is the same size as RoundedBoverT, you haven't said anything about it
if RoundedBeta(idx) == 0.5
Y(idx) = polyval([1.47, 0, 0, -5.5755, 7.8174, -4.9808, 1.8211], X_1);
elseif RoundedBeta == 0.6
Y(idx) = polyval([7.373, -37.301, 75.133, -77.111, 43.138, -13.169, 2.454], X_1);
elseif RoundedBeta == 0.9
Y(idx) = polyval([0, -1.5143, 7.4603, -14.377, 13.637, -6.232, 1.8101], X_1);
else
%never end on an elseif
%what to do when it's neither 0.5, 0.6 or 0.9
error('case undefined');
end
else
%what to do when it's not 2?
error('case undefined');
end
end
Note that your tests involves exact equality. Be aware that:
>> (0.1 + 0.1 + 0.1) == 0.3
ans =
logical
0
is false in matlab due to floating point precision. That's another topic altogether.

Community Treasure Hunt

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

Start Hunting!

Translated by