13

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?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
HJensen
  • 537
  • 2
  • 12

3 Answers3

10
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}

corey979
  • 23,947
  • 7
  • 58
  • 101
  • 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 09: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
  • 5
    @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
  • 1
    Trying to reproduce this in Mathematica 13.1, I get the error message that a singular Jacobian was encountered and it returns a root of the initial value I provided. Also, the ListPlot code from this answer looks different, I assume because of granularity updates to Date Objects. I thought the problems might be related. I updated the function definition to f[x_?NumberQ] := MoonPosition[DateObject[{2016, 9, 28, x, 0, 0}]][[2, 1]] and it restores the ListPlot behavior, but I still receive the Jacobian error. – Karl Feb 14 '24 at 13:17
5

You can use PlanetaryMoonData:

PlanetaryMoonData["Moon"]["RiseTime", "Date" -> DateObject[{2016, 9, 28}]]
(* DateObject[{2016, 9, 28, 4, 8}, "Minute", "Gregorian", 2.] *)

PlanetaryMoonData["Moon"]["SetTime", "Date" -> DateObject[{2016, 9, 28}]] (* DateObject[{2016, 9, 28, 17, 46}, "Minute", "Gregorian", 2.] *)

or an older function AstronomicalData, which was superseded in version 10:

AstronomicalData["Moon", {"NextRiseTime", DateObject[{2016, 9, 28}]}]
(* DateObject[{2016, 9, 28, 3, 6, 9.06516}, "Instant", "Gregorian", 1.] *)

AstronomicalData["Moon", {"NextSetTime", DateObject[{2016, 9, 28}]}] (* DateObject[{2016, 9, 28, 16, 43, 52.872}, "Instant", "Gregorian", 1.] *)

Two remarks: (i) AstronomicalData seems to give a result with finer granularity. (ii) The slight discrepancy in the results can be resolved by explicitly providing the location.

PlanetaryMoonData["Moon"]["RiseTime", {"Date" -> DateObject[{2016, 9, 28}], 
  "Location" -> GeoPosition[{46, 15}]}]
(* DateObject[{2016, 9, 28, 4, 6}, "Minute", "Gregorian", 2.] *)

AstronomicalData["Moon", {"NextRiseTime", DateObject[{2016, 9, 28}], {46, 15}}] (* DateObject[{2016, 9, 28, 3, 6, 16.2692}, "Instant", "Gregorian", 1.]*)

Domen
  • 23,608
  • 1
  • 27
  • 45
  • Much nicer than any option so far. The Documentation indicates that AstronomicalData has been superseded by other functions. There is both PlanetaryMoonData and the PlanetaryMoon entity, the former of which basically just produces the latter. There's a slightly different syntax to accessing the information, and I think the usage in the Documentation for PlanetaryMoon might have an error. Here's what works: PlanetaryMoonData["Moon"]["RiseTime","Date"->Now] Which is different from the suggested usage at the top: PlanetaryMoonData["Moon"][Dated["RiseTime",Now]] – Karl Feb 14 '24 at 16:38
  • @Karl, thank you for your comment! I have included that in the answer. – Domen Feb 14 '24 at 16:59
2

This accepted solution appears to no longer work in Mathematica 13.1, I believe because of updates to how granularity of dates are handled.

Borrowing heavily from previous answers, I stumbled on a solution that works for me.

f[x_?NumberQ] := 
 MoonPosition[DateObject[{2024, 9, 28, x, 0, 0}]][[2, 1]]
minimizeResult = NMinimize[Abs[f[x]], x]
crossingHour = x /. minimizeResult[[2]]
Karl
  • 316
  • 1
  • 8