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')

Respuestas (1)

Cris LaPierre
Cris LaPierre el 18 de Mzo. de 2021
Editada: Cris LaPierre el 18 de Mzo. de 2021

0 votos

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 comentarios

Anna Taylor
Anna Taylor el 18 de Mzo. de 2021
**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.
Cris LaPierre
Cris LaPierre el 18 de Mzo. de 2021
Editada: Cris LaPierre el 18 de Mzo. de 2021
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

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Preguntada:

el 18 de Mzo. de 2021

Editada:

el 18 de Mzo. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by