Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

undefined function inside two if statements

4 visualizaciones (últimos 30 días)
Joshua Prosper
Joshua Prosper el 27 de Feb. de 2020
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
%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

Respuestas (2)

David Hill
David Hill el 27 de Feb. de 2020
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 comentarios
David Hill
David Hill el 27 de Feb. de 2020
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 el 27 de Feb. de 2020
thank you very much, i appologise for the poor explination, i havent fully figured out my logic yet

Guillaume
Guillaume el 27 de Feb. de 2020
"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.

La pregunta está cerrada.

Community Treasure Hunt

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

Start Hunting!

Translated by