How can I graph a nonlinear system of differential equations?

7 visualizaciones (últimos 30 días)
I am having trouble solving a system of non linear differential equations regarding a simple adiabatic piston. I am able to find a solution using ode45, except the graphed solution does not look like it is supposed to. The graphs are below
For whatever reason, the graphs do not oscillate symmetrically like they are supposed to. My code is below. I am sure that I have entered the equations correctly, yet every different method I use to solve the equations I get similar results. Could someone please help
p_i = 2 ;
y0 = [1 0 1] ; %initial values
tspan = [0 10] ;
[t, y] = ode45(@rate,tspan,y0) ;
x = y(:,1) ;
v = y(:,2) ;
temp = y(:,3) ;
p = p_i * (temp / x) ;
figure(1)
plot(t,p)
title('pressure vs time')
figure(2)
plot(t,temp)
title('temperature vs time')
figure(3)
plot(t,v)
title('velocity vs time')
function dydt = rate(~,y)
x = y(1);
v = y(2);
temp = y(3);
p_i = 2 ;
a = 2/5 ;
p = p_i .* (temp ./ x) ;
dxdt = v ;
dvdt = (p - 1) ./ (p_i - 1) ;
dtempdt = -v .* (p / p_i) .* a ;
dydt = [dxdt ; dvdt ; dtempdt] ;
end

Respuesta aceptada

Sam Chak
Sam Chak el 12 de Jun. de 2022
I don't know how your graphs are supposed to look like because I'm not an adiabatic pistonist. But first things first, you must verify if your differential equations are 100% correct. I have made the substitutions and simplified them, so please check them.
function dydt = rate(~, y)
x = y(1);
v = y(2);
temp = y(3);
p_i = 2;
a = 2/5;
p = p_i*(temp./x);
dxdt = v;
dvdt = (p - 1)/(p_i - 1);
dTdt = - v.*(p/p_i)*a;
dydt = [dxdt; dvdt; dTdt];
end
One mistake is spotted though. To plot the pressure p, which is a division between two vectors, you must add a 'dot' in front of the slash /.
x = y(:,1);
v = y(:,2);
temp = y(:,3);
p = p_i*(temp./x);
figure(1)
plot(t, p)
title('pressure vs time')
  1 comentario
Josh Ciesar
Josh Ciesar el 12 de Jun. de 2022
I appreciate the help you have given; the error you pointed out with my pressure equation seemed to have fixed that graph. Although, in my code the velocity graph is still not symmetrical. Do you know why this might be? It is odd because when I graph x(t) I can see the x moves symmetrically, so why does velocity not? aka, why is velocity not moving as the derivative of x
You can tell that the velocity rises to its peak faster than it takes to fall it its minimum.

Iniciar sesión para comentar.

Más respuestas (0)

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