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?
1 次查看(过去 30 天)
显示 更早的评论
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
1 个评论
Mario Malic
2024-3-10
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.
回答(1 个)
Mathieu NOE
2024-3-11
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Satellite Mission Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!