Pointer with handle function. Error Maximum recursion limit of 500 reached

1 view (last 30 days)
Hi guys, I'll be very thankful if someone helps me with this problem: "??? Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N) to change the limit. Be aware that exceeding your available stack space can crash MATLAB and/or your computer." I am trying to create one recursive function with pointer using handle function. This is my code:
function [f]=fun_f(Ms,S)
f=fun_f(Ms,S); x=Ms(:,2); y= Ms(:,5); z= Ms(:,8); f={@(cx,cy,cz) fun_f(s,x,y,z,cx,cy,cz)};% function handle cell-array
for i=1:50
f=fun_f(f,x,y,z,@cx,@cy,@cz)
end
end
function [S,Ms]=fun_s(s,x,y,z,cx,cy, dcx,dcy)
cz=s; s=s'; dcx=dcx'; dcy=dcy';cx=cx';cy=cy'; m=1:1:8; s = meshgrid(s(:,1),1:size(s,1)); s = s(:); cx = meshgrid(cx(:,1),1:size(cx,1)); cx= cx(:); cy = meshgrid(cy(:,1),1:size(cy,1)); cy= cy(:); dcx = meshgrid(dcx(:,1),1:size(dcx,1)); dcx=dcx(:); dcy = meshgrid(dcy(:,1),1:size(dcy,1)); dcy=dcy(:);
Ms = meshgrid(s(:),1:size(m,2)); Ms=Ms'; Ms(:,2 ) = x(:)+ Ms(:,2 ); Ms(:,3 ) = cx(:)+ Ms(:,3 ); Ms(:,4 ) = dcx(:)+ Ms(:,4 ); Ms(:,5 ) = y(:)+ Ms(:,5 ); Ms(:,6 ) = cy(:)+ Ms(:,6 ); Ms(:,7 ) = dcy(:)+ Ms(:,7 ); Ms(:,8 ) = z(:)+ Ms(:,8 ); S = (Ms(:,2 )-Ms(:,3)).*Ms(:,4)+(Ms(:,5)-Ms(:,6)).*Ms(:,7)+Ms(:,8); end
clear all; close all; r =1; [M1] =[];[Mt] =[];[x] =[];[y] =[];[z] =[]; C_1=[]; Z =0; cz =linspace(0,4,10); s =cz; cx =cz.^2+cz; cy =sin(cz)*0; order=length(cx)-1; [dcx]=polyderiva(polycenter(s, cx, order),s); [dcy]=polyderiva(polycenter(s, cy, order),s);
for l=1:length(cz) N = size(cz,2); [M] =TranslationMatrix(dcx(l),dcy(l)); [X,Y]=circle(r,length(cz)); C =[cx(l); cy(l); cz(l)]'; M_1 =(TranslationMatrix(dcx(l),dcy(l))*[X ;Y; zeros(length(X),1)'])'+repmat(C,N,1); M1 =[M1; M_1]; x =M1(:,1); y =M1(:,2); z =M1(:,3); plot3(M1(:,1),M1(:,2),M1(:,3));hold on;axis square; plot3(cx,cy,cz, 'r'); view([0 0]);xlim([0 20]);ylim([0 20]);zlim([0 20]); pause(0.5); end
[S,Ms]=fun_s(s,x,y,z,cx,cy, dcx,dcy); [f]=fun_f(Ms,S)
Thankyou

Answers (1)

Naga
Naga on 28 Jan 2025
It looks like your function is getting stuck in a loop because it keeps calling itself without a way to stop. MATLAB has a limit on how many times a function can call itself, and you're hitting that limit.
To fix it add a bases case to ensure the function has a termination condition:
function [f] = fun_f(Ms, S, depth)
if depth <= 0
f = []; % Base case result
return;
end
% Recursive call with depth decrement
f = fun_f(Ms, S, depth - 1);end
end
Increase the recursion limit cauticiously Temporarily increase the limit if necessary just be careful, too much recursion can crash things!
set(0, 'RecursionLimit', 5000);
Consider using iterative methods to avoid deep recursion. By implementing a base case and cautiously adjusting the recursion limit, you can prevent the error without risking a stack overflow.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!