MATLAB: Plotting the results of 2 ODE45 in the same figure

MATLABode45plotsame figure

Hello all,
I would like to plot the results of 2 ODE´s in the same figure however, I keep getting an error saying:
the first ODE is ok, but the second gives me the above error.
I saved 2 .m files
1° file: I named it forced.m
function yp = forced(t,y)
yp = [y(2);(((0.01/0.5)*cos(6.28*t))-((1/0.5)*(y(2)^2)-((20/0.5)*y(1)))];
2° file: I named it forcedd.m
function yp = forcedd(t,y)
yp = [y(2);(((0.01/0.5)*cos(6.28*t))-((0.011/0.5)*y(2)-((20/0.5)*y(1)))];
And then I wrote the main file to solve it
tspan = [0:1800];
y0 = [0;0];
[t,y] = ode45 ('forced',tspan,y0);
plot(t,y(:,1));
grid on
xlabel ('time')
ylabel ('Displacement')
title ('System Response')
hold on;
[t,y] = ode45 ('forcedd',tspan,y0);
plot(t,y(:,1));
hold off;
I just get the result of the first ODE, I tried changing variables but without sucess.
Any help ?
Thanks in Advance

Best Answer

You need to check the placing of your parentheses in the forcing functions. Try
tspan = 0:1800;
y0 = [0;0];
[t,y] = ode45 (@forced,tspan,y0);
plot(t,y(:,1));
grid on
xlabel ('time')
ylabel ('Displacement')
title ('System Response')
hold on
[t,y] = ode45 (@forcedd,tspan,y0);
plot(t,y(:,1));
hold off;
function yp = forced(t,y)
yp = [y(2);
0.01/0.5*cos(6.28*t)-1/0.5*y(2)^2-20/0.5*y(1)];
end
function yp = forcedd(t,y)
yp = [y(2);
0.01/0.5*cos(6.28*t)-0.011/0.5*y(2)^2-20/0.5*y(1)];
end