Smooth Approximation of Staircase Function

1.6k Views Asked by At

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.

2

There are 2 best solutions below

1
On BEST ANSWER

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.

6
On

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$.