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

回答 (1 件)

Cris LaPierre
Cris LaPierre 2021 年 3 月 18 日
編集済み: Cris LaPierre 2021 年 3 月 18 日

0 投票

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 件のコメント

Anna Taylor
Anna Taylor 2021 年 3 月 18 日
**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 2021 年 3 月 18 日
編集済み: Cris LaPierre 2021 年 3 月 18 日
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

サインインしてコメントする。

カテゴリ

質問済み:

2021 年 3 月 18 日

編集済み:

2021 年 3 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by