Plotting four plots when only two plot
    4 views (last 30 days)
  
       Show older comments
    
    Areg Arzoomanian
 on 3 Sep 2020
  
    
    
    
    
    Answered: Asad (Mehrzad) Khoddam
      
 on 3 Sep 2020
            This is my code. Im expecting to get 4 graphs but it only plots 2 at a time! how can i fix this?
Thank you
clear all
close all
clc
mu      = 398600;
minutes = 60;  %Conversion from minutes to seconds
x0 = [10000; 0; 0];
v0 = [0; -5.8; 0];
Y0 = [x0; v0];
t0 = 0;
tf = 1440*minutes;
options = odeset('RelTol', 1e-8, 'AbsTol', 1e-8) ;
[t,Y] = ode45(@twobodyEOM, [t0 tf], Y0, options, mu);
plotit(t, Y, minutes)
x=Y(:,1);
y=Y(:,2);
plot(x,y);
title('2D Trajectory')
xlabel('x position, km')
ylabel('y position, km')
grid on
grid minor
R = sqrt(Y(:,1).^2 + Y(:,2).^2 + Y(:,3).^2);
V = sqrt(Y(:,4).^2 + Y(:,5).^2 + Y(:,6).^2);
e=((V.^2)/2)-mu./R;
plot(t,e)
title('Specific Energy vs. Time')
xlabel('Time')
ylabel('Specific Energy')
grid on
grid minor
% ~~~~~~~~~~~~~~~~~~~~~~~~
function dYdt = twobodyEOM(t,Y,mu)
rvec = Y(1:3);
vvec = Y(4:6);
r = sqrt(rvec(1)^2+rvec(2)^2+rvec(3)^2);
rdotvec = vvec ;
vdotvec = -mu/r^3*rvec ;
dYdt = [rdotvec; vdotvec];
end 
% ~~~~~~~~~~~~~~~~~~~~~~~~
function dYdt = rates(t,Y,mu)
x    = Y(1);
Dx   = Y(2);  % v
D2x  = -mu/x^2; % vdot
dYdt = [Dx; D2x];
end %rates
function plotit(t, Y, minutes)
% ~~~~~~~~~~~~~
%...Position vs time:
subplot(2,1,1)
plot(t/minutes,Y(:,1), '-ok')
xlabel('time, minutes')
ylabel('position, km')
grid on
axis([-inf inf 5000 15000])
%...Velocity versus time:
subplot(2,1,2)
plot(t/minutes,Y(:,2), '-ok')
xlabel('time, minutes')
ylabel('velocity, km/s')
grid on
axis([-inf inf -10 10]) 
end %plotit
0 Comments
Accepted Answer
  Star Strider
      
      
 on 3 Sep 2020
        Add figure calls to create new figure windows, otherwise the plots will occur on existing figure windows, and over-write the previous plots: 
mu      = 398600;
minutes = 60;  %Conversion from minutes to seconds
x0 = [10000; 0; 0];
v0 = [0; -5.8; 0];
Y0 = [x0; v0];
t0 = 0;
tf = 1440*minutes;
options = odeset('RelTol', 1e-8, 'AbsTol', 1e-8) ;
[t,Y] = ode45(@twobodyEOM, [t0 tf], Y0, options, mu);
figure                                                          % <— ADD
plotit(t, Y, minutes)
x=Y(:,1);
y=Y(:,2);
figure                                                          % <— ADD
plot(x,y);
title('2D Trajectory')
xlabel('x position, km')
ylabel('y position, km')
grid on
grid minor
axis('equal')                                                   % <— ADD (Optional)
R = sqrt(Y(:,1).^2 + Y(:,2).^2 + Y(:,3).^2);
V = sqrt(Y(:,4).^2 + Y(:,5).^2 + Y(:,6).^2);
e=((V.^2)/2)-mu./R;
figure                                                          % <— ADD
plot(t,e)
title('Specific Energy vs. Time')
xlabel('Time')
ylabel('Specific Energy')
grid on
grid minor
There is no reason to quote the functions here, so I do not.  They are not changed, regardless.  
0 Comments
More Answers (1)
  Asad (Mehrzad) Khoddam
      
 on 3 Sep 2020
        The subplot command is subplot(2,1,1) which means 2 rows and one column. If you need 4 graphs at the same time you should use subplot(2,2,1),subplot(2,2,2), subplot(2,2,3), and subplot(2,2,4).
If you want two graph over each other, after the first plot use this command:
hold on;
0 Comments
See Also
Categories
				Find more on Title in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

