# How to combine two matlab functions to get only one?

5 visualizaciones (últimos 30 días)
Giuseppe el 10 de Mayo de 2022
Editada: Jon el 10 de Mayo de 2022
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?
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Jon el 10 de Mayo de 2022
Editada: Jon el 10 de Mayo de 2022
This would be one approach, in which you keep your original MA_from_TA function, but within Time_of_Flight you call it twice
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
% e - length two vector of eccentricity values
% Ta - length two vector of true anomalies of points on orbit (rad)
% compute the mean anomaly for each case
MA = zeros(2); % preallocate
for k = 1:2
MA(k) = MA_from_TA(e(k),TA(k));
end
n = (GM * a^3 )^0.5; %(rad/s) mean motion
ToF = (MA(2) - MA(1))/n;
end
You could also vectorize your calculation of MA using .* .^ etc to do element by element operations and since it is only a few lines include it in your Time_of_Flight function like this:
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
% e - length two vector of eccentricity values
% Ta - length two vector of true anomalies of points on orbit (rad)
% compute the mean anomaly for each case
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);
n = (GM * a^3 )^0.5; %(rad/s) mean motion
ToF = (MA(2) - MA(1))/n;
end
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Jon el 10 de Mayo de 2022
Editada: Jon el 10 de Mayo de 2022
I saw your further explanation that there is just one value of e and two values of TA.
My second approach will work using arguments with these dimensions. For the first approach you would just have to put two identical values for e in the input vector. In either case appropriately modify the input description comments.
You could clean up the second approach a little knowing that e is a scalar and not use .* where not necessary as follows:
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
% e - eccentricity value
% Ta - length two vector of true anomalies of points on orbit (rad)
% compute the mean anomaly for each case
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);
n = (GM * a^3 )^0.5; %(rad/s) mean motion
ToF = (MA(2) - MA(1))/n;
end

Iniciar sesión para comentar.

### Más respuestas (1)

Prakash S R el 10 de Mayo de 2022
Editada: Prakash S R el 10 de Mayo de 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 comentariosMostrar NingunoOcultar Ninguno
Giuseppe el 10 de Mayo de 2022
Sorry for my imprecise explanation, one value of eccentricity and two values of true anomaly.
Prakash S R el 10 de Mayo de 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

Iniciar sesión para comentar.

### Categorías

Más información sobre Modeling the Earth en Help Center y File Exchange.

R2022a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by