forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample_12_01.m
125 lines (110 loc) · 5.45 KB
/
Example_12_01.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function Example_12_01
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%
% This function solves Example 12.1 by using MATLAB's ode45 to numerically
% integrate Equation 12.2 for atmospheric drag.
%
% User M-functions required: sv_from_coe, atmosphere
% User subfunctions required: rates, terminate
% ------------------------------------------------------------------------
%...Preliminaries:
close all, clear all, clc
%...Conversion factors:
hours = 3600; %Hours to seconds
days = 24*hours; %Days to seconds
deg = pi/180; %Degrees to radians
%...Constants;
mu = 398600; %Gravitational parameter (km^3/s^2)
RE = 6378; %Earth's radius (km)
wE = [ 0 0 7.2921159e-5]'; %Earth's angular velocity (rad/s)
%...Satellite data:
CD = 2.2; %Drag codfficient
m = 100; %Mass (kg)
A = pi/4*(1^2) ; %Frontal area (m^2)
%...Initial orbital parameters (given):
rp = RE + 215; %perigee radius (km)
ra = RE + 939; %apogee radius (km)
RA = 339.94*deg; %Right ascencion of the node (radians)
i = 65.1*deg; %Inclination (radians)
w = 58*deg; %Argument of perigee (radians)
TA = 332*deg; %True anomaly (radians)
%...Initial orbital parameters (inferred):
e = (ra-rp)/(ra+rp); %eccentricity
a = (rp + ra)/2; %Semimajor axis (km)
h = sqrt(mu*a*(1-e^2)); %angular momentrum (km^2/s)
T = 2*pi/sqrt(mu)*a^1.5; %Period (s)
%...Store initial orbital elements (from above) in the vector coe0:
coe0 = [h e RA i w TA];
%...Obtain the initial state vector from Algorithm 4.5 (sv_from_coe):
[R0 V0] = sv_from_coe(coe0, mu); %R0 is the initial position vector
%V0 is the initial velocity vector
r0 = norm(R0); v0 = norm(V0); %Magnitudes of R0 and V0
%...Use ODE45 to integrate the equations of motion d/dt(R,V) = f(R,V)
% from t0 to tf:
t0 = 0; tf = 120*days; %Initial and final times (s)
y0 = [R0 V0]'; %Initial state vector
nout = 40000; %Number of solution points to output
tspan = linspace(t0, tf, nout); %Integration time interval
% Set error tolerances, initial step size, and termination event:
options = odeset('reltol', 1.e-8, ...
'abstol', 1.e-8, ...
'initialstep', T/10000, ...
'events', @terminate);
global alt %Altitude
[t,y] = ode45(@rates, tspan, y0,options); %t is the solution times
%y is the state vector history
%...Extract the locally extreme altitudes:
altitude = sqrt(sum(y(:,1:3).^2,2)) - RE; %Altitude at each time
[max_altitude,imax,min_altitude,imin] = extrema(altitude);
maxima = [t(imax) max_altitude]; %Maximum altitudes and times
minima = [t(imin) min_altitude]; %Minimum altitudes and times
apogee = sortrows(maxima,1); %Maxima sorted with time
perigee = sortrows(minima,1); %Minima sorted with time
figure(1)
apogee(1,2) = NaN;
%...Plot perigee and apogee history on the same figure:
plot(apogee(:,1)/days, apogee(:,2),'b','linewidth',2)
hold on
plot(perigee(:,1)/days, perigee(:,2),'r','linewidth',2)
grid on
grid minor
xlabel('Time (days)')
ylabel('Altitude (km)')
ylim([0 1000]);
%...Subfunctions:
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function dfdt = rates(t,f)
% ~~~~~~~~~~~~~~~~~~~~~~~~~~
%
% This function calculates the spacecraft acceleration from its
% position and velocity at time t.
% ------------------------------------------------------------------------
R = f(1:3)'; %Position vector (km/s)
r = norm(R); %Distance from earth's center (km)
alt = r - RE; %Altitude (km)
rho = atmosphere(alt); %Air density from US Standard Model (kf/m^3)
V = f(4:6)'; %Velocity vector (km/s)
Vrel = V - cross(wE,R); %Velocity relative to the atmosphere (km/s)
vrel = norm(Vrel); %Speed relative to the atmosphere (km/s)
uv = Vrel/vrel; %Relative velocity unit vector
ap = -CD*A/m*rho*... %Acceleration due to drag (m/s^2)
(1000*vrel)^2/2*uv; %(converting units of vrel from km/s to m/s)
a0 = -mu*R/r^3; %Gravitational ecceleration (km/s^2)
a = a0 + ap/1000; %Total acceleration (km/s^2)
dfdt = [V a]'; %Velocity and the acceleraion returned to ode45
end %rates
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function [lookfor stop direction] = terminate(t,y)
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%
% This function specifies the event at which ode45 terminates.
% ------------------------------------------------------------------------
lookfor = alt - 100; % = 0 when altitude = 100 km
stop = 1; % 1 means terminate at lookfor = 0; Otherwise 0
direction = -1; % -1 means zero crossing is from above
end %terminate
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
end %Example_12_01
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~