Plotting the results of 2 ODE45 in the same figure

7 visualizaciones (últimos 30 días)
Rodrigo Pena
Rodrigo Pena el 18 de Feb. de 2021
Comentada: Rodrigo Pena el 19 de Feb. de 2021
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

Respuesta aceptada

Alan Stevens
Alan Stevens el 18 de Feb. de 2021
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
  9 comentarios
Alan Stevens
Alan Stevens el 19 de Feb. de 2021
The equations in my listing do that (for two different values of a).
Rodrigo Pena
Rodrigo Pena el 19 de Feb. de 2021
Ok, thank you !

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Ordinary Differential Equations en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by