TITLE: Is there a simple-ish function for modeling seasonal changes to day/night duration and height of the sun?
QUESTION [21 upvotes]: I'm a hobbyist programmer, and not much of a mathematician. I'm trying to model something like the seasonal change in day length. There are two other questions here that are very similar to mine, and I posted a bounty for one of them, but the answers are over my head, and I don't think I can adapt them to what I'm doing. I was thinking more something like a sine-ish function, and hoping for some easier math. Perhaps if I show my specific case, the answers can be narrowed and simplified.
What I've been able to come up with is a function getSunHeight(x, cycleDuration, dayToNightRatio). (It's not for Earth; I'm experimenting with different values in a simulation, so a 24-hour cycle isn't a given.)
In mathematical terms, getSunHeight is calculated as follows.

Let $d_{\text{cycle}}$ denote the duration of a full cycle and $r_\text{day-to-night}$ denote the ratio of day to night.
Let $$d_\text{daylight} = d_\text{cycle} \times r_\text{day-to-night}$$ and $$d_\text{darkness}= d_\text{cycle} - d_\text{daylight}$$
Then the sun height is
$$y(x)=\left\{
\begin{array}{lcl}
\sin\left(\frac{\pi x}{d_\text{daylight}}\right) & : & 0\le x\le d_\text{daylight}\\
\sin\left(\frac{\pi\left(x-d_\text{cycle}\right)}{d_\text{darkness}}\right) & : & d_\text{daylight} < x \le d_\text{cycle}
\end{array}
\right.$$

So $y=\operatorname{getSunHeight}(x, 10, 0.2)$ gives me a graph like this:

Is there some way to get rid of the hard angle at $x=d_\text{daylight}$ (i.e. daylightDuration)? It's not a problem if the shape of the curve changes slightly; in fact it might be better, more realistic.
Also, I'm not trying for a general case where I specify the latitude. I'm looking for something that assumes I'm at a fixed latitude. Further, although I'm trying to model a change in the period, I'm not particularly attached to that approach. It was suggested that I try to vary the height of the sun and keep the period the same. After lots of experimentation on Desmos, I'm still at a loss.
I've been experimenting with averaging the slopes at that discontinuity, and using that average somewhere in the equation, but I haven't been able to make any headway.
News: With inspiration from the comments, I've finally realized that I need to think about the entire winter/summer cycle, not just one day/night cycle. I think I almost have it solved:

Let $d_{\text{annualCycle}}$ denote the duration of a full summer/winter cycle, expressed in full day/night cycles
Let $d_{\text{diurnalCycle}}$ denote the duration of a full day/night cycle
Let $d_{\text{daylight}}$ denote the duration of daylight for one day/night cycle
Let $d_{\text{darkness}}$ denote the duration of darkness for one day/night cycle
Let $r_{\text{day-to-night}}$ denote $d_{\text{daylight}}:d_{\text{diurnalCycle}}$ at the first solstice! At the second solstice, the ratio is 1 - $r_{\text{day-to-night}}$, and at the equinoxes, the day/night ratio is 1:1 (d'oh!)
Finally, rather than thinking of the sun's height, with all that angle stuff, I'll think of the function as a kind of temperature reading. So with a function
y = getTemperature(x, $d_{\text{diurnalCycle}}$, $d_{\text{annualCycle}}$, $r_{\text{day-to-night}}$)
I've come up with this:
Let yearFullDuration = $d_{\text{annualCycle}} \ x \ d_{\text{diurnalCycle}}$
Let $r_{\text{night-to-day}} = 1 - r_{\text{day-to-night}}$
Let $c=\left(r_{\text{night-to-day}}-r_{\text{day-to-night}}\right)\sin\left(\frac{2\pi r_{\text{night-to-day}}}{d_{\text{diurnalCycle}} r_{\text{day-to-night}}}\right)+r_{\text{night-to-day}}$
$y = \sin\left(\frac{2\pi xd_{\text{diurnalCycle}}}{\text{yearFullDuration}}\right) + \sin\left(\frac{1.3 cxr_{\text{night-to-day}}}{\text{yearFullDuration}}\right)$

It gives me a graph like the following. As you can see, the zeros don't land quite where they're supposed to. I put in a fudge factor of 1.3, which is incredibly unsatisfying, but I haven't yet figured out how to the crossings right.

More News:
Again, with much inspiration and help from the comments, I've figured out the easier case of just adding the seasonal sine to the diurnal sine. The thing that was eluding me--the reason for the fudge factor of 1.3--was the need to square one of the ratios in the seasonal sine:

Let $d_{\text{diurnal}}$ denote the duration of one day/night cycle
Let $d_{\text{annual}}$ denote the number of full diurnal cycles in one summer/winter cycle
Let $d_{\text{full-year}}=d_{\text{annual}}*d_{\text{diurnal}}$
Let $r_{s}$ denote the ratio of daylight duration to $d_{diurnal}$ at the summer (first) solstice
Let $f_{a}=\sin\left(\frac{2xr_{s}^{2}}{d_{\text{full-year}}}\right)$ -- the annual curve
Let $f_{d}=\sin\left(\frac{2\pi xd_{\text{diurnal}}}{d_{\text{full-year}}}\right)$ -- the diurnal curve
And finally
$y=\frac{1}2\sin\left(f_{a}+f_{d}\right)$

The graph comes out looking like one might expect if one were more math-oriented. I'm still very curious to see whether there's a way to smoothly vary the daylight/darkness ratio as the seasons progress (my original idea, extended over the course of a year rather than just one day). I've been all over that one and not made any progress.

REPLY [2 votes]: Here, to make it easier to harmonise our conclusions, I'll use the
standard notation $\varepsilon$ for the axial tilt of the Earth, or
that of an imaginary planet.
From Axial tilt - Wikipedia:

Earth's orbital plane is known as the ecliptic plane, and Earth's
tilt is known to astronomers as the obliquity of the ecliptic, being
the angle between the ecliptic and the celestial equator on the
celestial sphere.  It is denoted by the Greek letter $\varepsilon.$


From Earth's orbit - Wikipedia:

From a vantage point above the north pole of either the Sun or
Earth, Earth would appear to revolve in a counterclockwise direction
around the Sun. From the same vantage point, both the Earth and the
Sun would appear to rotate also in a counterclockwise direction
about their respective axes.

The angle $\theta$ used in my answer and the angle $\beta$ used in
JonathanZ's answer, if I have read it correctly, are
shown together here.  The diagram takes the position of the Sun, and
the Earth's equinoxes and solstices, as fixed, and the Earth's solar
orbit as circular.  The same diagram will also be used for fictional
planets.

That is,
$$
\theta \equiv \beta - \frac\pi2 \pmod{2\pi}.
$$
There is a confusing variety of similar-looking but incompatible
spherical coordinate systems.
Many use the Greek letter $\varphi$ to denote either the polar angle
(colatitude, inclination angle) or its complement, the elevation
angle.  Nobody uses the alternative form of the same Greek letter,
$\phi,$ so of course that's what I foolishly chose to use!  The
choice was especially unfortunate because $\phi$ is the standard
notation for latitude, as correctly used in JonathanZ's answer.  My
simplifying assumption made the problem invisible, but now a saner
choice must be made.
No choice is without its problems, but for now, at least, I shall
use $\psi$ in place of $\phi$ as it was used in my answer.  If it is
necessary to refer to longitude, I shall use the letter $\lambda.$
Thus, $[1, \theta, \psi]$ and $[1, \lambda, \phi]$ are coordinates
in two different spherical systems for the surface of the planet.
(Ideally, I should not use $\theta$ in this way, but it will
usually have the value defined above, only sometimes being
used for $\theta + \pi \pmod{2\pi}.$ I don't think the confusion is
serious enough to warrant another change of notation.) When more
that one point is involved, I shall continue with the practice of
using subscripts to distinguish the coordinate values of one from
another.
The "simplifying assumption" just referred to is that we were
only interested in the experiences of people, or alien beings, on
the "tropic of Cancer" of their respective planets, i.e., the
circle of latitude defined by $\phi = \varepsilon.$ That was because
I imagined that the equations for the general case would get messy.
Even with the simplifying assumption, my equations did get messy.
I understood later that this was because I had missed something
obvious.  If $X$ and $Y$ are points on the respective "great
semicircles" defined by $\theta$ and $\theta + \pi \pmod{2\pi},$
then it is (or should have been) clear that the distance $\|XY\|$
is given by
$$
\|XY\| = 2\sin\frac{\pi - \psi_X - \psi_Y}2 =
2\cos\frac{\psi_X + \psi_Y}2.
$$

It should now be possible to treat the general case in my notation
as well as in JonathanZ's notation, and thereby reconcile the two
answers.
[More than one Community Wiki post might be needed, because
this one is already quite long.]
I'm particularly interested in
checking the realism of the results for the Earth, at several
latitudes, and at several times of year - do our simplifications
lead to any serious errors?

For simplicity, I will continue to assume that we are in the
Northern hemisphere, i.e., $\phi \geqslant 0.$ (Symmetry can be used
to get results for the Southern hemisphere; or else we can just drop
the restriction, if it turns out not to simplify anything after all.)
In order for $P$ and $R$ to be inside the circle of latitude ($P$ on
the surface of the planet, $R$ inside it), we require
$$
\phi + \varepsilon < \frac\pi2.
$$
In spite of the appearance of the figure, it is not required
that $\phi > \epsilon.$ The case $\phi = \varepsilon$ is the one
already considered in my answer, i.e., the tropic of Cancer.  The
case $\phi = 0$ is the Equator.  The line through $P$ perpendicular
to $SN$ is the projection of the Arctic Circle, the upper bound of
latitudes for which there is always an alternation of day and night,
i.e., the circle of latitude $\frac\pi2 - \varepsilon.$
The radius of the circle of latitude is
$$
\|CU\| = \|CV\| = \cos\phi,
$$
and the important point $R$ is given by
$$
\|CR\| = \sin\phi\tan\varepsilon.
$$
(In the case already considered,
$\phi = \varepsilon = \frac\pi2 - \alpha,$ whence
$\|CR\| = \cos\alpha\cot\alpha.$)
Denoting by $a(\varepsilon, \phi, \theta)$ the fraction of the
circle of latitude $\phi$ that is in daylight at the time of year
given by the angle $\theta,$ we have:
\begin{equation}
\label{3766767:eq:3}\tag{$3$}
a(\varepsilon, \phi, \theta + \pi) = a(\varepsilon, -\phi, \theta) =
1 - a(\varepsilon, \phi, \theta)
\quad \left(\varepsilon \geqslant 0, \
|\phi| < \frac\pi2 - \varepsilon\right)
\end{equation}
where addition of angles is modulo $2\pi.$ It is therefore enough to
give a formula for the case $\phi \geqslant 0,$
$\pi \leqslant \theta \leqslant 2\pi.$ The result turns
out to be quite simple and neat:
\begin{gather}
\label{3766767:eq:4}\tag{$4$}
a(\varepsilon, \phi, \theta) = \frac1\pi\sin^{-1}\sqrt{\frac
{1 - \sec^2\phi\sin^2\varepsilon\sin^2\theta}
{1 - \sin^2\varepsilon\sin^2\theta}} \\
\notag
\left(\varepsilon \geqslant 0, \ \phi \geqslant 0, \
\phi + \varepsilon < \frac\pi2, \
\pi \leqslant \theta \leqslant 2\pi\right).
\end{gather}
At Northern latitudes, i.e., when $\phi \geqslant 0,$ the values of
$a$ at the solstices are:
\begin{gather}
\label{3766767:eq:5}\tag{$5$}
a_\text{max}(\varepsilon, \phi) =
a\left(\varepsilon, \phi, \frac{\pi}2\right) =
\frac12 + \frac{\sin^{-1}(\tan\varepsilon\tan\phi)}\pi, \\
\notag
a_\text{min}(\varepsilon, \phi) =
a\left(\varepsilon, \phi, \frac{3\pi}2\right) =
\frac12 - \frac{\sin^{-1}(\tan\varepsilon\tan\phi)}\pi.
\end{gather}
I don't yet know a neat way to derive equation \eqref{3766767:eq:4},
although presumably it can be done by constructing a few
cunningly-chosen right-angled triangles.  For the moment, I'll give
two derivations, both of which are unfortunately quite messy.
First method
In Cartesian coordinates, the North pole $N$ is
$$
\mathbf{n} = (\sin\varepsilon, 0, \cos\varepsilon),
$$
and the centre, $C,$ of the circle of latitude $\phi$ is
$$
\mathbf{c} = (\sin\phi)\mathbf{n} =
(\sin\varepsilon\sin\phi, 0, \cos\varepsilon\sin\phi).
$$
A point $J$ on the planet's surface whose Cartesian coordinates are
$\mathbf{j} = (x, y, z)$ lies on the circle of latitude $\phi$ iff
$\mathbf{j}\cdot\mathbf{n} = \mathbf{c}\cdot\mathbf{n},$ i.e., iff
$$
x\sin\varepsilon + z\cos\varepsilon = \sin\phi.
$$
If $\mathbf{j} = (0, 0, \pm1),$ then
$$
|\mathbf{j}\cdot\mathbf{n}| = \cos\varepsilon =
\sin\left(\frac\pi2 - \varepsilon\right) > |\sin\phi|,
$$
so $J$ does not lie on the plane, and we may ignore those points.
If $\mathbf{j} \ne (0, 0, \pm1),$ then $J$ has well-defined
spherical polar coordinates $[1, \theta, \psi],$ where
$$
(x, y, z) =
(\cos\psi\cos\theta, \, \cos\psi\sin\theta, \, \sin\psi),
\quad |\psi| < \frac\pi2.
$$
In terms of these coordinates, the equation of the plane is
\begin{equation}
\label{3766767:eq:6}\tag{$6$}
\sin\varepsilon\cos\psi\cos\theta + \cos\varepsilon\sin\psi
= \sin\phi.
\end{equation}
Claim: For all $\varepsilon \geqslant 0,$ all $\phi \in
\left(-\frac\pi2 + \varepsilon, \frac\pi2 - \varepsilon\right),$ and
all real $\theta,$ equation \eqref{3766767:eq:6} has at least
one solution for $\psi \in \left(-\frac\pi2, \frac\pi2\right).$ This
follows from the Intermediate Value Theorem, because the left hand
side of \eqref{3766767:eq:6} is nearly equal to $\pm\cos\varepsilon$
when $\psi$ is nearly equal to $\pm\frac\pi2$ respectively, and we
have just observed, when considering the points $(0, 0, \pm1),$ that
$\cos\varepsilon > |\sin\phi|.$ $\ \square$
The value of the coordinate $\psi$ is uniquely determined by the
value of
$$
t = \tan\frac\psi2 \quad (|t| < 1).
$$
In terms of this parameter $t,$ equation \eqref{3766767:eq:6}
becomes
$$
(\sin\varepsilon\cos\theta)\frac{1 - t^2}{1 + t^2} +
(\cos\varepsilon)\frac{2t}{1 + t^2} = \sin\phi,
$$
i.e.,
\begin{equation}
\label{3766767:eq:7}\tag{$7$}
(\sin\phi + \sin\varepsilon\cos\theta)t^2 - 2(\cos\varepsilon)t
+ (\sin\phi - \sin\varepsilon\cos\theta) = 0.
\end{equation}
Consider also the same equation in which $\theta$ is replaced by
$\theta + \pi \pmod{2\pi},$ i.e.,
\begin{equation}
\label{3766767:eq:7p}\tag{$7^*$}
(\sin\phi - \sin\varepsilon\cos\theta)t^2 - 2(\cos\varepsilon)t
+ (\sin\phi + \sin\varepsilon\cos\theta) = 0.
\end{equation}
Bearing in mind once again the inequality
$\cos\varepsilon > |\sin\phi|,$ together with the requirement
$|t| < 1,$ we find: (i) if $\sin\varepsilon\cos\theta = \sin\phi,$
then the only admissible solution of \eqref{3766767:eq:7} is
$t_X = 0,$ and the only admissible solution of \eqref{3766767:eq:7p}
is $t_Y = \sin\phi/\cos\varepsilon$; (ii)
if $\sin\varepsilon\cos\theta = -\sin\phi,$ then the only admissible
solution of \eqref{3766767:eq:7} is
$t_X = \sin\phi/\cos\varepsilon,$ and the only admissible solution
of \eqref{3766767:eq:7p} is $t_Y = 0.$ In either of these
exceptional cases (i) and (ii), therefore, we have:
\begin{equation}
\label{3766767:eq:8}\tag{$8$}
t_X + t_Y = \frac{\sin\phi}{\cos\varepsilon}; \quad t_Xt_Y = 0.
\end{equation}
Suppose now that $\sin\varepsilon\cos\theta \ne \pm\sin\phi.$ Then
neither \eqref{3766767:eq:7} nor \eqref{3766767:eq:7p} has zero as a
root, and the roots of one equation are the reciprocals of the roots
of the other.  Because of the requirement $|t| < 1,$ it follows that
\eqref{3766767:eq:7} has only one admissible solution $t = t_X,$ and
\eqref{3766767:eq:7p} has only one admissible solution $t = t_Y,$
where:
\begin{align*}
t_X & = \frac{\cos\varepsilon - \sqrt{\cos^2\varepsilon -
(\sin^2\phi - \sin^2\varepsilon\cos^2\theta)}}
{\sin\phi + \sin\varepsilon\cos\theta}, \\
t_Y & = \frac{\cos\varepsilon - \sqrt{\cos^2\varepsilon -
(\sin^2\phi - \sin^2\varepsilon\cos^2\theta)}}
{\sin\phi - \sin\varepsilon\cos\theta}.
\end{align*}
To simplify these formulae, we write
$$
A = \sqrt{\cos^2\varepsilon -
(\sin^2\phi - \sin^2\varepsilon\cos^2\theta)} =
\sqrt{\cos^2\phi - \sin^2\varepsilon\sin^2\theta}.
$$
This is well-defined (as indeed it was bound to be), because:
$$
\cos^2\phi = \sin^2\left(\frac\pi2 - |\phi|\right) >
\sin^2\varepsilon \geqslant \sin^2\varepsilon\sin^2\theta.
$$
Recalling the reciprocal relationship between \eqref{3766767:eq:7}
and \eqref{3766767:eq:7p}, we obtain:
\begin{align*}
t_X & =
\frac{\cos\varepsilon - A}{\sin\phi + \sin\varepsilon\cos\theta} =
\frac{\sin\phi - \sin\varepsilon\cos\theta}{\cos\varepsilon + A}, \\
t_Y & =
\frac{\cos\varepsilon - A}{\sin\phi - \sin\varepsilon\cos\theta} =
\frac{\sin\phi + \sin\varepsilon\cos\theta}{\cos\varepsilon + A}.
\end{align*}
This gives:
\begin{equation}
\label{3766767:eq:9}\tag{$9$}
t_X + t_Y = \frac{2\sin\phi}{\cos\varepsilon + A}, \quad
t_Xt_Y = \frac{\cos\varepsilon - A}{\cos\varepsilon + A}.
\end{equation}
In the special cases (i) and (ii) defined by
$\sin\varepsilon\cos\theta = \pm\sin\phi,$ we have
$A = \cos\varepsilon,$ therefore \eqref{3766767:eq:8} is a special
case of \eqref{3766767:eq:9}, therefore \eqref{3766767:eq:9} holds
in all cases.
Just as before, with only a change of notation:
$$
a = \begin{cases}
1 - \dfrac1\pi\sin^{-1}\dfrac{\|XY\|}{2\cos\phi}
& (0 \leqslant \theta \leqslant \pi), \\[1.5ex]
\dfrac1\pi\sin^{-1}\dfrac{\|XY\|}{2\cos\phi}
& (\pi \leqslant \theta \leqslant 2\pi),
\end{cases}
$$
and
$$
\frac{\|XY\|}2 = \cos\frac{\psi_X + \psi_Y}2 =
\frac{1 - t_Xt_Y}{\sqrt{1 + t_X^2}\sqrt{1 + t_Y^2}}.
$$
From \eqref{3766767:eq:9},
\begin{gather*}
(1 + t_X^2)(1 + t_Y^2) =
1 + (t_X + t_Y)^2 - 2t_Xt_Y + t_X^2t_Y^2 \\
= \frac{(\cos\varepsilon + A)^2 + 4\sin^2\phi -
2(\cos^2\varepsilon - A^2) + (\cos\varepsilon - A)^2}
{(\cos\varepsilon + A)^2} \\
= \frac{4A^2 + 4\sin^2\phi}{(\cos\varepsilon + A)^2}, \\
\therefore\ \frac{(t_X + t_Y)^2}{(1 + t_X^2)(1 + t_Y^2)} =
\frac{\sin^2\phi}{A^2 + \sin^2\phi},
\\ \therefore\ \frac{(1 - t_Xt_Y)^2}{(1 + t_X^2)(1 + t_Y^2)} =
1 - \frac{(t_X + t_Y)^2}{(1 + t_X^2)(1 + t_Y^2)} =
\frac{A^2}{A^2 + \sin^2\phi} =
\frac{\cos^2\phi - \sin^2\varepsilon\sin^2\theta}
{1 - \sin^2\varepsilon\sin^2\theta}, \\
\therefore\
\frac{1 - t_Xt_Y}{\sqrt{1 + t_X^2}\sqrt{1 + t_Y^2}\cos\phi} =
\sqrt{\frac{1 - \sec^2\phi\sin^2\varepsilon\sin^2\theta}
{1 - \sin^2\varepsilon\sin^2\theta}}.
\end{gather*}
This completes the first proof of  \eqref{3766767:eq:4}. $\ \square$
Second method
The projection of the circle of latitude $\phi$ onto the $(x, y)$
plane is an ellipse with semi-major axis $\cos\phi,$ semi-minor axis
$\cos\varepsilon\cos\phi,$ and centre
$(\sin\varepsilon\sin\phi, 0),$ so its equation is
$$
\left(\frac{x - \sin\varepsilon\sin\phi}{\cos\varepsilon}\right)^2 +
y^2 = \cos^2\phi.
$$
The points $X, Y$ project onto the points of intersection $X', Y'$
of the ellipse with the straight line
$\{t(\cos\theta, \sin\theta) : t \in \mathbb{R}\}.$ The length of
the chord $X'Y'$ is equal to the absolute difference of the roots of
the resulting quadratic equation for $t$:
$$
\left(\frac{t\cos\theta - \sin\varepsilon\sin\phi}{\cos\varepsilon}
\right)^2 + t^2\sin^2\theta = \cos^2\phi.
$$
We rewrite this equation successively as
\begin{gather*}
(\cos^2\theta + \cos^2\varepsilon\sin^2\theta)t^2 -
2(\sin\varepsilon\sin\phi\cos\theta)t +
(\sin^2\varepsilon\sin^2\phi - \cos^2\varepsilon\cos^2\phi) = 0, \\
(1 - \sin^2\varepsilon\sin^2\theta)t^2 -
2(\sin\varepsilon\sin\phi\cos\theta)t -
(1 - \sin^2\varepsilon - \sin^2\phi) = 0,
\end{gather*}
which gives
$$
\frac{\|X'Y'\|}2 = \frac
{\sqrt{\sin^2\varepsilon\sin^2\phi\cos^2\theta +
(1 - \sin^2\varepsilon\sin^2\theta)
(1 - \sin^2\varepsilon - \sin^2\phi)}}
{1 - \sin^2\varepsilon\sin^2\theta}
$$
From the figure below,
$$
\|XY\| = \|X'Y'\|\sec\eta =
\|X'Y'\|\sqrt{1 + \tan^2\varepsilon\cos^2\theta} =
\frac{\|X'Y'\|\sqrt{1 - \sin^2\varepsilon\sin^2\theta}}
{\cos\varepsilon}.
$$

Substituting into the expression for $a$ in terms of $\|XY\|,$ and
simplifying (a lot!), we end up with \eqref{3766767:eq:4}. $\ \square$
# ~\Work\Comp\Python\3\Lib\maths\latitude.py
#
# Wed 12 Aug 2020  (created)
# Fri 14 Aug 2020  (updated)
"""
Day/night cycle: https://math.stackexchange.com/q/3766767.

See also previous question: https://math.stackexchange.com/q/3339606.

Has been run using Python 3.8.1 [MSC v.1916 64 bit (AMD64)] on win32.
"""

__all__ = ['circle']

from math import asin, fabs, pi, radians, sin, sqrt
import matplotlib.pyplot as plt
import numpy as np

class circle(object):
    # Wed 12 Aug 2020  (created)
    # Fri 14 Aug 2020  (updated)
    """
    A circle of latitude on a spherical planet.
    """
    
    def __init__(self, lati=4/5, tilt=5/13):
        # Wed 12 Aug 2020  (created)
        # Thu 13 Aug 2020  (updated)
        """
        Create circle, given sines of latitude and axial tilt.
        """
        self.lsin = lati
        self.tsin = tilt
        self.lcossq = 1 - self.lsin**2
        self.tsinsq = self.tsin**2
        self.amax = self.day_frac(1/4)
    
    def day_frac(self, x, tolerance=.000001):
        # Wed 12 Aug 2020  (created)
        # Thu 13 Aug 2020  (updated)
        """
        Compute daylight fraction of cycle as a function of time of year.
        """
        sin2pix = sin(2*pi*x)
        if fabs(sin2pix) < tolerance:  # near an equinox
            return 1/2
        else:
            sin2pixsq = sin2pix**2
            expr = self.tsinsq*sin2pixsq
            a = asin(sqrt((1 - expr/self.lcossq)/(1 - expr)))/pi
            if sin2pix > 0:  # k < x < k + 1/2 for some integer k
                return 1 - a
            else:            # k - 1/2 < x < k for some integer k
                return a
    
    def compare(self, xsz=8.0, ysz=6.0, N=600):
        # Wed 12 Aug 2020  (created)
        # Fri 14 Aug 2020  (updated)
        """
        Plot the daylight fraction as a function of the time of year.
        """
        plt.figure(figsize=(xsz, ysz))
        plt.title(r'Annual variation of day length at latitude ' +
            r'${:.2f}^\circ$ when axial tilt is ${:.2f}^\circ$'.format(
            asin(self.lsin)*180/pi, asin(self.tsin)*180/pi))
        plt.xlabel('Time from Spring equinox')
        plt.ylabel('Daylight fraction of cycle')
        xvals = np.linspace(0, 1, N)
        yvals = [1/2 + (self.amax - 1/2)*sin(2*pi*x) for x in xvals]
        plt.plot(xvals, yvals, label='Sine function', c='k', ls=':', lw=.75)
        yvals = [self.day_frac(x) for x in xvals]
        plt.plot(xvals, yvals, label='Physical model')
        plt.legend()
        return plt.show()

def main():
    # Wed 12 Aug 2020  (created)
    # Fri 14 Aug 2020  (updated)
    """
    Function to exercise the module.
    """
    obliquity = sin(radians(23.43661))
    greenwich = sin(radians(51.47793))
    circle(lati=greenwich, tilt=obliquity).compare()

if __name__ == '__main__':
    main()

# end latitude.py

Near Greenwich:

On another imaginary planet:

[I haven't finished floundering about yet, but I'll try not to make
this answer much longer!  If anyone manages to find a neat proof of
\eqref{3766767:eq:4}, it could be added here; otherwise, this answer
has completed the job of updating my answer to use the same notation
as JonathanZ's answer, and to treat the case of general latitudes;
so it is probably best frozen (except for the correction of any
errors).
I may also ask in Astronomy SE for a reference to
\eqref{3766767:eq:4}, which probably exists in an old source, even
if not in a modern text.  After that, if it remains a puzzle, I'll
post a separate question about it in Maths.SE.
In another CW answer, I want to add in a correction for the Earth's
movement in its solar orbit during its daily rotation.  That should
make it straightforward to compare these calculations with those in
JonathanZ's answer.  Then it would be interesting to add terms to
correct for the apparent size of the Sun's disc and (empirically)
diffraction in the Earth's atmosphere.  Although I, for one, have no
interest in trying to model the Earth's non-spherical shape, or its
non-circular solar orbit, it would be interesting to get a numerical
idea of the accuracy that can be obtained without considering those
or other factors.]