# How to approximate $\max (0, 1 - \exp (x))$ with a differentiable, positive function

Does anybody have suggestions for approximating $$f(x) = \max (0, 1 - \exp (x))$$ with a function that is at least twice differentiable, strictly greater than or equal to $$0$$ across its domain, and not prone to introducing numerical issues into nonlinear optimization programs (NLPs)?

I would like to include $$0 \leq y \leq f(x)$$ as a constraint in an optimization problem without having to resort to using integer/boolean variables, hence I need some sort of continuous approximation. Here $$y$$ is some other variable. Importantly, $$f$$ must not go negative, otherwise the problem will become infeasible.

I tried multiplying $$f$$ with various sigmoidal functions, but they invariably do a poor job near the origin, or worse, go negative. E.g., see the figure where $$f(x) \approx (1-\exp(x)) \times (1+\exp(100x))$$. For my application it is important that $$f$$ very rapidly goes to $$0$$ when approaching the origin from the negative (going to the positive) axis, but never itself goes negative. Does anybody have any ideas?

• convolve the curve with a smooth enough kernel Oct 19, 2021 at 17:45

You can rewrite $$\max(0,x)$$ as $$\frac{x+|x|}{2}$$. Then approximate $$|x|$$ as $$\sqrt{\epsilon+x^2}$$, where you can make $$\epsilon$$ as small as needed, but positive.
Putting all together $$f(x)=\frac{1-e^x+\sqrt{0.01+(1-e^x)^2}}{2}>\max(0,1-e^x)$$
Multiplying $$f$$ with sigmoidal functions was a good idea. You just have to take a "good" one and modify it. You could use a modification of the smoothstep $$S_2(x)=\max(0,\min(1,-6x^5-15x^4-10x^3)),$$ which is a twice differentiable function. Then the sequence of functions could be $$y_n(x)=(1-\exp(x))\cdot\max(0,\min(1,-6(nx)^5-15(nx)^4-10(nx)^3)),$$ which is also a twice differentiable function with $$0\leq y(x)\leq f(x)$$ for all $$x\in \mathbb R$$ and satisfies $$||y_n-f||_\infty\to 0.$$ Regarding the numerical issues I can't tell you anything.