Tutorial on how to overuse Mathematica

David Luposchainsky

Take this as a humorous example of what the usual Mathematica approach looks like. A Goldberg machine of code, so to speak.

. The problem

I wrote this as an answer to the question "Figuring when the minute and hour hand coincide on a clock" on mathematica.stackexchange.com.

. Establishing the equation

Establishing is Hebrew and stands for do until happy with trial and error. Trial and error here means coding a clock, which will give us valuable information about the system, for example whether the clock hands meet at all at some point.

polar is a wrapper generating polar coordinates: it maps the interval [0,1) to coordinates on the unit circle. minute/hour then use this map to calculate where the clock's hands should point to: the minute (or hour) hand travels around the circle once in 60 (or 12·60) minutes, therefore the parameter is divided by those numbers, making them maps from [0,60) (or [0,12·60)) to the unit circle. ticks is the same thing again, only that the ticks should "travel" around the clock once only per half day, which yields a divisor of 12.

polar[t_]:=Through[{Cos,Sin}[-2π t+(π/2)]]
minute=polar[((#)/60)]&;
hour=polar[((#)/(12 60))]&;
ticks=polar[#/12]&;
Manipulate[
	Graphics@Flatten@{
		Circle[],
		Text[#,1.1ticks[#]]&/@Range[12],
		Line[{0.9ticks[#],ticks[#]}]&/@Range[12],
		Gray,Thickness[.025],Arrowheads[.1],Arrow[{{0,0},0.7hour[t]}],
		Black,Thickness[.01],Arrowheads[.05],Arrow[{{0,0},minute[t]}]
	},
	{{t,0,"Time\n(minutes after midnight)"},0,12*60}
]

After countless hours of experimentation, I was finally able to determine a position in which (up to numerical precision) the hands did indeed overlap.

. Solving the system

The bad news is that I have no sense of reason (12/11 hour etc), luckily there's Mathematica at my hand. Let’s generate the equations …

eqns=Thread[minute[t]==hour[t]]

Wonderful! Solve it!

intersections=Sort[t/.FullSimplify@Solve[Flatten@{eqns,0≤t<12 60},t]]

. Displaying the results

We can now format this so it looks more like a time,

dates=DatePlus[{2012,1,23},{#,"Minute"}]&/@intersections;
DateString[#,{"Hour",":","Minute",":","Second"}]&/@dates

As for the visualization part, we can simply strip the Manipulate code given above a bit, add multiple hour/minute hands in return, and combine it all into a single graphic:

Graphics@Flatten@{
	Circle[],
	Text[#,1.1ticks[#]]&/@Range[12],
	Line[{0.9ticks[#],ticks[#]}]&/@Range[12],
	Gray,Thickness[.025],Arrowheads[.1],Arrow[{{0,0},0.7hour[#]}]&/@intersections,
	Black,Thickness[.01],Arrowheads[.05],Arrow[{{0,0},minute[#]}]&/@intersections
}

Done :-)