# How to get rid of the error message set(0,'Rec​ursionLimi​t',N) ?

22 views (last 30 days)
Debajyoti Choudhuri on 24 May 2015
Commented: Walter Roberson on 25 May 2015
Error message:
??? Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N)
to change the limit. Be aware that exceeding your available stack space can
Hi Jan Simon,
The code is as follows.
1) The code below is run for an output
clear all
close all
clc
a=1;
b=1;
c=1;
dvec=[1 0 0]; %direction vector for prolate spheroid
uinfinity=[1 0 0]'; %flow at infinity
N=8;
% mesh for collocation
mesh='triangles'; % 'triangles', 'cuboid' or 'tensorproduct'
meshparam=5; % this parameter changes depending on 'mesh'
% 'triangles': number of subdivisions for triangle mesh (n yields grid with 2^(2*(n+2)) points)
% 'cuboid': size of face-subdivisions for cuboid grid (n yields grid with 6*n^2 points provided n is even. If n is odd, poles are removed)
% 'tensorproduct': number of theta-subdivisions for
% tensorproduct grid (n yields grid with 2n^2 points)
thDragGrid=15; % mesh for drag integration
phiDragGrid=2*thDragGrid;
tic;F=CalculateCoefsSquimmer(a,b,c,uinfinity,N,mesh,meshparam);toc
2) The subprogram CalculateCoefsSquimmer(a,b,c,uinfinity,N,mesh,meshparam) is called in the above code in 1)

Walter Roberson on 24 May 2015
• You could write a program that provided an interface to MATLAB and sent everything to MATLAB and got back all of the results, except that it recognized that particular error message and discarded it and instead showed you the message, "Time to go outside for a break!". To be more effective, it should also give you that message randomly so that any one time that it gave you the message you would not know whether it was a random occurrence or if it was hiding the error message.
• You could use a different language for the MATLAB user interface, so that it gave you an error message with the same meaning but with different wording
• You could give the command set(0,'RecursionLimit',2000) and then when it next gave you the message, it would be complaining that the maximum limit of 2000 was reached, which would at least be a different message.
I do not recommend any of those approaches. I recommend that you leave the error message alone, and instead fix your code so that it does not call itself accidentally, and that if it calls itself deliberately that you can prove that the chain of calls must end.
The most common way that people call their own code without realizing it, is that people accidentally name their own code something like "plot.m" to mean "My homework assignment is to draw a plot", and then inside their code they intend to call the MATLAB routine plot(). However, MATLAB searches for names first in the current routine and then in the current file, so if your own file is plot.m then MATLAB would try to use your own file to handle the plot() call, rather than "knowing" that you mean you want to call the MATLAB routine. The fix is to not name files with the same name as any of the MATLAB routines.

Jan on 24 May 2015
You cannot avoid the error in general.
It appears very often, when a function calls itself recursively without intention. In this forum you find a lot of examples, where functions to be integrated by ODE45 call the integrator instead of the other way around. So these are simply mistakes.
Recursive functions can be solved by non-recursive code also in every case. This is more efficient considering the stack size im Matlab. So a soultion would be to avoid recursion. But sometimes recursive code is such nice, that the programmer hesitates to re-organize the code. Then the maximum depth of recursion must be adjusted to the possible size of the problem. But as the message warns you already: If the stack size is exhausted, Matlab might not be able to display a message anymore and you can only see a silent crash. This is extremely annoying and horrible to debug.
Please post the relevant part of the code. Then we could decide if it is a programming error or an intentional recursion and a specific suggestion is possible.
Walter Roberson on 25 May 2015
CalculateCoefsSquimmer is not a MATLAB routine, so we will need to see your source for it.