Clear Filters
Clear Filters

How to fix ode graphs?

1 view (last 30 days)
Fatemeh
Fatemeh on 20 Apr 2023
Commented: Star Strider on 20 Apr 2023
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

Accepted Answer

Star Strider
Star Strider on 20 Apr 2023
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 Comments
Fatemeh
Fatemeh on 20 Apr 2023
Thank you so much.
Star Strider
Star Strider on 20 Apr 2023
As always, my pleasure!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!