How would i change this code so that it plots all of the circular paths and markers all at once instead of one after the other?

2 views (last 30 days)
close all;
clear all;
earth_radius = 6371; % Earth radius in km
satellite_altitude = 500; % Altitude of satellites in km
num_satellites = 4;
a = 0;
b = 2*pi;
angle = (b).*rand(num_satellites,1);
r = earth_radius + satellite_altitude;
% Initialize marker position
marker_pos = zeros(num_satellites, 3);
for i = 1:num_satellites
center = [0,0,0];
th = 0:pi/20:2*pi;
x = center(1) + r.*cos(th)
y = center(2) + r.*sin(th)
z = center(3) + r.*sin(angle(i)).*cos(th)
% Plot satellite path
plot3(x,y,z,'b-.','linewidth',1)
grid on;
hold on;
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
% Plot marker at initial position
marker = plot3(x(1), y(1), z(1), 'ro','MarkerFaceColor',[1 0 0], 'MarkerSize', 10);
% Plot marker moving along the path
for j = 2:length(x)
set(marker, 'XData', x(j), 'YData', y(j), 'ZData', z(j));
drawnow;
pause(0.05); % Adjust the pause time as needed
end
end
x = 1×41
1.0e+03 * 6.8710 6.7864 6.5347 6.1221 5.5588 4.8585 4.0387 3.1194 2.1233 1.0749 0.0000 -1.0749 -2.1233 -3.1194 -4.0387 -4.8585 -5.5588 -6.1221 -6.5347 -6.7864 -6.8710 -6.7864 -6.5347 -6.1221 -5.5588 -4.8585 -4.0387 -3.1194 -2.1233 -1.0749
y = 1×41
1.0e+03 * 0 1.0749 2.1233 3.1194 4.0387 4.8585 5.5588 6.1221 6.5347 6.7864 6.8710 6.7864 6.5347 6.1221 5.5588 4.8585 4.0387 3.1194 2.1233 1.0749 0.0000 -1.0749 -2.1233 -3.1194 -4.0387 -4.8585 -5.5588 -6.1221 -6.5347 -6.7864
z = 1×41
1.0e+03 * -6.4263 -6.3472 -6.1118 -5.7259 -5.1990 -4.5441 -3.7773 -2.9175 -1.9858 -1.0053 -0.0000 1.0053 1.9858 2.9175 3.7773 4.5441 5.1990 5.7259 6.1118 6.3472 6.4263 6.3472 6.1118 5.7259 5.1990 4.5441 3.7773 2.9175 1.9858 1.0053
x = 1×41
1.0e+03 * 6.8710 6.7864 6.5347 6.1221 5.5588 4.8585 4.0387 3.1194 2.1233 1.0749 0.0000 -1.0749 -2.1233 -3.1194 -4.0387 -4.8585 -5.5588 -6.1221 -6.5347 -6.7864 -6.8710 -6.7864 -6.5347 -6.1221 -5.5588 -4.8585 -4.0387 -3.1194 -2.1233 -1.0749
y = 1×41
1.0e+03 * 0 1.0749 2.1233 3.1194 4.0387 4.8585 5.5588 6.1221 6.5347 6.7864 6.8710 6.7864 6.5347 6.1221 5.5588 4.8585 4.0387 3.1194 2.1233 1.0749 0.0000 -1.0749 -2.1233 -3.1194 -4.0387 -4.8585 -5.5588 -6.1221 -6.5347 -6.7864
z = 1×41
1.0e+03 * 6.3644 6.2860 6.0529 5.6707 5.1489 4.5003 3.7409 2.8894 1.9667 0.9956 0.0000 -0.9956 -1.9667 -2.8894 -3.7409 -4.5003 -5.1489 -5.6707 -6.0529 -6.2860 -6.3644 -6.2860 -6.0529 -5.6707 -5.1489 -4.5003 -3.7409 -2.8894 -1.9667 -0.9956
x = 1×41
1.0e+03 * 6.8710 6.7864 6.5347 6.1221 5.5588 4.8585 4.0387 3.1194 2.1233 1.0749 0.0000 -1.0749 -2.1233 -3.1194 -4.0387 -4.8585 -5.5588 -6.1221 -6.5347 -6.7864 -6.8710 -6.7864 -6.5347 -6.1221 -5.5588 -4.8585 -4.0387 -3.1194 -2.1233 -1.0749
y = 1×41
1.0e+03 * 0 1.0749 2.1233 3.1194 4.0387 4.8585 5.5588 6.1221 6.5347 6.7864 6.8710 6.7864 6.5347 6.1221 5.5588 4.8585 4.0387 3.1194 2.1233 1.0749 0.0000 -1.0749 -2.1233 -3.1194 -4.0387 -4.8585 -5.5588 -6.1221 -6.5347 -6.7864
z = 1×41
1.0e+03 * 5.5266 5.4586 5.2561 4.9243 4.4711 3.9079 3.2485 2.5090 1.7078 0.8646 0.0000 -0.8646 -1.7078 -2.5090 -3.2485 -3.9079 -4.4711 -4.9243 -5.2561 -5.4586 -5.5266 -5.4586 -5.2561 -4.9243 -4.4711 -3.9079 -3.2485 -2.5090 -1.7078 -0.8646
x = 1×41
1.0e+03 * 6.8710 6.7864 6.5347 6.1221 5.5588 4.8585 4.0387 3.1194 2.1233 1.0749 0.0000 -1.0749 -2.1233 -3.1194 -4.0387 -4.8585 -5.5588 -6.1221 -6.5347 -6.7864 -6.8710 -6.7864 -6.5347 -6.1221 -5.5588 -4.8585 -4.0387 -3.1194 -2.1233 -1.0749
y = 1×41
1.0e+03 * 0 1.0749 2.1233 3.1194 4.0387 4.8585 5.5588 6.1221 6.5347 6.7864 6.8710 6.7864 6.5347 6.1221 5.5588 4.8585 4.0387 3.1194 2.1233 1.0749 0.0000 -1.0749 -2.1233 -3.1194 -4.0387 -4.8585 -5.5588 -6.1221 -6.5347 -6.7864
z = 1×41
1.0e+03 * -6.8309 -6.7468 -6.4966 -6.0864 -5.5263 -4.8302 -4.0151 -3.1012 -2.1109 -1.0686 -0.0000 1.0686 2.1109 3.1012 4.0151 4.8302 5.5263 6.0864 6.4966 6.7468 6.8309 6.7468 6.4966 6.0864 5.5263 4.8302 4.0151 3.1012 2.1109 1.0686
  1 Comment
Mario Malic
Mario Malic on 10 Mar 2024
Your code calculates each point within for loop and plots it. You can reorganise your code to calculate x, y and z to be a vector for 1 satelite. Afterwards, if you put it in a 2D matrix where each column represents the coordinate, you can do the same for num_satelites that will end up with 3D matrix, where each page (3rd dimension) represents a satelite.
Consult the documentation on plot3 on how to plot data when input arguments are vectors or matrices.

Sign in to comment.

Answers (1)

Mathieu NOE
Mathieu NOE on 11 Mar 2024
simply commenting the inner for loop , and you get this result :
(I also reduced the number of theta points by factor 2 ) : th = 0:pi/10:2*pi;
earth_radius = 6371; % Earth radius in km
satellite_altitude = 500; % Altitude of satellites in km
num_satellites = 4;
a = 0;
b = 2*pi;
angle = (b).*rand(num_satellites,1);
r = earth_radius + satellite_altitude;
% Initialize marker position
marker_pos = zeros(num_satellites, 3);
for i = 1:num_satellites
center = [0,0,0];
th = 0:pi/10:2*pi;
x = center(1) + r.*cos(th);
y = center(2) + r.*sin(th);
z = center(3) + r.*sin(angle(i)).*cos(th);
% Plot satellite path
plot3(x,y,z,'b-.','linewidth',1)
grid on;
hold on;
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
% Plot marker
marker = plot3(x, y, z, 'ro','MarkerFaceColor',[1 0 0], 'MarkerSize', 10);
% % Plot marker moving along the path
% for j = 2:length(x)
% set(marker, 'XData', x(j), 'YData', y(j), 'ZData', z(j));
% drawnow;
% pause(0.05); % Adjust the pause time as needed
% end
end

Categories

Find more on Satellite Mission Analysis in Help Center and File Exchange

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!