A smooth function instead of a piecewise function

As I said in one of my comments you can modify $f(x)$ in a neighbourhood of $x=a$, say $(a-\varepsilon,a+\varepsilon)$, for $\varepsilon>0$ as small as you wish. In that small interval you want to change your function by some other smooth function $g(x)$ satisfying the following conditions: $g(a-\varepsilon)=a-\varepsilon$, $g(a+\varepsilon)=a$, $g'(a-\varepsilon)=1$, $g'(a+\varepsilon)=0$. These conditions ensure that $g$ glues well (smoothly) with $f$.

If my computations are not wrong a function satisfying the conditions above is $$g(x)=-\frac{1}{4\varepsilon}\left(x^2-2(a+\varepsilon)x+(a-\varepsilon)^2\right).$$

Then a smooth function approximating $f$ would be $$ f_{\varepsilon}(x)=\left\{ \begin{array}{ccl} x &\mbox{if}& x\leq a-\varepsilon\\-\frac{1}{4\varepsilon}\left(x^2-2(a+\varepsilon)x+(a-\varepsilon)^2\right)&\mbox{if}& a-\varepsilon\leq x\leq a+\varepsilon\\ a&\mbox{if} & x\geq a+\varepsilon \end{array} \right. $$ Notice the subscript $f_\varepsilon$ indicating the dependence on the parameter $\varepsilon$. The smaller you take the parameter, the better the approximation (since you're modifying $f$ in a smaller interval).


Consider the weighting function $h(x)=exp(5(x-a))/(1+exp(5(x-a))$, which takes values close to 0 for $x<<a$ and close to 1 for $x>>a$, [plot it to check this statement] Now approximate your $f(x)$ weighting $x$ and $a$, like in $$ g(x)=x(1-h(x))+ah(x) $$ Intuitively, $h$ allows you to switch from $x$ to $a$ (or any other constant or function, for that matters) around $x=a$, with a speed that depends on the arbitrary coefficient 5, which can be seen in the definition of $h$. Increase it to improve the fit.

See an example for $a=3$ in the picture below.Approximation of $f$