Cycle depth limit 500 exceeded in MATLAB Function MATLAB Function. This could indicate an infinite cycle.

10 visualizaciones (últimos 30 días)
I have received this error message, but I am not using an infinite loop:
function [t_winding,pc,pe] = fcn(ta,tw,net_torque,motor_speed)
tm = 0.5*(ta+tw);
B = 1.32-0.0012*(tm-293);
i = 0.561*B*net_torque
R = 0.0575*(1+0.0039*(tw-293));
pc = 3*i^2*R;
pe = (9.602e-06*(B*motor_speed)^2)/R;
t_winding = 0.455*(pc+pe)+ta;
if (abs(t_winding-tw)) > 1
[t_winding,pc,pe] = fcn(ta,t_winding,net_torque,motor_speed);
end
end

Respuesta aceptada

Adam Danz
Adam Danz el 11 de Jul. de 2018
Editada: Adam Danz el 11 de Jul. de 2018
Your function is an infinite recursion. Within your fcn() function, you are calling the fcn() function. Your code reduces to:
function fcn(x)
fcn(x)
which means the fcn function is calling itself over and over again. It's like an M.C. Escher painting.
  2 comentarios
Guillaume
Guillaume el 11 de Jul. de 2018
There's nothing inherently wrong with recursion, and here there's a limit in place to stop the recursion. However, the limit is not reached before 500 recursions so matlab puts a stop to it.
Adam Danz
Adam Danz el 11 de Jul. de 2018
Editada: Adam Danz el 11 de Jul. de 2018
To add to Guillaume's point, if you intend to recurse, you could replace the conditional with a while-loop which is cleaner.
function [t_winding,pc,pe] = fcn(ta,tw,net_torque,motor_speed)
t_winding = tw+2;
while (abs(t_winding-tw)) > 1
tm = 0.5*(ta+tw);
B = 1.32-0.0012*(tm-293);
i = 0.561*B*net_torque;
R = 0.0575*(1+0.0039*(tw-293));
pc = 3*i^2*R;
pe = (9.602e-06*(B*motor_speed)^2)/R;
t_winding = 0.455*(pc+pe)+ta;
end

Iniciar sesión para comentar.

Más respuestas (1)

Guillaume
Guillaume el 11 de Jul. de 2018
Your function is recursive, it calls itself. You've put a condition to stop the recursion when abs(t_winding-tw) is less than 1, unfortunately after calling itself 500 times the condition still hasn't been met. It's most likely a bug in your code.
If you did mean to recurse more than 500 times, there is a way to increase that limit. However, matlab puts the limit in the first place to protect you as each recursion consumes quite a lot of memory as it needs to create a new set of all the variables in your function.
To start debugging what is actually happening with your recursion, I would remove the semi-colon on the t_winding = ... line, so you can see how it evolves. If it doesn't decrease then you indeed have an infinite loop.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by