Quantcast
Viewing latest article 13
Browse Latest Browse All 130

Re: 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."

"Devdatt Thengdi" wrote in message <oo0u9g$3be$1@newscl01ah.mathworks.com>...
> Getting same error for minimizing sin(x) over 0 to 2*pi.
> Function File:
>
> function fun = SinExample(x3);
> x1 = 0;
> x2 = 2.*3.14178;
> fun = sin(x3);
> x3 = fminbnd(@SinExample,x1,x2)

Well, yes.

The SinExample function calls FMINBND. FMINBND calls SinExample. Then ...
The SinExample function calls FMINBND. FMINBND calls SinExample. Then ...
The SinExample function calls FMINBND. FMINBND calls SinExample. Then ...
The SinExample function calls FMINBND. FMINBND calls SinExample. Then ...
The SinExample function calls FMINBND. FMINBND calls SinExample. Then ...
The SinExample function calls FMINBND. FMINBND calls SinExample. Then ...
The SinExample function calls FMINBND. FMINBND calls SinExample. Then ...

There is no way to break out of that recursion.

> Solution File:
>
> x1 = 0;
> x2 = 2.*3.14178;
> set(0,'RecursionLimit',1100);
> x3 = fminbnd(@SinExample,x1,x2)
>
> Software crashes for anything beyond N=1100. No solution.

Yes. The error message you received when you encountered the recursion warned that setting the RecursionLimit too high could cause MATLAB to crash.

https://en.wikipedia.org/wiki/Stack_overflow

The correct solution is not to call FMINBND with @SinExample as the objective function from within the SinExample function itself. In general, calling any of the "function functions" like the ODE solvers or any of the optimization or zero finding routines on a function from within that function itself smells funny.

https://en.wikipedia.org/wiki/Code_smell

function y = SinExample
x1 = 0;
x2 = 2*pi; % I assume you have an (inexact) approximation to pi in your original code
y = fminbnd(@sin, x1, x2)

--
Steve Lord
slord@mathworks.com
To contact Technical Support, use the Contact Us link at the top of http://www.mathworks.com

Viewing latest article 13
Browse Latest Browse All 130

Trending Articles