How to combine two matlab functions to get only one?

조회 수: 3 (최근 30일)
Giuseppe
Giuseppe 2022년 5월 10일
편집: Jon 2022년 5월 10일
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?

채택된 답변

Jon
Jon 2022년 5월 10일
편집: Jon 2022년 5월 10일
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
Jon
Jon 2022년 5월 10일
편집: Jon 2022년 5월 10일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Prakash S R
Prakash S R 2022년 5월 10일
편집: Prakash S R 2022년 5월 10일
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
Giuseppe
Giuseppe 2022년 5월 10일
Sorry for my imprecise explanation, one value of eccentricity and two values of true anomaly.
Prakash S R
Prakash S R 2022년 5월 10일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Agriculture에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by