-
Notifications
You must be signed in to change notification settings - Fork 3
/
rv2oe.m
67 lines (38 loc) · 1.53 KB
/
rv2oe.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
function [oe] = rv2oe(r,v)
%Obtaining the classical orbital elements from inertial position and velocity vectors
%Inputs:
% r: Position vector [km]
% v: Velocity vector [km/s]
%Outputs:
% oe: Vector of orbital elements
% oe(1): Semimajor axis [km]
% oe(2): Eccentricity [dimensionless]
% oe(3): Inclination [rad]
% oe(4): RAAN [rad]
% oe(5): Argument of periapsis [rad]
% oe(6): True anomaly [rad]
mu=3.986004418e+5; %Standard gravitational parameter [km^3/s^2]
h=cross(r,v); %Specific angular momentum [km^2/s]
inc=acos(h(3)/norm(h)); %Inclination [rad]
n=[-h(2);h(1);0]; %Vector perpendicular to h and Earth's north pole
if n(2)>=0
W=acos(n(1)/norm(n)); %RAAN [rad]
elseif n(2)<0
W=2*pi-acos(n(1)/norm(n)); %RAAN [rad]
end
E=(1/mu)*(cross(v,h))-r/norm(r); %Eccentricity vector [dimensionless]
if E(3)>=0
w=acos((E'*n)/(norm(n)*norm(E))); %Argument of periapsis [rad]
elseif E(3)<0
w=2*pi-acos((E'*n)/(norm(n)*norm(E))); %Argument of periapsis [rad]
end
Vr=v'*r/norm(r); %Derivative of position magnitude [km/s]
if Vr>=0
theta=acos((E'*r)/(norm(r)*norm(E))); %True anomaly [rad]
elseif Vr<0
theta=2*pi-acos((E'*r)/(norm(r)*norm(E))); %True anomaly [rad]
end
a=(norm(h)^2/mu)/(1-norm(E)^2); %Semimajor axis [km]
e=norm(E); %Eccentricity [dimensionless]
oe=[a,e,inc,W,w,theta]; %Vector of orbital elements
end