Maximum recursion limit of 500 reached.

I am trying to plot with this code below but keep getting a Maximum recursion limit of 500 reached error.
function yp = nonlinear (t,y)
e = .2;
yp(1) = y(2);
yp(2) = (-y(1)-e*y(1)^3);
tspan = [0 20];
y0 = [0;0];
[t,y] = ode45('nonlinear', tspan, y0);
plot (t,y(:,1))
grid
xlabel ('time')
ylabel ('u')
title ('u vs t')

Answers (1)

Cris LaPierre
Cris LaPierre on 18 Mar 2021
Edited: Cris LaPierre on 18 Mar 2021
You have given your plotting function and your odefunc the same name. This is causing ode45 to call your function recursively (call itself).
Change the name of either give your odefunc or you plotting function.

2 Comments

**ok so i changed the name of the plotting function
function yp = nonlinear (t,y)
e = .2;
yp(1) = y(2);
yp(2) = (-y(1)-e*y(1)^3);
tspan = [0 20];
y0 = [0;0];
[t,y] = ode45('non', tspan, y0);
plot (t,y(:,1))
grid
xlabel ('time')
ylabel ('u')
title ('u vs t')
**But now i'm getting new errors:
Error using feval
Unrecognized function or variable 'non'.
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in nonlinear (line 7)
[t,y] = ode45('non', tspan, y0);
**I'm really sorry. I've never used MATLAB before so I'm just lost in what I'm doing.
I've never used MATLAB before so I'm just lost in what I'm doing.
I suggest going through MATLAB Onramp. It doesn't cover odes, but it will at least give you the basics, which you will need for a foundation to build on.
Now the error is that you have not defined an ode to solve, at least not the way MATLAB is expecting. See this example on the ode45 documentation page.
I think you want something like this.
tspan = [0 20];
y0 = [0;0];
[t,y] = ode45(@non, tspan, y0);
plot (t,y(:,1))
grid
xlabel ('time')
ylabel ('u')
title ('u vs t')
function yp = non(t,y)
yp = zeros(2,1);
e = .2;
yp(1) = y(2);
yp(2) = (-y(1)-e*y(1)^3);
end

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 18 Mar 2021

Edited:

on 18 Mar 2021

Community Treasure Hunt

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

Start Hunting!