Orbital elements: Difference between revisions

→‎version 2: added input from the Perl 6 entry, added detailed output to show what data (orbital elements) are being used, added a title for the output.
m (→‎{{header|ooRexx}}: 2 more little glitches :-( good night!)
(→‎version 2: added input from the Perl 6 entry, added detailed output to show what data (orbital elements) are being used, added a title for the output.)
Line 939:
<lang rexx>/*REXX pgm converts orbital elements ──► orbital state vectors (angles are in radians).*/
numeric digits length( pi() ) - length(.) /*limited to pi len, but show 1/3 digs.*/
parsecall valueorbV orbV(1, .1, 0, 355/(113*/6), 0, 0) /*orbital elements taken from: with Java position '~' speed*/
call orbV 1, .1, pi/18, pi/6, pi/4, 0 /* " " " " Perl 6 */
say ' position:' show(position)
say ' speed:' show(speed)
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
orbV: procedure; parse arg semiMaj, eccentricity, inclination, node, periapsis, anomaly
say; say center(' orbital elements ', 99, "═")
say ' semi-major axis:' fmt(semiMaj)
say ' eccentricity:' fmt(eccentricity)
say ' inclination:' fmt(inclination)
say ' ascending node longitude:' fmt(node)
say ' argument of periapsis:' fmt(periapsis)
say ' true anomaly:' fmt(anomaly)
i= 1 0 0; j= 0 1 0; k= 0 0 1 /*define the I, J, K vectors.*/
parse value rot(i, j, node) with i '~' j /*rotate ascending node longitude.*/
Line 954 ⟶ 960:
c= cos(anomaly); s= sin(anomaly) /*calculate COS and SIN of anomaly*/
r= L / (1 + eccentricity * c)
@= s*r**2 / L; speed= MA(i, @*c - r*s, j, @*s + r*c)
returnspeed= mulV( MA(i,c,j,s), r)'~'mulV( divV( speed, absV(speed) ), sqrt(2 / r - 1 / semiMaj) )
say ' position:' show( mulV( MA(i, c, j, s), r) )
say ' speed:' show( speed)
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
absV: procedure; parse arg x y z; return sqrt(x**2 + y**2 + z**2)
Line 962 ⟶ 971:
show: procedure; parse arg a b c; return '('fmt(a)"," fmt(b)',' fmt(c)")"
fmt: procedure; parse arg #; return strip( left( left('', #>=0)# / 1, digits() %3), 'T')
MA: procedure; parse arg x y z,a@,xxa yyb zzc,b$; return (x*a@ +xx a*b$) (y*a@ +yy* b*$) (z*a@ +zz c*b$)
pi: pi= 3.1415926535897932384626433832795028841971693993751058209749445923; return pi
rot: procedure; parse arg i,j,$; return MA(i,cos($),j,sin($))'~'MA(i, -sin($), j, cos($))
Line 968 ⟶ 977:
/*──────────────────────────────────────────────────────────────────────────────────────*/
cos: procedure; arg x; x= r2r(x); if x=0 then return 1; a= abs(x); Hpi= pi * .5
numeric fuzz min(6, digits() - 3); if a=pi then return '-1'
if a=Hpi | a=Hpi*3 then return 0; if a=pi / 3 then return .5
if a=pi * 2 / 3 then return '-.5'; return .sinCos(1, '-1')
/*──────────────────────────────────────────────────────────────────────────────────────*/
sin: procedure; arg x; x= r2r(x); numeric fuzz min(5, max(1, digits() - 3) )
if x=0 then return 0; if x=pi*.5 then return 1; if x==pi*1.5 then return '-1'
if abs(x)=pi then return 0; return .sinCos(x, 1)
/*──────────────────────────────────────────────────────────────────────────────────────*/
.sinCos: parse arg z 1 _,i; xx= x*x
Line 981 ⟶ 990:
sqrt: procedure; arg x; if x=0 then return 0; d= digits(); numeric form; m.= 9; h= d+6
numeric digits; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g= g *.5'e'_ % 2
do j=0 while h>9; m.j= h; h= h % 2 + 1; end
do k=j+5 to 0 by '-1'; numeric digits m.k; g= (g+x/g) * .5; end; return g</lang>
{{out|output|text=&nbsp; when using the default internal inputs:}}
<pre>
════════════════════════════════════════ orbital elements ═════════════════════════════════════════
position: ( 0.779422843398679832, 0.450000034653684237, 0)
semi-major axis: 1
speed: (-0.552770840960443759, 0.957427083179761535, 0)
eccentricity: 0.1
inclination: 0
ascending node longitude: 0.523598820058997050
argument of periapsis: 0
true anomaly: 0
position: ( 0.779422843398679832, 0.450000034653684237, 0)
speed: (-0.552770840960443759, 0.957427083179761535, 0)
 
════════════════════════════════════════ orbital elements ═════════════════════════════════════════
semi-major axis: 1
eccentricity: 0.1
inclination: 0.174532925199432957
ascending node longitude: 0.523598775598298873
argument of periapsis: 0.785398163397448309
true anomaly: 0
position: ( 0.237771283982206547, 0.860960261697715834, 0.110509023572075562)
speed: (-1.061933017480060047, 0.275850020569249507, 0.135747024865598167)
</pre>