2
$\begingroup$

What is a function $f(x)$ that is smooth and approximates a staircase function (a function composed of a set of equally spaced jumps of equal length)?

With this answer, I know of $f^n(x)$, where n is an integer and $f(x)=x-\sin(x)$, but it requires quite a large amount of terms to get to a desired sharpness. So, I'm looking for approximations that is not recursive and requires only a finite amount of terms.

$\endgroup$
15
  • $\begingroup$ What kind of approximation are you looking for? $\endgroup$
    – user239203
    Dec 3, 2019 at 16:39
  • $\begingroup$ @cwindolf I've seen it, but I'm looking for other functions. $\endgroup$
    – DUO Labs
    Dec 3, 2019 at 16:40
  • $\begingroup$ @Gae.S. Anything that is smooth and includes finite terms. $\endgroup$
    – DUO Labs
    Dec 3, 2019 at 16:40
  • 3
    $\begingroup$ You will have to state clearly what you want in your question. No one can guess that. $\endgroup$ Dec 3, 2019 at 16:50
  • 1
    $\begingroup$ The qualities "computationally easy" and "smooth" do not really go together. Computation of values can only occur at the finite number of points the computer can represent. "smooth" describes limiting behavior, requiring a continuum of points to define. Though any computed set of function values, no matter how wild, there are an uncountable number of not just smooth, but analytic functions. Can you please be more clear about what your needs are, instead of just saying "no! no! no!" to every guess people are trying to make? $\endgroup$ Dec 4, 2019 at 0:55

2 Answers 2

6
$\begingroup$

A pretty classic approach would be to observe that $x-\lfloor x\rfloor$ is a periodic function, hence subject to all the theory that applies to periodic functions. In particular, we can approximate this difference as a sum of sine waves - and it's rather easy to.

One can start from a well-known formula which gives a sawtooth wave defined by $f(t)=t$ for $t\in (-\pi,\pi)$ and extended periodically with period $2\pi$: $$f(t)=\sum_{n=1}^{\infty}(-1)^{n+1}\cdot\frac{2}n\cdot\sin\left(nt\right)$$ where you can truncate this series at any point to get an approximation.

You can then note that $x-\lfloor x\rfloor$ is a scaled version of this function given as $\frac{1}2 + \frac{1}{2\pi}f(2\pi \cdot (x+1/2) )$ and therefore $$\lfloor x\rfloor = x-\frac{1}2-\frac{1}{2\pi}\sum_{n=1}^{\infty}(-1)^{n+1}\cdot \frac{2}n\cdot \sin(n\cdot 2\pi\cdot (x+1/2))$$ which simplifies as $$\lfloor x\rfloor = x-\frac{1}2+\sum_{n=1}^{\infty}\frac{1}{n\pi}\cdot \sin(2n\pi \cdot t)$$ Here's a plot that shows, overlayed, these sums truncated to between $1$ and $10$ terms: enter image description here

This has some bad properties: It oscillates wildly (with a large derivative) and, near each jump, has an overshoot that is not fixed by adding more terms (Gibbs phenomenon). You can also get somewhat nicer functions by averaging initial segments together (as in a Cesaro summation); for instance, the average of the first $10$ partial sums looks like this:

enter image description here

However, to get a really nice result you can sum the terms with an exponential weighting - that is, look at the sum $$\sum_{n=1}^{\infty}\gamma^n\cdot\frac{1}{n\pi}\cdot \sin(2n\pi \cdot t)$$ where $\gamma$ is a constant a little bit less than $1$. This avoids a lot of the problems you might otherwise have. You can write this sum in terms of elementary functions. In terms of complex numbers, the sum comes out to $$\frac{1}{\pi}\cdot i\cdot \log\left(\frac{1-\gamma e^{-2 i \pi t}}{1-\gamma e^{2 i \pi t}}\right)$$ which simplifies to the purely real expression $$\frac{\tan^{-1}\left(\frac{-\gamma\sin(2\pi t)}{1-\gamma\cos(2\pi t)}\right)}{\pi}.$$ where you can, doing a bit of geometry, realize that this is truly the desired function when $\gamma=1$. In full, this gives the following approximation: $$\lfloor x\rfloor \approx x - \frac{1}2 - \frac{\tan^{-1}\left(\frac{-\gamma\sin(2\pi t)}{1-\gamma\cos(2\pi t)}\right)}{\pi}$$ where values of $\gamma$ closer to $1$ give closer approximations. Here's an image of the function when $\gamma = 0.99$: enter image description here

This function has all sorts of nice properties - you don't need more terms to get a better approximation, it is increasing for all $0<\gamma<1$, and it converges uniformly to $\lfloor x\rfloor$ on any closed interval not containing an integer and you don't need additional terms to get closer - you just modify $\gamma$. Note that this probably could be found with some geometrical cleverness - the oscillating portion is essentially generated by drawing a circle of radius $\gamma$ around the point $(1,0)$ and then measuring the angle a point moving around that circle makes with the origin $(0,0)$ - but it doesn't hurt to find it using standard tricks of Fourier analysis either.

$\endgroup$
1
  • $\begingroup$ This is perfect-- though I will wait for other answers before accepting. $\endgroup$
    – DUO Labs
    Dec 4, 2019 at 2:42
1
$\begingroup$

Edit - (Because my original function rose around $0$, but the floor function trick in the other thread expected the rise between $0$ and $1$, I changed the function to match that expectation (which makes for a much nicer function than adjusting the trick.)

If by smooth, you just mean a continuous derivative, then I suggest you consider the function $$f(x) = \begin{cases}0, & x \le 0\\3x^2-2x^3, & 0 < x < 1\\1,&1\le x\end{cases}$$

This function is $0$ up to $x = 0$, then rises smoothly (i.e., with continuous first derivative, so there are no sharp corners) to 1 at $x = 1$, then remains at $1$ from then on. It's higher derivatives are not continuous, but visually, it looks smooth.

You can control how fast it rises by dividing $x$ by a positive constant $c$:

$$g(x) = f\left(\dfrac xc\right)$$

The closer $c$ is the $0$, the more quickly $g$ will rise from $0$ to $1$, better approximating a single step.

Finally, you get a full step function using the technique of this answer to the other thread:

$$h(x) = g(x - \lfloor x \rfloor) + \lfloor x \rfloor$$

You can see the graph of $y = h(x)$ at Desmos, and play with the effects of $c$.

$\endgroup$
6
  • $\begingroup$ Well, that trick requires the floor function, which is kind of against the point. $\endgroup$
    – DUO Labs
    Dec 4, 2019 at 2:41
  • $\begingroup$ What point is that? You did not disallow the floor function, and it is quite easy to calculate. I had to make a change in my function because the floor trick expected the rise to be in a different location, but you can see by the linked graph there are no jumps. While the corners do turn tightly, they are still smooth, though desmos doesn't show it that well. This is likely the most easily calculated of all possible step functions with continuous first derivatives. $\endgroup$ Dec 4, 2019 at 4:05
  • $\begingroup$ The floor function is a staircase function, and I'm trying to approximate a staircase function. So, using the floor function is counter-intuitive $\endgroup$
    – DUO Labs
    Dec 4, 2019 at 16:03
  • $\begingroup$ That seems like a quixotic complaint. If the floor function were hard to compute and you were looking for an easier method of computing it, then it would make sense. But the floor function is trivial to compute, and the approximations are much harder. So the only reason for approximating it is to obtain continuity and smoothness. Up to $C^1$, this method fully satisfies that requirement. Besides being in the eye of the beholder, "counter-intuitive" does not mean wrong. $\endgroup$ Dec 4, 2019 at 17:33
  • $\begingroup$ I guess so. +1 from me. $\endgroup$
    – DUO Labs
    Dec 4, 2019 at 20:52

Not the answer you're looking for? Browse other questions tagged .