I need help with my MATLAB code. I am trying to create an orbit with the given kepler elements. But I am having a hard time plotting the orbit around the earth. When I try to plot the 3-D Orbit with this code, all I get is a sphere. I want to see a graph with the sphere and the orbit around it. Can you help me with this? % Given parameters omega_earth = rad2deg(7.2921151467e-5); % Earth's rotational rate in deg/s period_of_repetition = 1 / 2; % Two orbits per day % Orbital parameters ecc = 0.74; i = 63.4349; % Inclination (deg) RAAN = -86.915798; % RAAN (deg) argp = 270; % Arg_of_Perigee (deg) f = linspace(0, 360, 100); % True anomaly (deg) mu = 398600.4418; % gravitational parameter of Earth (km^3/s^2) % Calculate semi-major axis a = ((omega_earth * period_of_repetition) / (360))^(-2/3) % km % Calculate periapsis and apoapsis distances periapsis_distance = a * (1 - ecc); % km apoapsis_distance = a * (1 + ecc); % km % Display the results disp(['Semi-Major Axis: ' num2str(a) ' meters']); disp(['Periapsis Distance: ' num2str(periapsis_distance) ' meters']); disp(['Apoapsis Distance: ' num2str(apoapsis_distance) ' meters']); % Calculate orbital radius evolution with time r = a * (1 - ecc^2) ./ (1 + ecc * cosd(f)); % Calculate Cartesian coordinates (position and velocity) x = r .* (cosd(RAAN) * cosd(argp + f) - sind(RAAN) * sind(argp + f) * cosd(i)); y = r .* (sind(RAAN) * cosd(argp + f) + cosd(RAAN) * sind(argp + f) * cosd(i)); z = r .* (sind(argp + f) * sind(i)); v = sqrt(mu ./ r); % velocity magnitude vx = v .* (-sind(RAAN) * cosd(argp + f) - cosd(RAAN) * sind(argp + f) * cosd(i)); vy = v .* (cosd(RAAN) * cosd(argp + f) - sind(RAAN) * sind(argp + f) * cosd(i)); vz = v .* (cosd(argp + f) * sind(i)); % Plot orbital radius evolution figure; plot(f, r); xlabel('True Anomaly (rad)'); ylabel('Orbital Radius (km)'); title('Orbital Radius Evolution'); % Plot 3D orbit figure; plot3(x, y, z); hold on; % Plot the Earth as a simple sphere earth_radius = 6.371e3; % Earth's radius in meters [x_earth, y_earth, z_earth] = sphere; earth = surf(x_earth * earth_radius, y_earth * earth_radius, z_earth * earth_radius); colormap([0 0 1]); % Set the color to blue for Earth alpha(earth, 0.7); % Set transparency for Earth % Set equal axis scale axis equal; % Set labels and title xlabel('X (meters)'); ylabel('Y (meters)'); zlabel('Z (meters)'); title('3D Representation of the Orbit with Earth'); % Display the plot hold off;
I need help with my MATLAB code. I am trying to create an orbit with the given kepler elements. But I am having a hard time plotting the orbit around the earth. When I try to plot the 3-D Orbit with this code, all I get is a sphere. I want to see a graph with the sphere and the orbit around it. Can you help me with this?
% Given parameters
omega_earth = rad2deg(7.2921151467e-5); % Earth's rotational rate in deg/s
period_of_repetition = 1 / 2; % Two orbits per day
% Orbital parameters
ecc = 0.74;
i = 63.4349; % Inclination (deg)
RAAN = -86.915798; % RAAN (deg)
argp = 270; % Arg_of_Perigee (deg)
f = linspace(0, 360, 100); % True anomaly (deg)
mu = 398600.4418; % gravitational parameter of Earth (km^3/s^2)
% Calculate semi-major axis
a = ((omega_earth * period_of_repetition) / (360))^(-2/3) % km
% Calculate periapsis and apoapsis distances
periapsis_distance = a * (1 - ecc); % km
apoapsis_distance = a * (1 + ecc); % km
% Display the results
disp(['Semi-Major Axis: ' num2str(a) ' meters']);
disp(['Periapsis Distance: ' num2str(periapsis_distance) ' meters']);
disp(['Apoapsis Distance: ' num2str(apoapsis_distance) ' meters']);
% Calculate orbital radius evolution with time
r = a * (1 - ecc^2) ./ (1 + ecc * cosd(f));
% Calculate Cartesian coordinates (position and velocity)
x = r .* (cosd(RAAN) * cosd(argp + f) - sind(RAAN) * sind(argp + f) * cosd(i));
y = r .* (sind(RAAN) * cosd(argp + f) + cosd(RAAN) * sind(argp + f) * cosd(i));
z = r .* (sind(argp + f) * sind(i));
v = sqrt(mu ./ r); % velocity magnitude
vx = v .* (-sind(RAAN) * cosd(argp + f) - cosd(RAAN) * sind(argp + f) * cosd(i));
vy = v .* (cosd(RAAN) * cosd(argp + f) - sind(RAAN) * sind(argp + f) * cosd(i));
vz = v .* (cosd(argp + f) * sind(i));
% Plot orbital radius evolution
figure;
plot(f, r);
xlabel('True Anomaly (rad)');
ylabel('Orbital Radius (km)');
title('Orbital Radius Evolution');
% Plot 3D orbit
figure;
plot3(x, y, z);
hold on;
% Plot the Earth as a simple sphere
earth_radius = 6.371e3; % Earth's radius in meters
[x_earth, y_earth, z_earth] = sphere;
earth = surf(x_earth * earth_radius, y_earth * earth_radius, z_earth * earth_radius);
colormap([0 0 1]); % Set the color to blue for Earth
alpha(earth, 0.7); % Set transparency for Earth
% Set equal axis scale
axis equal;
% Set labels and title
xlabel('X (meters)');
ylabel('Y (meters)');
zlabel('Z (meters)');
title('3D Representation of the Orbit with Earth');
% Display the plot
hold off;

Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 3 images









