How can I graph a nonlinear system of differential equations?

조회 수: 8 (최근 30일)
Josh Ciesar
Josh Ciesar 2022년 6월 11일
댓글: Josh Ciesar 2022년 6월 12일
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

채택된 답변

Sam Chak
Sam Chak 2022년 6월 12일
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
Josh Ciesar
Josh Ciesar 2022년 6월 12일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by