MATLAB: 2nd Order Diff Equation

differential equations

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

Best Answer

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