How to make two for loop plots appear next to each other at the same time?

7 visualizaciones (últimos 30 días)
I want to place two plots with separate for loop next to each other using subplot but how can I make it so that they both appear at the same time? At the moment, the second plot will only appear after the first for loop is finished. Thanks!
This is the code:
%
% Planetary orbit using Euler Cromer methods.
% by Kevin Berwick,
% based on 'Computational Physics' book by N Giordano and H Nakanishi
% Section 4.1
%
npoints=500;
dt = 0.002; % time step in years
x=1; % initialise position of planet in AU
y=0;
v_x=0; % initialise velocity of planet in AU/yr
v_y=2*pi;
% Plot the Sun at the origin
subplot(2,1,1)
plot(0,0,'oy','MarkerSize',30, 'MarkerFaceColor','yellow');
hold on
s = text(0.15,0,'Sun');
% Plot Earth and orbit
h1 = plot(x,y,'ob','MarkerSize',10, 'MarkerFaceColor','blue');
h = animatedline(x, y, 'Color', 'b');
axis([-1.5 1.5 -1.5 1.5]);
xlabel('x(AU)');
ylabel('y(AU)');
title('Planetary orbit using Euler Cromer method');
% add text to Earth orbit
e = text(x-0.2,y-0.2, 'Earth');
for step = 1:npoints-1;
% loop over the timesteps
radius=sqrt(x^2+y^2);
% Compute new velocities in the x and y directions
v_x_new=v_x - (4*pi^2*x*dt)/(radius^3);
v_y_new=v_y - (4*pi^2*y*dt)/(radius^3);
% Euler Cromer Step - update positions using newly calculated velocities
x_new=x+v_x_new*dt;
y_new=y+v_y_new*dt;
% Plot planet position immediately
addpoints(h, x_new, y_new); %new
drawnow;
set(h1,'XData',x_new,'YData',y_new); %new
drawnow;
%Plot text position
set(e,'Position',[x_new-0.2,y_new-0.2]); %new
drawnow;
% Update x and y velocities with new velocities
v_x=v_x_new;
v_y=v_y_new;
% Update x and y with new positions
x=x_new;
y=y_new;
end;
%
% Planetary orbit using second order Runge-Kutta method.
% by Kevin Berwick,
% based on 'Computational Physics' book by N Giordano and H Nakanishi
% Section 4.1 %
npoints=500;
dt = 0.002;
t=0;
x=1;
y=0;
v_x=0;
v_y=2*pi;
% Plot the Sun at the origin
subplot(2,1,2)
plot(0,0,'oy','MarkerSize',30, 'MarkerFaceColor','yellow');
hold on
s = text(0.15,0,'Sun');
% Plot Earth and orbit
h1 = plot(x,y,'ob','MarkerSize',10, 'MarkerFaceColor','blue');
h = animatedline(x, y, 'Color', 'b');
axis([-1.5 1.5 -1.5 1.5]);
xlabel('x(AU)');
ylabel('y(AU)');
title('Planetary orbit using second order Runge-Kutta method');
%hold on;
% add text to Earth orbit
e = text(x-0.2,y-0.2, 'Earth');
for step = 1:npoints-1;
% loop over the timesteps
radius=sqrt(x^2+y^2);
% Compute Runge Kutta values for the y equations
y_dash=y+0.5*v_y*dt;
v_y_dash=v_y - 0.5*(4*pi^2*y*dt)/(radius^3);
% update positions and new y velocity
y_new=y+v_y_dash*dt;
v_y_new=v_y-(4*pi^2*y_dash*dt)/(radius^3);
% Compute Runge Kutta values for the x equations
x_dash=x+0.5*v_x*dt;
v_x_dash=v_x - 0.5*(4*pi^2*x*dt)/(radius^3);
% update positions using newly calculated velocity
x_new=x+v_x_dash*dt;
v_x_new=v_x-(4*pi^2*x_dash*dt)/(radius^3);
% Plot planet position immediately
addpoints(h, x, y);
drawnow;
set(h1,'XData',x_new,'YData',y_new); %new
drawnow;
%Plot text position
set(e,'Position',[x_new-0.2,y_new-0.2]); %new
drawnow;
% Update x and y velocities with new velocities
v_x=v_x_new;
v_y=v_y_new;
% Update x and y with new positions
x=x_new;
y=y_new;
end;

Respuesta aceptada

Jan
Jan el 15 de Jul. de 2022
Move the code of both loops into one loop. Define the parent of the dranw objects. Example:
FigH = figure;
Axes1 = subplot(1,2,1);
Axes2 = subplot(1,2,2);
Line1 = plot(Axes1, 1, 1);
Line2 = plot(Axes2, 1, 1);
for k = 1:20
Line1.YData = [Line1.YData, rand];
Line1.XData = [Line1.XData, k + 1];
Line2.YData = [Line2.YData, rand];
Line2.XData = [Line2.XData, k + 1];
drawnow;
pause(0.5);
end

Más respuestas (0)

Categorías

Más información sobre Graphics Performance en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by