How to fix ode graphs?

조회 수: 1 (최근 30일)
Fatemeh
Fatemeh 2023년 4월 20일
댓글: Star Strider 2023년 4월 20일
Hello, I'm trying to combine these two ode plots into one chart, but it gives me two different charts. Can someone please help me with this?
syms y(x) x Y
N=5;
r=0.05;
m=0.01;
p=1;
s=1;
t=0.1;
a=0.25;
b=0.25;
C0=1;
C=5;
t0= N+r+t*p*s-m;
t1=-(N+r+t*p*s-m);
Dy = diff(y);
D2y = diff(y,2);
ode = y-(1/x)*(N+r)-((t*p*s*Dy)/y)+((Dy*((s^2)-m))/y)+((D2y*x*(s^2))/(2*y));
[VF,Subs] = odeToVectorField(ode);
odefcn = matlabFunction(VF, 'Vars',{x,Y});
tspan = [C0 80];
ic = [t0 t1];
[x,y] = ode45(odefcn, tspan, ic);
figure
plot(x, y)
grid
hold on
syms y(x) x Y
f=(x+(x^2+4*r*x*(1-a-b))^0.5)/(2*(1-a-b));
t00=N/C;
t11=-N/(C^2);
Dy = diff(y);
D2y = diff(y,2);
ode2= y-((C-x+f*((1/(r+f))^(1/(1-a-b)))+t*p*s*x*Dy+x*Dy*((s^2)-m)+Dy*(C-x)+0.5*D2y*(x^2)*(s^2)-x*(s^2)*Dy)/x*(1+t*p*s-m));
[VF1,Subs1] = odeToVectorField(ode2);
odefcn1 = matlabFunction(VF1, 'Vars',{x,Y});
tspan2 = [C 1];
ic2 = [t00 t11];
[x,y] = ode45(odefcn1, tspan2, ic2);
figure
plot(x, y)
grid
hold off

채택된 답변

Star Strider
Star Strider 2023년 4월 20일
You are telling it to produce two different plots because of two separate figure calls.
syms y(x) x Y
N=5;
r=0.05;
m=0.01;
p=1;
s=1;
t=0.1;
a=0.25;
b=0.25;
C0=1;
C=5;
t0= N+r+t*p*s-m;
t1=-(N+r+t*p*s-m);
Dy = diff(y);
D2y = diff(y,2);
ode = y-(1/x)*(N+r)-((t*p*s*Dy)/y)+((Dy*((s^2)-m))/y)+((D2y*x*(s^2))/(2*y));
[VF,Subs] = odeToVectorField(ode);
odefcn = matlabFunction(VF, 'Vars',{x,Y});
tspan = [C0 80];
ic = [t0 t1];
[x1,y1] = ode45(odefcn, tspan, ic);
figure
plot(x1, y1)
grid
hold on
syms y(x) x Y
f=(x+(x^2+4*r*x*(1-a-b))^0.5)/(2*(1-a-b));
t00=N/C;
t11=-N/(C^2);
Dy = diff(y);
D2y = diff(y,2);
ode2= y-((C-x+f*((1/(r+f))^(1/(1-a-b)))+t*p*s*x*Dy+x*Dy*((s^2)-m)+Dy*(C-x)+0.5*D2y*(x^2)*(s^2)-x*(s^2)*Dy)/x*(1+t*p*s-m));
[VF1,Subs1] = odeToVectorField(ode2);
odefcn1 = matlabFunction(VF1, 'Vars',{x,Y});
tspan2 = [C 1];
ic2 = [t00 t11];
[x2,y2] = ode45(odefcn1, tspan2, ic2);
figure
plot(x2, y2)
grid
hold off
figure % All Together 1!
plot(x1,y1(:,1), 'DisplayName','(x_1,y_1_1)')
hold on
plot(x1,y1(:,2), 'DisplayName','(x_1,y_1_2)')
plot(x2, y2(:,1), 'DisplayName','(x_2,y_2_1)')
plot(x2, y2(:,2), 'DisplayName','(x_2,y_2_2)')
hold off
grid
legend('Location','best')
figure % All Together 2!
yyaxis left
plot(x1,y1(:,1), 'DisplayName','(x_1,y_1_1)')
hold on
plot(x1,y1(:,2), 'DisplayName','(x_1,y_1_2)')
hold off
yyaxis right
plot(x2, y2(:,1), 'DisplayName','(x_2,y_2_1)')
hold on
plot(x2, y2(:,2), 'DisplayName','(x_2,y_2_2)')
hold off
grid
legend('Location','best')
The ‘x’ limits in the two integrations are not the same.
.
  댓글 수: 2
Fatemeh
Fatemeh 2023년 4월 20일
Thank you so much.
Star Strider
Star Strider 2023년 4월 20일
As always, my pleasure!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by