How to combine two matlab functions to get only one?
Ältere Kommentare anzeigen
Hi to everyone,
I've the following two matlab functions
function MA = MA_from_TA(e,TA)
% MA_FROM_TA: compute Mean anomaly of a point on elliptical orbit
% starting from orbit eccentricity and true anomaly of point.
% e - eccentricity
% TA - true anomaly of point on orbit (rad)
% MA - mean anomaly (rad)
A = (1-e*e)^0.5 * sin(TA);
B = 1 + e*cos(TA);
C = e + cos(TA);
MA = atan2(A/B,C/B) - (e*(A/B));
% atan2 returns angles in interval -pi, +pi, we need angles in interval 0,2pi
MA = mod(MA,2*pi);
end
function ToF = Time_of_Flight(GM,a,MA1,MA2)
%TIME_OF_FLIGHT: compute time of flight from a point 1 to point 2 on
% elliptical orbit
% GM - central body gravitation parameter (km^3 * s-^2)
% a - semimajor axis
n = (GM * a^3 )^0.5; %(rad/s) mean motion
ToF = (MA2 - MA1)/n;
end
I should use the first function twice to compute to different values of the variable MA, i.e. MA1 and MA2, and then pass them to the second function to compute the variable ToF (so I should use the first function for two different values of TA, i.e. TA1 and TA2 but the eccentricity is the same because we are on the same orbit).
I'd like to avoid to use separately the two functions but I want to create a single function to perform my task in a smart way (to avoid to duplicate the variables of first function for point 1 and 2), and at the end to compute ToF.
Can you show me how to do this task?
Akzeptierte Antwort
Weitere Antworten (1)
Prakash S R
am 10 Mai 2022
Bearbeitet: Prakash S R
am 10 Mai 2022
Help me understand: You want to call ToF for two points that correspond to two different values of e or TA or both, and those values affect ToF through the value of MA. Is that correct?
Then, maybe you can modify ToF to take e=[e1 e2] and TA=[TA1 TA2] as parameters, and make it the first function in the file.
function ToF = Time_of_Flight(GM,a,e,TA)
%TIME_OF_FLIGHT: compute time of flight from a point 1 to point 2 on
% elliptical orbit
% GM - central body gravitation parameter (km^3 * s-^2)
% a - semimajor axis
n = (GM * a^3 )^0.5; %(rad/s) mean motion
ToF = (MA(e(1), TA(1)) - MA(e(2), TA(2)))/n;
end
function MA = MA_from_TA(e,TA)
% MA_FROM_TA: compute Mean anomaly of a point on elliptical orbit
% starting from orbit eccentricity and true anomaly of point.
% e - eccentricity
% TA - true anomaly of point on orbit (rad)
% MA - mean anomaly (rad)
A = (1-e*e)^0.5 * sin(TA);
B = 1 + e*cos(TA);
C = e + cos(TA);
MA = atan2(A/B,C/B) - (e*(A/B));
% atan2 returns angles in interval -pi, +pi, we need angles in interval 0,2pi
MA = mod(MA,2*pi);
end
%
Alternatively, you can pass the same information as pt1 = [e1, TA1], pt2 = [e2, TA2]
function ToF = Time_of_Flight(GM,a,pt1,pt2)
%TIME_OF_FLIGHT: compute time of flight from a point 1 to point 2 on
% elliptical orbit
% GM - central body gravitation parameter (km^3 * s-^2)
% a - semimajor axis
n = (GM * a^3 )^0.5; %(rad/s) mean motion
ToF = (MA(pt1(1), pt1(2)) - MA(pt2(1), pt2(2)))/n;
end
function MA = MA_from_TA(e,TA)
% MA_FROM_TA: compute Mean anomaly of a point on elliptical orbit
% starting from orbit eccentricity and true anomaly of point.
% e - eccentricity
% TA - true anomaly of point on orbit (rad)
% MA - mean anomaly (rad)
A = (1-e*e)^0.5 * sin(TA);
B = 1 + e*cos(TA);
C = e + cos(TA);
MA = atan2(A/B,C/B) - (e*(A/B));
% atan2 returns angles in interval -pi, +pi, we need angles in interval 0,2pi
MA = mod(MA,2*pi);
end
2 Kommentare
Giuseppe
am 10 Mai 2022
Prakash S R
am 10 Mai 2022
Then the first version should still work, but with just the single value of e and TA = [TA1, TA2] as parameters to ToF. The MA function remains unchanged from what you wrote
Kategorien
Mehr zu Axes Transformations finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!