File size: 5,188 Bytes
5ab75a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
function [CART] = Kep2Cart(KEP)
% Kep2Cart - Converts an object's Keplerian elements to Cartesian elements
%
% Syntax: [CART] = Kep2Cart(a,e,i,Omega,w,M);
%
% =========================================================================
%
% Copyright (c) 2023 United States Government as represented by the
% Administrator of the National Aeronautics and Space Administration.
% All Rights Reserved.
%
% =========================================================================
%
% Description:
%
%   Converts an object's Keplerian elements to Cartesian elements. Note 
%   that the input anomaly angle must be with respect to mean anomaly and 
%   all angle inputs are in the units of radians.
%
%   NOTE: Mean2EccAnomaly.m is a dependency for this function.
%
% =========================================================================
%
% Input:
%
%   a       -   Semi-major axis (km)                                  [nx1]
%   e       -   Eccentricity                                          [nx1]
%   i       -   Inclination (rad)                                     [nx1]
%   Omega   -   Right ascension of the ascending node (RAAN) (rad)    [nx1]
%   w       -   Argument of perigee (rad)                             [nx1]
%   M       -   Mean anomaly (rad)                                    [nx1]
%
% =========================================================================
%
% Output:
%
%   CART    -   Position and velocity vector in ECI coordinates (km,  [nx6]
%               km/s)
%
% =========================================================================
%
% Initial version: Mar 2013;  Latest update: Jun 2023
%
% ----------------- BEGIN CODE -----------------

    sz = size(KEP);
    
    if sz(2) ~= 6 || ~all(isa(KEP, 'double'))
        error('Kep2Cart:InvalidOrbitalElements', 'Invalid Keplerian elements');
    end
    
    a     = KEP(:,1);
    e     = KEP(:,2);
    i     = KEP(:,3);
    Omega = KEP(:,4);
    w     = KEP(:,5);
    M     = KEP(:,6);
    
    if any(abs(e - 1) <= 1E-5)
        error('Kep2Cart:ParabolicOrRectilinearOrbit', 'Parabolic or rectilinear orbit, cannot calculate Cartesian elements');
    end
    
    if any(e > 1)
        error('Kep2Cart:HyperbolicOrbit', 'Hyperbolic orbit, cannot calculate Cartesian elements');
    end
    
    if any(abs(e) <= 1E-5)
        warning('Kep2Cart:SpecialCaseOrbit', 'Special case orbit: circular orbit');
    end
    
    if any(abs(i) <= 1E-5)
        warning('Kep2Cart:SpecialCaseOrbit', 'Special case orbit: equatorial orbit');
    end

    % Earth gravitational constant (EGM-96) [km^3/s^2]
    mu = 3.986004418e5;

    % Semilatus rectum
    p = a .* (1 - e.^2);
    
    % Compute eccentric anomaly from mean anomaly
    EA = Mean2EccAnomaly(M,e);
    
    % Compute true anomaly from eccentric anomaly
    nu = 2 * atan(sqrt((1+e)./(1-e)).*tan(EA/2));
    
    % Position and Velocity in Perifocal Coordinate System (PQW)
    x  = (p.*cos(nu)) ./ (1 + e.*cos(nu));
    y  = (p.*sin(nu)) ./ (1 + e.*cos(nu));
    z  = zeros(sz(1),1);
    vx = -sqrt(mu./p) .* sin(nu);
    vy =  sqrt(mu./p) .* (e + cos(nu));
    vz =  z;
    
    a11 = cos(Omega).*cos(w)-sin(Omega).*sin(w).*cos(i);
    a12 = -1*cos(Omega).*sin(w)-sin(Omega).*cos(w).*cos(i);
    a13 = sin(Omega).*sin(i);
    a21 = sin(Omega).*cos(w)+cos(Omega).*sin(w).*cos(i);
    a22 = -1*sin(Omega).*sin(w)+cos(Omega).*cos(w).*cos(i);
    a23 = -1*cos(Omega).*sin(i);
    a31 = sin(w).*sin(i);
    a32 = cos(w).*sin(i);
    a33 = cos(i);
    
    col1 = dot([a11,a12,a13],[x,y,z],2);
    col2 = dot([a21,a22,a23],[x,y,z],2);
    col3 = dot([a31,a32,a33],[x,y,z],2);
    col4 = dot([a11,a12,a13],[vx,vy,vz],2);
    col5 = dot([a21,a22,a23],[vx,vy,vz],2);
    col6 = dot([a31,a32,a33],[vx,vy,vz],2);
    
    CART = [col1,col2,col3,col4,col5,col6];
    
    CART(abs(CART) <= 1E-5) = 0;
    
return

% ----------------- END OF CODE ------------------
%
% Please record any changes to the software in the change history 
% shown below:
%
% ----------------- CHANGE HISTORY ------------------
% Developer      |    Date    |     Description
% ---------------------------------------------------
% D. Plakalovic  | Mar - 2013 | Initial Development
% D. Plakalovic  | 12-11-2014 | Added more detailed commentary
% E. White       | 06-13-2023 | Added warnings for special case orbits,
%                               added errors for parabolic orbits, added
%                               input validation for size and type,
%                               updated documentation, added compliant
%                               header and footer
% E. White       | 06-30-2023 | Updated documentation, added checks for
%                               rectilinear and hyperbolic orbits
% 
% =========================================================================
%
% Copyright (c) 2023 United States Government as represented by the
% Administrator of the National Aeronautics and Space Administration.
% All Rights Reserved.
%
% =========================================================================