# Calculate moonrise

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.

Suggestions?

• It does not fail, it just complains and at the end I get {x -> 29.1625} – Kuba Sep 28 '16 at 8:33
• Try with mp[x_?NumberQ] := MoonPosition[DateObject[{2016, 9, 28, x}]][[2, 1]] to not prompt any errors. – Kuba Sep 28 '16 at 8:34
• 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. – Kuba Sep 28 '16 at 8:36
• 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. – 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)"}]


So another root is at about 4:

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


{x -> 3.7208}

• Unfortunately it's extremely slow. Because MoonPosition itself is so slow that it isn't really usable for such tasks ... – Szabolcs Sep 28 '16 at 9:21
• 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) – HJensen Sep 28 '16 at 11:35
• @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. – creidhne Sep 28 '16 at 14:23
• @creidhne Good point! Anyway, the AngularDegree returned by MoonPosition[] seems to have a resolution of (only) 3-4 digits. – HJensen Sep 29 '16 at 16:27