Hi Folks,
I get the following error when I attempt to run this bit of code
"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.
Error in ode45 "
function yp = unforced1(t,y)
m=1;
k=100;
%s=.1;
c=2*m*0.1*(k/m)^(1/2);
y(2)=0 ;
y(1)=0.02;
yp = [y(2);(-((c/m)*y(2))-((k/m)*y(1)))];
tspan=0:0.01:4;
y0=[0.02;0];
[t,y]=ode45('unforced1',tspan,y0);
plot(t,y(:,1));
grid on
xlabel('time')
ylabel('Displacement')
title('Displacement Vs Time')
hold on;
end
Any ideas? Regards Bugatti

 Respuesta aceptada

Star Strider
Star Strider el 15 de Jul. de 2014

0 votos

You are calling ode45 inside your ODE function. This is likely the reason for the recursion limit warning.
Delete the function statement at the start, change the yp line to be an anonymous function:
unforced1 = @(t,y) [y(2);(-((c/m)*y(2))-((k/m)*y(1)))];
and change the call to ode45 to:
[t,y]=ode45(unforced1,tspan,y0);
When I made those changes it worked for me without errors (R2014a).

6 comentarios

bugatti79
bugatti79 el 15 de Jul. de 2014
Is there a way of doing it such that the following 2 lines is a separate M file and then the next batch of code calls this 'unforced1' function in another M file? See my attempt below:
function yp = unforced1(t,y)
yp = [y(2);(-((c/m)*y(2))-((k/m)*y(1)))];
The next bit of code calls the above function
tspan=[0 4];
y0=[0.02;0];
[t,y]=ode45('unforced1',tspan,y0);
plot(t,y(:,1));
grid on
xlabel(time)
ylabel(Displacement)
title(Displacement Vs Time)
hold on;
This does not work at the moment. Regards
Star Strider
Star Strider el 15 de Jul. de 2014
It’s certainly possible to do what you describe. You have to name your main script file (the script that includes the call to ode45) something other than unforced1.m. Save your ODE function to a separate file as unforced1.m. Then to run your code, run the main script. (All the files have to be in MATLAB’s path, preferably in your user directory.)
You can call ode45 with the code you wrote, or change the call to ode45 to:
[t,y]=ode45(@unforced1,tspan,y0);
that also works and is currently the preferred syntax.
bugatti79
bugatti79 el 15 de Jul. de 2014
It seems to run the code but now I just get a plot of a straight line instead of an oscillatory response...here is what i have
in an m file
function yp = unforced1(t,y)
m=1;
k=100;
c=2*m*0.1*(k/m)^(1/2);
y(2)=0 ;
y(1)=0.02;
yp = [y(2);(-((c/m)*y(2))-((k/m)*y(1)))];
In another m file
tspan=[0 4];
y0=[0.02;0];
[t,y]=ode45('unforced1',tspan,y0);
plot(t,y(:,1));
grid on
xlabel('time')
ylabel('Displacement')
title('Displacement Vs Time')
hold on;
all in the same directory.
Star Strider
Star Strider el 15 de Jul. de 2014
The reason it’s producing a straight line is because that’s what you told it to do in your unforced1 ODE function.
Remove these two lines from unforced1 and all should be well:
y(2)=0 ;
y(1)=0.02;
You correctly passed them as y0, but the ODE solver calculates y(1) and y(2) for every time step, and uses your ODE function to calculate the next increments. If you override that by supplying your own constant values of y(1) and y(2), it will never use the updates provided by ode45.
bugatti79
bugatti79 el 15 de Jul. de 2014
Ah ok! Great, thanks for your help. Appreciated.
Star Strider
Star Strider el 15 de Jul. de 2014
My pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Preguntada:

el 15 de Jul. de 2014

Comentada:

el 15 de Jul. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by