daysPerIncrement=5;% Set the number of days per increment for the animation steps
% Initialize empty arrays to store the trajectory points
earthTrajectoryX=[];% Initialize empty array for Earth's x-coordinates
earthTrajectoryY=[];% Initialize empty array for Earth's y-coordinates
earthTrajectoryZ=[];% Initialize empty array for Earth's z-coordinates
marsTrajectoryX=[];% Initialize empty array for Mars's x-coordinates
marsTrajectoryY=[];% Initialize empty array for Mars's y-coordinates
marsTrajectoryZ=[];% Initialize empty array for Mars's z-coordinates
% Initialize markers for Earth and Mars at the starting position
earthMarker=plot3(Xearth_AU(1,1),Xearth_AU(2,1),Xearth_AU(3,1),'bo','MarkerFaceColor','blue','MarkerSize',5);% Place an Earth marker on the plot
marsMarker=plot3(Xmars_AU(1,1),Xmars_AU(2,1),Xmars_AU(3,1),'ro','MarkerFaceColor','red','MarkerSize',5);% Place a Mars marker on the plot
% Initialize labels for Earth and Mars
earthLabel=text(Xearth_AU(1,1)+0.1,Xearth_AU(2,1)+0.1,Xearth_AU(3,1),'Earth\n0 km/s','Color','blue','FontSize',10,'HorizontalAlignment','left');% Label for Earth with initial velocity
marsLabel=text(Xmars_AU(1,1)+0.1,Xmars_AU(2,1)+0.1,Xmars_AU(3,1),'Mars\n0 km/s','Color','red','FontSize',10,'HorizontalAlignment','left');% Label for Mars with initial velocity
% Animation loop for updated legend and labels
fori=1:daysPerIncrement:length(et)% Loop through the ephemeris time array in increments defined by daysPerIncrement
% Append current position to trajectory arrays
earthTrajectoryX=[earthTrajectoryXXearth_AU(1,i)];% Append Earth's current X position
earthTrajectoryY=[earthTrajectoryYXearth_AU(2,i)];% Append Earth's current Y position
earthTrajectoryZ=[earthTrajectoryZXearth_AU(3,i)];% Append Earth's current Z position
marsTrajectoryX=[marsTrajectoryXXmars_AU(1,i)];% Append Mars's current X position
marsTrajectoryY=[marsTrajectoryYXmars_AU(2,i)];% Append Mars's current Y position
marsTrajectoryZ=[marsTrajectoryZXmars_AU(3,i)];% Append Mars's current Z position
set(earthLabel,'Position',[Xearth_AU(1,i)+0.1,Xearth_AU(2,i)+0.1,Xearth_AU(3,i)],'String',sprintf('Earth\n%.2f km/s',v_earth));% Update Earth label with new velocity
set(marsLabel,'Position',[Xmars_AU(1,i)+0.1,Xmars_AU(2,i)+0.1,Xmars_AU(3,i)],'String',sprintf('Mars\n%.2f km/s',v_mars));% Update Mars label with new velocity
% Convert the ephemeris time to a readable date
currentDate=cspice_et2utc(et(i),'C',0);% Convert ephemeris time to a UTC string
% Dynamically update the date legend's text
set(dateLegend,'String',sprintf('Date: %s',currentDate));% Update the date legend with the current date
% Adjust speed of animation
pause(0.1);% Pause to slow down the animation for better visualization
end
%% Clean up by unloading SPICE kernels
cspice_kclear;% Unload all SPICE kernels and clear the SPICE subsystem