Mathematica has Sunrise[], but no Moonrise[]. But it has MoonPosition[], so I thought it would be trivial to calculate with something like

FindRoot[MoonPosition[DateObject[{2016, 9, 28, x}]][[2,1]], {x, 12}]

But the evaluation of DateObject fails in this context!

A Table works fine, but I cannot get it to evaluate the Dateobject in Solve, NSolve, FindRoot or similar.


  • $\begingroup$ It does not fail, it just complains and at the end I get {x -> 29.1625} $\endgroup$ – Kuba Sep 28 '16 at 8:33
  • 1
    $\begingroup$ Try with mp[x_?NumberQ] := MoonPosition[DateObject[{2016, 9, 28, x}]][[2, 1]] to not prompt any errors. $\endgroup$ – Kuba Sep 28 '16 at 8:34
  • $\begingroup$ Take a look here: How do I use ?NumericQ to affect order of evaluation? and User-defined functions, numerical approximation, and NumericQ which is likely a duplicate. $\endgroup$ – Kuba Sep 28 '16 at 8:36
  • 2
    $\begingroup$ Just an astronomical note: moon position varies up to a degree depending where on Earth you are, and you might want to consider refraction of 34 minutes at the horizon. If you're looking for super-accuracy (matching USNO's tables), it's possible but more difficult. $\endgroup$ – barrycarter Sep 28 '16 at 13:33
f[x_?NumberQ] := MoonPosition[DateObject[{2016, 9, 28, x}]][[2, 1]]

FindRoot[f[x], {x, 12}]

{x -> 17.5595}

ListPlot[Table[{x, f[x]}, {x, 1, 24, 0.5}], Frame -> True, FrameLabel -> {"x", "f(x)"}]

enter image description here

So another root is at about 4:

FindRoot[f[x], {x, 4}]

{x -> 3.7208}

  • $\begingroup$ Unfortunately it's extremely slow. Because MoonPosition itself is so slow that it isn't really usable for such tasks ... $\endgroup$ – Szabolcs Sep 28 '16 at 9:21
  • $\begingroup$ Thanks guys, also @Kuba! Curious, in Mathematica 10 I get {x -> 4.00933}, but I suppose this could be some rounding or insufficient resolution in MoonPosition[] (declination seem only given to 0.01 resolution) $\endgroup$ – HJensen Sep 28 '16 at 11:35
  • 5
    $\begingroup$ @HJensen MoonPosition[] uses \$GeoLocation and \$TimeZone to determine your location and time zone, so from your location, the Moon's position is different, and hence FindRoot[] finds a different time for Moon rise. $\endgroup$ – creidhne Sep 28 '16 at 14:23
  • $\begingroup$ @creidhne Good point! Anyway, the AngularDegree returned by MoonPosition[] seems to have a resolution of (only) 3-4 digits. $\endgroup$ – HJensen Sep 29 '16 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.