Advertisement
Back__Fire

MatLab

Sep 14th, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.44 KB | None | 0 0
  1. Appendix 2 – Week 2 Assisting MatLab Scripts
  2. 2A – Kepler’s Universal solution for Universal Anomaly
  3. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. function x = kepler_U(dt, ro, vro, a)
  5. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. %{
  7. This function uses Newton's method to solve the universal
  8. Kepler equation for the universal anomaly.
  9. mu - gravitational parameter (km^3/s^2)
  10. x - the universal anomaly (km^0.5)
  11. dt - time since x = 0 (s)
  12. ro - radial position (km) when x = 0
  13. vro - radial velocity (km/s) when x = 0
  14. a - reciprocal of the semimajor axis (1/km)
  15. z - auxiliary variable (z = a*x^2)
  16. C - value of Stumpff function C(z)
  17. S - value of Stumpff function S(z)
  18. n - number of iterations for convergence
  19. nMax - maximum allowable number of iterations
  20. User M-functions required: stumpC, stumpS
  21. % ----------------------------------------------
  22. global mu
  23. %...Set an error tolerance and a limit on the number of iterations:
  24. error = 1.e-8;
  25. nMax = 1000;
  26. %...Starting value for x:
  27. x = sqrt(mu)*abs(a)*dt;
  28. %...Iterate on Equation 3.65 until until convergence occurs within
  29. %...the error tolerance:
  30. n = 0;
  31. ratio = 1;
  32. while abs(ratio) > error && n <= nMax
  33.     n = n + 1;
  34.     C = stumpC(a*x^2);
  35.     S = stumpS(a*x^2);
  36.     F = ro*vro/sqrt(mu)*x^2*C + (1 - a*ro)*x^3*S + ro*x - sqrt(mu)*dt;
  37.     dFdx = ro*vro/sqrt(mu)*x*(1 - a*x^2*S) + (1 - a*ro)*x^2*C + ro;
  38.     ratio = F/dFdx;
  39.     x = x - ratio;
  40. end
  41. %...Deliver a value for x, but report that nMax was reached:
  42. if n > nMax
  43.     fprintf('\n **No. iterations of Kepler''s equation = %g', n)
  44.     fprintf('\n F/dFdx = %g\n', F/dFdx)
  45. end
  46. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  47. 2B – Computation of f and g Lagrange Coefficients
  48. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  49. function [f, g] = f_and_g(x, t, ro, a)
  50. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  51. %{
  52. This function calculates the Lagrange f and g coefficients.
  53. mu - the gravitational parameter (km^3/s^2)
  54. a - reciprocal of the semimajor axis (1/km)
  55. ro - the radial position at time to (km)
  56. t - the time elapsed since ro (s)
  57. x - the universal anomaly after time t (km^0.5)
  58. f - the Lagrange f coefficient (dimensionless)
  59. g - the Lagrange g coefficient (s)
  60. User M-functions required: stumpC, stumpS
  61. %}
  62. % ----------------------------------------------
  63. global mu
  64. z = a*x^2;
  65. %...Equation 3.69a:
  66. f = 1 - x^2/ro*stumpC(z);
  67. %...Equation 3.69b:
  68. g = t - 1/sqrt(mu)*x^3*stumpS(z);
  69. end
  70. 2C – Computation of the derivation of f and g Lagrange Coefficients
  71. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  72. function [fdot, gdot] = fDot_and_gDot(x, r, ro, a)
  73. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  74. %{
  75. This function calculates the time derivatives of the
  76. Lagrange f and g coefficients.
  77. mu - the gravitational parameter (km^3/s^2)
  78. a - reciprocal of the semimajor axis (1/km)
  79. ro - the radial position at time to (km)
  80. t - the time elapsed since initial state vector (s)
  81. r - the radial position after time t (km)
  82. x - the universal anomaly after time t (km^0.5)
  83. fdot - time derivative of the Lagrange f coefficient (1/s)
  84. gdot - time derivative of the Lagrange g coefficient (dimensionless)
  85. User M-functions required: stumpC, stumpS
  86. %}
  87. % --------------------------------------------------
  88. global mu
  89. z = a*x^2;
  90. %...Equation 3.69c:
  91. fdot = sqrt(mu)/r/ro*(z*stumpS(z) - 1)*x;
  92. %...Equation 3.69d:
  93. gdot = 1 - x^2/r*stumpC(z);
  94. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  95. 2D – Computation of the Stumpff Function C(z)
  96. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  97. function c = stumpC(z)
  98. % ~~~~~~~~~~~~~~~~~~~~~~
  99. %{
  100. This function evaluates the Stumpff function C(z) according
  101. to Equation 3.53.
  102. z - input argument
  103. c - value of C(z)
  104. User M-functions required: none
  105. %}
  106. % ----------------------------------------------
  107. if z > 0
  108.     c = (1 - cos(sqrt(z)))/z;
  109. elseif z < 0
  110.     c = (cosh(sqrt(-z)) - 1)/(-z);
  111. else
  112.     c = 1/2;
  113. end
  114. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  115. 2E – Computation of the Stumpff Function S(z)
  116. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  117. function s = stumpS(z)
  118. % ~~~~~~~~~~~~~~~~~~~~~~
  119. %{
  120. This function evaluates the Stumpff function S(z) according
  121. to Equation 3.52.
  122. z - input argument
  123. s - value of S(z)
  124. User M-functions required: none
  125. %}
  126. % ----------------------------------------------
  127. if z > 0
  128.     s = (sqrt(z) - sin(sqrt(z)))/(sqrt(z))^3;
  129. elseif z < 0
  130.     s = (sinh(sqrt(-z)) - sqrt(-z))/(sqrt(-z))^3;
  131. else
  132.     s = 1/6;
  133. end
  134. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement