%% Function Description (Aerobraking Orbit Animation)
% The script simulates and animates the process of aerobraking for a spacecraft in an elliptical orbit around Mars.
% Aerobraking is a technique used to reduce the velocity and altitude of a spacecraft by utilizing atmospheric drag.
% This script models the gradual change in orbital parameters, specifically the eccentricity, as the spacecraft performs multiple passes through the Martian atmosphere.
%
% The script begins by defining the initial orbital parameters such as the periapsis altitude, the initial eccentricity,
% and the physical properties of the spacecraft and Mars. Using these parameters, it calculates the initial semi-major axis
% and orbital period of the spacecraft's orbit.
%
% The animation is generated by iteratively solving Kepler's equation to determine the spacecraft's position over time in its orbit.
% As the spacecraft passes through the Martian atmosphere, the drag force reduces its velocity, causing the orbit to become more circular (reducing eccentricity).
% This is achieved by updating the semi-major axis and eccentricity of the orbit after each pass.
%
% Key concepts and calculations:
% - Semi-major axis (a): Determines the size of the orbit and is recalculated based on the changing eccentricity.
% - Eccentricity (e): Measures the shape of the orbit. The aerobraking effect reduces the eccentricity over multiple orbits.
% - Gravitational parameter (mu): Represents the gravitational influence of Mars, affecting the orbital motion.
% - Drag force (Fd): Calculated based on the atmospheric density, drag coefficient, cross-sectional area, and velocity of the spacecraft.
% This force acts opposite to the velocity, reducing the orbital energy and altering the orbit.
% - Kepler's equation: Used to solve for the eccentric anomaly, which helps in determining the spacecraft's position in its orbit.
%
% The script continuously updates the plot to animate the changing orbit, showing the effect of atmospheric drag on the spacecraft's path around Mars.
% It also calculates the total time spent in aerobraking and the number of orbits required to reach the target eccentricity.
%
% Author: Thomas West
% Date: September 3, 2024
%% Script
% Clear all variables from the workspace to ensure a fresh start
clearvars;
% Close all open figures to clear the plotting space
closeall;
% Clear the command window for a clean output display
clc;
% Set the display format to long with general number formatting for precision
formatlongG;
Mars_radius=3396;% Radius of Mars in km
Altitude=100;
Apo_Radius=400;
ae=Apo_Radius+Mars_radius;
pe=Altitude+Mars_radius;
target_eccentricity=(ae-pe)/(ae+pe);% Target eccentricity to achieve
% Display the result
fprintf('The final target orbit eccentricity is: %.6f\n',target_eccentricity);
% Constants and Initial Conditions
Mars_radius=3396;% Radius of Mars in km
rp=Mars_radius+Altitude;% Periapsis distance from center of Mars, in km
e=0.9;% Initial Eccentricity
mu=42828;% Mars' gravitational parameter in km^3/s^2
Cd=1.75;% Drag coefficient
A=15.9;% Cross-sectional area of the spacecraft in m^2
m=1500;% Mass of the spacecraft in kg
H=11.1;% Scale height of Mars' atmosphere in km
rho0=0.02;% Reference density at a reference altitude in kg/m^3
h0=Altitude;% Reference altitude above Mars' surface in km
% Calculate initial semi-major axis and orbital period
a=calculate_semi_major_axis(rp,e);
P=2*pi*sqrt(a^3/mu);% Initial orbital period in seconds