Turning a Piecewise Function into a Single Continuous Expression

Solution 1:

Consider the Heaviside step function defined this way (other variants for $H(0)$ exist) :

$$ H(x)=\begin{cases} x<0&\ 0\\[8pt] x=0&\frac 12\\[8pt] x>0&\ 1\\[8pt] \end{cases} $$

(you may too write $H$ as a $\rm{sign}$ function : $\ H(x)=\dfrac {1+\rm{sign}(x)}2$) then :

$$\boxed{\displaystyle f(a,b)= g(a,b)\cdot H(b-a)+ h(a,b)\cdot H(a-b)}$$

(with value $\dfrac{g(a,a)+h(a,a)}2$ for $a=b$ of course)

To get a 'smoothed' result you may replace $H$ by a Sigmoid function : $\ \displaystyle S(x)=\frac 1{1+e^{-x}}\ $ or other variants.

Since $\ H(x)=\lim_{n->\infty} S(n\cdot x)\ $ you may replace $H(x)$ by something like $S(n\cdot x)$ with any $n \gg 1$ to get a smooth transition!

Let's illustrate this with an example : $x\mapsto x^2$ for negative $x$ and $x\mapsto \cos(10 x)$ for positive $x$ illustrated with $n=1000$ and $n=100$ :

f(t)=t^2/(1+exp(1000*t))+cos(10*t)/(1+exp(-1000*t)

n=1000

f(t)=t^2/(1+exp(100*t))+cos(10*t)/(1+exp(-100*t)

n=100

Hoping it helped,

Solution 2:

If it is acceptable for you to write $$g(a,b)=u\left(\frac{a+b}{2}\right)+(b-a)\cdot g_*(a,b)\\ h(a,b)=u\left(\frac{a+b}{2}\right)+(b-a)\cdot h_*(a,b)$$ then you can write $$f(a,b)=u\left(\frac{a+b}{2}\right)+\frac{(b-a)-|b-a|}{2}\cdot g_*(a,b)+\frac{(b-a)+|b-a|}{2}\cdot h_*(a,b)$$


On the other hand, if you have to consider $g,h$ as opaque functions, it is impossible to find continuous $\varphi,\psi$ such that $$f(a,b)=\varphi(a,b)g(a,b)+\psi(a,b)h(a,b)$$ (this is because necessarily $\varphi(a,b)=1$ when $a<b$ and $0$ when $a>b$, creating a discontinuity).

In particular, if $\varphi,\psi$ are constructed from constants, the variables $a,b$, operators like $+,-,\times,/$ and continuous functions like $\exp,\log,\sin,\cos,\tan,\sqrt{\cdot},|\cdot|$ (paying attention to their domain of definition), then they are necessarily continuous.