Approximating a clamp function using only addition, multiplication, division and subtraction

1.2k Views Asked by At

I'm trying to construct a function which satisfies the following:

$$ \begin{align} f(x) = 0 & \qquad x \leq 0\\ f(x) = x & \qquad 0 \lt x \lt 1\\ f(x) = 1 & \qquad x \geq 1\\ \end{align} $$

Sadly I only have the very basic operators at my disposal: + - / *. I also have grouping operators ( ).

I've managed to come close using the Butterworth function:

$$f(x) = \frac{x}{1+\frac{2x-1}{1}+\frac{2x-1}{1}+\frac{2x-1}{1}+…}$$

This (approximately) satisfies my first two conditions, but not the third one. I'm sure there's a way to modify this function to satisfy all three, but I don't have the skills.

EDIT:

I'd like to be able to handle the range of -600 < x < 120. For accuracy, I don't have a definite target. Perhaps to start with if f(x) could be <0.01 when x < -0.01 and f(x) could be >0.99 when x > 1.01? I know that higher accuracy will result in a longer expression, so if I understand how to build the expression I can experiment to find an appropriate balance between brevity & accuracy.

If it makes your answer easier to read, feel free to include exponentiation by a constant value. I don't have access to any exponential operations, but I can easily convert them into repeated multiplication myself as long as the exponent is constant.

3

There are 3 best solutions below

3
On BEST ANSWER

Your function can be approximated through two shifted ramp functions, as $$ \eqalign{ & f(x) = x\,H(x) - \left( {x - 1} \right)\,H(x - 1) \approx \cr & \approx {x \over 2}\left( {1 + {x \over {\sqrt {x^{\,2} + \varepsilon ^{\,2} } }}} \right) - {{\left( {x - 1} \right)} \over 2}\left( {1 + {{\left( {x - 1} \right)} \over {\sqrt {{\left( {x - 1} \right)}^{\,2} + \varepsilon ^{\,2} } }}} \right) \cr} $$ where
$H(x)$ denotes the Heaviside step function;
$\varepsilon <<1$ is a small value.

Concerning the square root, you might calculate itrecursively through the famous Babylonian Method

This is an example of what you get with $\varepsilon = 0.1$.

Intp_Rampa_1

It is clear that operating around the symmetric function $ x-1/2 \to y-1/2$ you can reduce the computations almost in half.

1
On

I would comment if I only were privileged.

Have in mind that the function you want to construct is only piecewise analytic. Maybe you can obtain this function as the limit of some sequence of functions, which is presumably the exercise here, given the question tags.

I suspect that there won't be any closed series representing the given function.

1
On

I apologize for the late response. I recently came across this post and as it so happens, I have been experimenting with such functions recently and figured I would add my two-cents.

For my current research, I needed a smooth(ish) approximation to the Heaviside step function defined on a compact interval. I really wanted to use a similar approach to the one discussed in chapter 13 of the book "An Introduction to Manifolds" by Loring W. Tu. This approach uses the function $$f(x)=\left\{\begin{array}{cc}e^{-\frac{1}{x}},&x>0,\\0,&x\le0\end{array}\right.$$ to construct a $C^\infty$ bump function. When I tried implementing this numerically I ran into some problems due to the very fast nature of how this function approaches $0$ (overflow errors, division by zero, etc. ).

This led me to an approach that is extremely similar to that used in the book but instead of using the function $e^{-\frac{1}{x}}$, I needed to find a function with the same basic ''shape'' and behavior as the exponential but which was simpler to compute and used only addition/subtraction, multiplication/division (and was perhaps not $C^\infty$ but just $C^1$ or $C^2$). I thought for a while and finally decided to use the function

$$f(x)=\left\{\begin{array}{cc}1-\frac{1}{1+x^p},&x>0,\\0,&x\le0\end{array}\right.$$ where $p$ is a positive integer greater than or equal to 2.

To construct the Heaviside step function approximation from this you can simply follow a slightly altered version of the procedure in the book by Tu

Define $g(x)=\frac{f(x)}{f(x)+f(1-x)}$ and let $\epsilon$ be the desired width of the smoothed interval around the corner point (in the plots below I use $\epsilon=.1$)

The approximation to the Heaviside-step function is then given by $$H_\epsilon(x)=g\left(\frac{x-\frac{\epsilon}{2}}{\epsilon}\right)$$

Here is a comparison what Tu's Heaviside step function (red) and my Heaviside step function (purple) approximation look like for $p=2$ enter image description here

They look very similar and get sharper as you increase the value of $p$. As an added benefit, the function $f$ gains more degrees of smoothness as $p$ is increased.

When I apply this approximation to your desired function $x(H(x)-H(x-1))+H(x-1)$ for $\epsilon=.1$ and $p=4$ the result is visually indistinguishable from the desired result. Actual function (blue), My approximation (red) enter image description here

I hope this helps you out! Please let me know if you have any questions regarding my approach