# Continuous function for day/night with night being $c$ times longer than day

I'm looking for a function to transform domain $$[0,1)$$ into range $$[0,1)$$ such that the size of the domain corresponding to the range interval $$[.5,1)$$ is $$c$$ times the size of the domain corresponding to the range interval $$[0,.5)$$. The range interval $$[0,.5)$$ will correspond to day time and [.5,1) will correspond to night time, so this function's output will make night time $$c$$ times as long as day time.

A piece-wise function for example would satisfy the problem, but I would like the function to be smooth on the interval $$[0,1)$$ (and $$y=x$$ would satisfy this problem perfectly if $$c=1$$).

Some extra more explicit constraints:

$$f'(0)=f'(1)$$ (because I would like the transition from night to day to be smooth as well--if possible, all derivatives at $$x=0$$ and $$y=1$$ should be the same)

$$f(0)=0$$

$$f(1)=1$$

function is monotonic on $$[0,1)$$

$$cf^{-1}(.5)=1$$ (I believe this ensures night is $$c$$ times longer than day)

• "Non-piecewise" doesn't actually have any meaning; it's just a description of how you happen to write the formula. Maybe you are looking specifically for a polynomial? Aug 31 '19 at 0:26
• So something like $$f_a(x)=x+a \sin^2(\pi x),$$ where we need the condition $|a|\le 1/\pi$ to make sure that $f$ is increasing everywhere? This is too simple-minded to be an answer, because $f_a^{-1}(0.5)$ will be limited to the interval $[b,1-b]$ where $b\approx 0.2957$ is reached with $a=1/\pi$ and $1-b$ with $a=-1/\pi$. Just trying to get a more precise fix on the problem in my mind :-) Jul 21 '20 at 9:30
• @JyrkiLahtonen More generally, it seems to be desirable, although this isn't stated in the question, to have $f'(0) = f'(1) = 1.$ I was trying to add a Hermite interpolation term to the constant function, but it's messy, and more restrictive than your idea. It's easy to give a cubic spline solution with $f'(0) = f'(1) = f'\left(\frac1{c + 1}\right) = 0,$ for unrestricted $c$ but that doesn't seem "realistic". I imagine (again it's not stated) that solutions are meant to be chained in some way to model a succession of days and nights. I'll try adding a spline to the constant function. Jul 21 '20 at 11:22
• It would also help if one or both of the questioners would give an idea of the range of values of $c$ for which a solution is needed. I'm guessing that $c$ might need to be at least as large as $2,$ and at least as small as $\frac12.$ Jul 21 '20 at 12:31
• I did not mean that you should stop the Earth mid-game. What I meant is is that if you were really calculating the day length for the Earth then you would need to consider that the Earth orbits the Sun. I meant to ignore that: pretend that the Earth is magically held at one point in its orbit but still rotating. This would simplify the calculations and give a constant day length (which I thought that you wanted). Jul 27 '20 at 8:18

In the notation of my "answer" to my still-unanswered question Almost simple Hermite interpolation, we can compute a quintic polynomial $$l_a(x)$$ such that $$l_a(0) = l_a(1) = 0,$$ $$l_a(a) = 1,$$ and $$l_a'(0) = l_a'(1) = l_a'(a) = 0,$$ where $$a = \frac1{c + 1},$$ so that we can consider, as a possible solution to the problem, at least for some values of $$c$$: $$f(x) = x + \left(\tfrac12 - a\right)l_a(x) \quad (0 \leqslant x \leqslant 1).$$ After much simplification, we arrive at the formula $$$$\label{3339606:eq:1}\tag{1} \boxed{f(x) = x + \frac{(c^2 - 1)(c + 1)^2x^2(1 - x)^2[(3c - 2) - 2(c^2 - 1)x]}{2c^3}.}$$$$ The appendix gives a range of values of $$c$$ for which this polynomial function satisfies the conditions of the question. For the moment, I'll just give two examples (excluding the trivial case $$c = 1$$):

When $$c = 2,$$ $$f(x) = x + \frac{27x^2(1 - x)^2(2 - 3x)}8.$$ From Wolfram Alpha:

When $$c = \tfrac12,$$ $$f(x) = x + \frac{27x^2(1 - x)^2(1 - 3x)}8.$$ From Wolfram Alpha:

The reason why these graphs are $$180^\circ$$ rotated images of one another is as follows:

By the uniqueness of the Hermite interpolating polynomial, $$l_{1 - a}(x) = l_a(1 - x) \quad (0 < a < 1, \ 0 \leqslant x \leqslant 1).$$ Write $$c = (1 - a)/a,$$ i.e. $$a = 1/(c + 1).$$ Then $$a$$ is related to $$c$$ as $$1 - a$$ is to $$1/c,$$ and $$f_{1/c}(x) = x + \left(\tfrac12 - (1 - a)\right)l_{1 - a}(x) = x - \left(\tfrac12 - a\right)l_a(1 - x) = 1 - f_c(1 - x),$$ where, for all $$c > 0,$$ $$f_c(x)$$ denotes the function in \eqref{3339606:eq:1} with parameter $$c.$$ $$\ \square$$

Dropping the constraint $$f'\left(\frac1{c + 1}\right) = 1$$ simplifies the formula somewhat: $$f(x) = x + \frac{(c^2 - 1)(c + 1)^2x^2(1 - x)^2}{2c^2},$$ but this doesn't greatly increase the range of usable values of $$c.$$ Also, the graphs take on a squashed appearance when $$x$$ approaches $$1,$$ as this example for $$c = 2$$ illustrates:

The graph for $$c = 5/2,$$ although still monotonic - unlike \eqref{3339606:eq:1}, in this case - is even worse:

So I won't consider this simplification any further.

If $$f$$ need not be analytic, and if continuous differentiability is enough, and if the values of $$f'(0)$$ and $$f'(1)$$ do not matter so long as they are equal, then it is easy to solve the problem using cubic splines. For example: $$f(x) = \begin{cases} \tfrac12g\left[(c + 1)x\right] & \text{if } 0 \leqslant x \leqslant \frac1{c + 1}, \\ \tfrac12\left\{1 + g\left[\frac{(c + 1)x - 1}{c}\right]\right\} & \text{if } \frac1{c + 1} \leqslant x \leqslant 1, \end{cases}$$ where $$\begin{gather*} g(t) = 3t^2 - 2t^3, \ g'(t) = 6t(1 - t) \ \, (0 \leqslant t \leqslant 1), \\ g(0) = 0, \ g(1) = 1, \ g'(0) = g'(1) = 0, \\ g'(t) > 0 \ \, (0 < t < 1). \end{gather*}$$ This has $$f'(0) = f'(1) = f'\left(\frac1{c + 1}\right) = 0.$$ It is valid for all $$c > 0.$$

But we can do a lot better than that. I delayed looking at this possibility, wrongly imagining that it would work only for a restricted range of values of $$c,$$ like the Hermite interpolation solution. In fact, it works for all values of $$c$$ (the value of $$c$$ must be strictly positive, of course), without exception.

We continue to use the same "cardinal" cubic spline function $$g,$$ but now we define $$f(x) = x + \left(\tfrac12 - a\right)s_a(x) \quad (0 \leqslant x \leqslant 1),$$ where $$s_a(x) = \begin{cases} g\left(\frac{x}{a}\right) & \text{ if } 0 \leqslant x \leqslant a,\\ g\left(\frac{1 - x}{1 - a}\right) & \text{ if } a \leqslant x \leqslant 1. \end{cases}$$ Differentiating, $$s_a'(x) = \begin{cases} \frac1{a}g'\left(\frac{x}{a}\right) & \text{ if } 0 \leqslant x \leqslant a,\\ -\frac1{1 - a}g'\left(\frac{1 - x}{1 - a}\right) & \text{ if } a \leqslant x \leqslant 1. \end{cases}$$ Because $$g'(t) \geqslant 0$$ for all $$t \in [0, 1],$$ and $$\max_{0 \leqslant t \leqslant 1}g'(t) = \tfrac32,$$ we have $$\begin{gather*} \min_{0 \leqslant x \leqslant 1}s_a'(x) = -\frac3{2(1 - a)}, \\ \max_{0 \leqslant x \leqslant 1}s_a'(x) = \frac3{2a}. \end{gather*}$$ Therefore, if $$a \leqslant \tfrac12,$$ i.e. $$c \geqslant 1,$$ $$\min_{0 \leqslant x \leqslant 1}f'(x) = 1 + \left(\frac12 - a\right)\left(-\frac3{2(1 - a)}\right) = 1 - \frac{3(1 - 2a)}{4(1 - a)} = \frac{1 + 2a}{4(1 - a)} > 0.$$ On the other hand, if $$a \geqslant \tfrac12,$$ i.e. $$c \leqslant 1,$$ $$\min_{0 \leqslant x \leqslant 1}f'(x) = 1 + \left(\frac12 - a\right)\left(\frac3{2a}\right) = 1 - \frac{3(2a - 1)}{4a} = \frac{3 - 2a}{4a} > 0.$$ In all cases, therefore, $$f$$ is strictly increasing on $$[0, 1].$$

In terms of the constant $$c = (1 - a)/a,$$ the definition of the function $$f$$ is: $$$$\label{3339606:eq:2}\tag{2} \boxed{f(x) = \begin{cases} x + \frac{c - 1}{2(c + 1)}g[(c + 1)x] & \text{ if } 0 \leqslant x \leqslant \frac1{c + 1}, \\ x + \frac{c - 1}{2(c + 1)}g\left[\frac{(c + 1)(1 - x)}{c}\right] & \text{ if } \frac1{c + 1} \leqslant x \leqslant 1. \end{cases} }$$$$ For example, when $$c = 4,$$ \eqref{3339606:eq:2} becomes: $$f(x) = \begin{cases} x + \frac{15}2x^2(3 - 10x) & \text{ if } 0 \leqslant x \leqslant \frac15, \\ x + \frac{15}{64}(1 - x)^2(1 + 5x) & \text{ if } \frac15 \leqslant x \leqslant 1. \end{cases}$$ From Wolfram Alpha:

Here is a closer look at the knot of that spline function:

## Appendix

For any $$a$$ such that $$0 < a < 1,$$ we define the quintic polynomial function $$l_a(x) = \frac{x^2(1 - x)^2[a(3 - 5a) - 2(1 - 2a)x]}{a^3(1 - a)^3}.$$ Its derivative is given by $$l_a'(x) = \frac{2x(1 - x)(x - a)[5(1 - 2a)x - (3 - 5a)]}{a^3(1 - a)^3}.$$ It satisfies (and indeed it is uniquely determined by) six constraints $$\begin{gather*} l_a(0) = l_a(1) = 0, \ l_a(a) = 1, \\ l_a'(0) = l_a'(1) = l_a'(a) = 0. \end{gather*}$$ Writing $$c = (1 - a)/a,$$ or equivalently $$a = 1/(c + 1),$$ where $$c$$ is any strictly positive number, we define $$f_c(x) = x + \left(\tfrac12 - a\right)l_a(x).$$ Then $$\begin{gather*} f_c(0) = 0, \ f_c(1) = 1, \ f_c\left(\frac1{c + 1}\right) = \frac12, \\ f_c'(0) = f_c'(1) = f_c'\left(\frac1{c + 1}\right) = 1. \end{gather*}$$

I shall determine a set of values of $$c$$ such that $$f_c'(x) > 0$$ for all $$x \in [0, 1].$$ (I shall not try to determine all such values of $$c.$$) It was shown above that for all $$c > 0,$$ if either of $$f_c',$$ $$f_{1/c}'$$ is strictly positive on $$[0, 1],$$ then so is the other. Because $$f_1(x) = x,$$ it suffices to consider only the case $$c > 1,$$ i.e., $$a < \tfrac12.$$

Differentiating: $$$$\label{3339606:eq:3}\tag{3} 1 - f_c'(x) = -\left(\tfrac12 - a\right)l_a'(x) = \frac{20\left(\tfrac12 - a\right)^2x(1 - x)(x - a)(b - x)} {a^3(1 - a)^3},$$$$ where $$b = \frac{3 - 5a}{5(1 - 2a)} = \tfrac12\cdot\frac{\tfrac35 - a}{\tfrac12 - a} = \tfrac12\left(1 + \frac{\tfrac1{10}}{\tfrac12 - a}\right),$$ i.e., $$\left(\frac12 - a\right)\left(b - \frac12\right) = \frac1{20},$$ so we can rewrite \eqref{3339606:eq:3} as $$$$\label{3339606:eq:4}\tag{4} 1 - f_c'(x) = \frac{\left(\tfrac12 - a\right)x(1 - x)(x - a)(b - x)} {a^3(1 - a)^3\left(b - \tfrac12\right)}.$$$$ We are interested in determining $$c > 1$$ such that $$1 - f_c'(x) < 1$$ for all $$x \in [0, 1].$$ By \eqref{3339606:eq:4}, we only need to consider $$x$$ such that $$a < x < \min\{1, b\}.$$

Case 1: $$\boxed{c \leqslant \tfrac32 \iff a \geqslant \tfrac25 \iff \tfrac12 - a \leqslant \tfrac1{10} \iff b \geqslant 1.}$$ Looking at the factors in \eqref{3339606:eq:4}, we have: $$\begin{gather*} \frac{b - x}{b - \tfrac12} = 1 + \frac{\tfrac12 - x}{b - \tfrac12} \leqslant 1 + \frac{\tfrac12 - x}{1 - \tfrac12} = 2(1 - x) \leqslant \frac65, \\ \frac12 - a \leqslant \frac1{10}, \\ x \leqslant 1, \\ (1 - x)(x - a) \leqslant \left(\frac{1 - a}2\right)^2 \leqslant \left(\frac3{10}\right)^2, \\ a(1 - a) = \frac14 - \left(\frac12 - a\right)^2 \geqslant \frac6{25}, \end{gather*}$$ therefore $$1 - f_c'(x) \leqslant \frac{\tfrac65\cdot\tfrac1{10}\cdot\left(\tfrac3{10}\right)^2} {\left(\tfrac6{25}\right)^3} = \frac{5^2}{2^5} = \frac{25}{32} < 1.$$ This completes the proof that $$f_c'(x) > 0$$ for $$x \in [0, 1]$$ and $$c \in \left[\tfrac23, \tfrac32\right].$$ $$\ \square$$

Case 2: $$\boxed{c \geqslant \tfrac32 \iff a \leqslant \tfrac25 \iff \tfrac12 - a \geqslant \tfrac1{10} \iff b \leqslant 1.}$$

From \eqref{3339606:eq:3}, using the inequalities $$x(1 - x) \leqslant \tfrac14$$ and $$(x - a)(b - x) \leqslant ((b - a)/2)^2,$$ $$1 - f_c'(x) \leqslant \frac{5\left(\tfrac12 - a\right)^2(b - a)^2}{4a^3(1 - a)^3}.$$ Reparameterising in terms of $$p,$$ where $$a = \frac12 - p, \quad 1 - a = \frac12 + p, \quad b - \frac12 = \frac1{20p} \qquad \left(\frac1{10} \leqslant p < \frac12\right),$$ we have $$1 - f_c'(x) \leqslant \frac{5p^2(p + 1/20p)^2}{4\left(\frac14 - p^2\right)^3} = \frac{5(p^2 + 1/20)^2}{4\left(\frac14 - p^2\right)^3} = \frac{5\left(\frac3{10} - q\right)^2}{4q^3} = \frac{(3 - 10q)^2}{80q^3},$$ where $$q = \frac14 - p^2 = a(1 - a) \in \left(0, \, \frac6{25}\right)\!.$$ According to Wolfram Alpha, the cubic equation $$80q^3 = (3 - 10q)^2$$ has a single real root, $$q_0 \bumpeq 0.212428328248244.$$ We therefore have $$f_c'(x) > 0$$ for all $$x \in [0, 1]$$ if any of the following list of equivalent conditions is satisfied: \begin{align*} q > q_0 & \iff \frac{c}{(c + 1)^2} > q_0 \\ & \iff c^2 - 2\left(\frac1{2q_0} - 1\right)c + 1 < 0 \\ & \iff c < c_0 = \left(\frac1{2q_0} - 1\right) + \sqrt{\left(\frac1{2q_0} - 1\right)^2 - 1} \bumpeq 2.266203431. \end{align*} Finally, then: $$f_c'(x) > 0$$ for all $$x \in [0, 1]$$ if $$c \in [0.4413, 2.2662]$$. $$\ \square$$

The graph of $$f_c$$ for $$c = c_0$$ looks like this:

Here is a close-up view of the flattest part of that graph:

This estimated value, $$c_0,$$ is evidently quite close to the least upper bound of the set (presumably a closed interval) of all values of $$c$$ for which $$f_c$$ is monotonic.

• I haven't looked much at your Hermite solution (except to realize as you pointed out that under certain cases e.g. c=.2, x=.5 the monotonicity does not hold), but I believe that your cubic spline satisfies all the OP's criteria. Trying it on desmos, I've realized that I probably should have also asked for a function that minimizes the second derivative (perhaps it's max absolute value) as especially at the endpoint it looks awkward and when c=1 it looks strange not to be a straight line... Jul 22 '20 at 1:18
• ...Though I am still happy with your solution as I don't need this anymore, so I'll probably wait until the bounty expires to mark a solution in unison with what @SaganRitual awards Jul 22 '20 at 1:19
• When $c = 1,$ \eqref{3339606:eq:1} simplifies to $f(x) = x,$ as it should. The value $c = .2$ is definitely beyond its range. Empirically, $4/9 \leqslant c \leqslant 9/4$ seems OK, and when I've had some more sleep (early morning insomnia right now!), I'll look at $c = 7/3.$ (If that works, so will $c = 3/7.$) Do you think a value as small as $c = 1/5$ might be needed? That would require one of the other approaches I've listed; and even those, apart from the spline with zero derivatives at $0, 1, \frac1{c + 1}$ might not work. Jul 22 '20 at 4:42
• Oh, sorry, I misunderstood (sleepy!): you were referring to the cubic spline solution, not \eqref{3339606:eq:1}. No, I don't like that solution much, either! (It was the first thing I tried, but I didn't think it was worth posting.) My fourth method should be better, but it won't work for all $c.$ Jul 22 '20 at 4:48
• I don't feel qualified to advise anybody on the subject, I'm afraid. The only book I've read much of is M. J. D. Powell, Approximation theory and methods (1981), and I found it pretty difficult! There are still some knotty points I'd like to go back and try to understand better. I've only dipped into the Davis book, and the reason I had to ask a question in Maths.SE about it is that I couldn't get his formulae for general Hermite interpolation to work. I think it has an extremely good reputation, though. A seach of Maths.SE might turn up some questions with helpful answers; I haven't looked. Jul 22 '20 at 18:13

If $$0 then there are simple trigonometric formulae for $$f$$. For instance, we can put $$f(x)=\sin^k\left(\frac{\pi x}{2}\right)$$, where $$k>1$$ is picked to assure $$f\left(\tfrac 1{c+1}\right)=\tfrac 12$$, that is $$k=\log_{\sin\left(\frac{\pi}{2(c+1)}\right)}\frac 12$$. Even simpler, we can put $$f(x)=\sin \frac{\pi x^k}{2}$$, where $$k>1$$ is picked to assure $$f\left(\tfrac 1{c+1}\right)=\tfrac 12$$, that is $$k=\log_{c+1} 3$$ or $$c=\sqrt[k]3-1$$.

I also looked for a polynomial $$f$$ of small degree, but not so successfully.

If $$f$$ is a polynomial of third degree such that $$f(0)=0$$ and $$f(1)=1$$ then $$f(x)-x$$ has roots $$0$$ and $$1$$, so $$f(x)=x+ax(x-1)(x+b)$$ for some real $$a$$ and $$b$$. Since case $$a=0$$ is trivial, we assume that $$a\ne 0$$. So $$f’(x)=a(3x^2+2xb-2x-b)+1$$. If $$f’(0)=f’(1)$$ then $$-ab=a(1+b)$$, so either $$a=0$$ or $$b=-1/2$$. In both cases $$f(1/2)=1$$.

Assume that $$f$$ is a polynomial of fourth degree. Then $$f’(x)$$ is a cubic polynomial such that $$f’(x)-f’(0)$$ has two roots $$0$$ and $$1$$. Thus $$f’(x)=f’(0)+ax(x-1)(x+b)$$ for some real $$a$$ and $$b$$. An equality $$1=f(1)-f(0)=\int_0^1 f’(x)dx$$ implies $$f’(0)=1+\tfrac a{12}(2b+1)$$. The monotonicity of $$f$$ is equivalent to $$f’(x)\ge 0$$ at $$[0,1]$$. The latter holds iff $$f’(0)\ge 0$$ and $$f’(x_m)\ge 0$$ for each local minimum $$x_m\in (0,1)$$ of the function $$f$$. Since $$f’’(x_m)=0$$, $$3x_m^2+2(b-1)x_m-b=0$$, that is $$x_m=\tfrac{1-b+r}3$$, where $$r=\pm \sqrt{b^2+b+1}$$. Since $$f’’’(x_m)=6ax_m+2a(b-1)=2ar$$ and $$x_m$$ is a point of a local minimum, we have that $$r$$ and $$a$$ have the same sign. We also need $$x_m\in [0,1]$$, that is $$b-1\le r\le b+2$$. It is easy to check that this is equivalent to $$b\ge -1$$, if $$a>0$$, and to $$b\le 0$$, if $$a<0$$. Unfortunately, I don’t see an easy way to find a range of $$c$$ for which there exists $$f$$ satisfying the above sconditions such that $$f\left(\tfrac 1{c+1}\right)=\tfrac 12$$. We can illustrate $$f$$ for $$a=-12$$ and $$b=0$$. Then $$f(x)=-3x^4+4x^3$$ and $$f^{-1}\left(\tfrac 12\right)\approx 0.614$$.

• By some symmetry argument couldn't you rotate the $\sin$ functions $180^\circ$ when $c>1$ and evaluate with $c'=\frac{1}{c}$ to allow any positive $c$? I also really like your explanations, especially the way you convert everything into a finding roots problem Jul 23 '20 at 17:25
• @Acreol A nice observation. Yes, we can rotate the graph around a point $\left(\tfrac 12, \tfrac 12\right)$. Analytically we can take a function $g(x)=1-f(1-x)$. Then we obtain $g(0)=0$, $g(1)=1$. Moreover, $g’(x)=f’(1-x)\ge 0$ so, $g(x)$ is monotonic and $g’(0)=g’(1)$. Finally $g\left(\tfrac {1}{{\tfrac 1c}+1}\right)=g\left(\tfrac {c}{c+1}\right)=1- f\left(1-\tfrac {c}{c+1}\right)= 1- f\left(\tfrac {1}{c+1}\right)=\tfrac 12$. Jul 23 '20 at 17:59