# Smooth transition between linear functions

I have two functions that are approximately linear. To keep thing simple, I will deal with linear functions in the following.

Lets take $f(x)=x+15$ and $g(x)=3x+2$. I would like to stitch these functions together at the point $x=6$.

For that I currently use a $\tanh$-function: $s(x) = 0.5+0.5\tanh((x-6)/w)$, where $w$ a width. So I ultimately get the function $h(x)=s(x)f(x) + (1-s(x))g(x)$ that looks like this This works as intended, however, at the transition point $x_0=6$ $h(x)$ has a small "hump", so $h(x)$ is pushed upwards relative to $f(x)$ at the transition point.

This is undesirable for me. Is there a different transition function I can use that doesn't have this "hump"? Maybe some exponential function?

• In computer graphics, Bezier curves are typically used for this. Oct 28, 2017 at 11:11
• You could also perhaps also use a circle tangent to both straight lines ore another conic section. Oct 28, 2017 at 11:17
• @Gribouillis Your last suggestion is quite interesting! Do you know how I can get started on that? Oct 28, 2017 at 11:25
• What do you mean with "stitched together at $x=6$"? Usually, to stitch something together at a specific point, the functions must intersect at this point. Do you have some concrete intentions with this statement, or you just want a smooth transition anywhere (like here)? And do you mean smooth in the mathematical concrete meaning of $C^\infty$, i.e. infinitely often differentiable. Oct 30, 2017 at 8:58

If you don't want to use a Bezier curve, you could perhaps simply join two points with a cubic polynomial $f(x) = a x^3 + bx^2+ c x + d$. Given two points $(x_0, y_0)$ and $(x_1, y_1)$ and two slopes $p_0$ and $p_1\in \mathbb{R}$, find a cubic polynomials that passes through the two points with the given slopes. It gives 4 equations with 4 unknowns

$$\left\{ \begin{array}[rcl] \\a x_0^3 + b x_0^2 + c x_0 + d& =& y_0 \\a x_1^3 + b x_1^2 + c x_1 + d& =& y_1 \\3 a x_0^2 + 2 b x_0 + c &=&p_0 \\3 a x_1^2 + 2 b x_1 + c &=&p_1 \end{array} \right.$$ Solve for $a,b,c,d$ and you have the curve.

Edit: If you want to avoid the "hump" there will probably be a condition that avoids an inflection point between $(x_0, y_0)$ and $(x_1, y_1)$. Probably something like $(y_1-y_0)-p_0(x_1-x_0)$ and $(y_1-y_0)-p_1(x_1-x_0)$ must have different signs.

Edit Sympy gives me the following values:

let $z = {\left({x}_{0}-{x}_{1}\right)}^{3}$

then

$$\renewcommand{\arraystretch}{2} \begin{array}{rl}z a =&\left({p}_{0}+{p}_{1}\right) \left({x}_{0}-{x}_{1}\right)-2 \left({y}_{0}-{y}_{1}\right)\\ z b =&3 \left({x}_{0}+{x}_{1}\right) \left({y}_{0}-{y}_{1}\right)-\left({x}_{0}-{x}_{1}\right) \left(2 {p}_{0} {x}_{1}+{p}_{1} {x}_{1}+{p}_{0} {x}_{0}+2 {p}_{1} {x}_{0}\right)\\ z c =&{-6} {x}_{0} {x}_{1} \left({y}_{0}-{y}_{1}\right)+\left({x}_{0}-{x}_{1}\right) \left(2 {p}_{0} {x}_{0} {x}_{1}+{p}_{0} {x}_{1}^{2}+{p}_{1} {x}_{0}^{2}+2 {p}_{1} {x}_{0} {x}_{1}\right)\\ z d =&{-{p}_{0}} {x}_{0}^{2} {x}_{1}^{2}+{p}_{0} {x}_{0} {x}_{1}^{3}-{p}_{1} {x}_{0}^{3} {x}_{1}+{p}_{1} {x}_{0}^{2} {x}_{1}^{2}+{x}_{0}^{3} {y}_{1}-3 {x}_{0}^{2} {x}_{1} {y}_{1}+3 {x}_{0} {x}_{1}^{2} {y}_{0}-{x}_{1}^{3} {y}_{0} \end{array}$$

In your case, choosing $x_0=6$ and $x_1=7$ with $p_0 = 3$ and $p_1=1$ and $y_0 = 3 x_0 + 2 = 20$ and $y_1 = x_1+15 = 22$ produces the function $$f(x) = -x^2 + 15 x - 34, \quad x\in [6,7]$$

(the $x^3$ term is zero). Here is the picture Edit Another way to write the polynomial, similar to the Lagrange interpolation formulas is this

$$\renewcommand{\arraystretch}{2} \begin{array}{rl}P(x) =&\displaystyle {y}_{0} \frac{{\left(x-{x}_{1}\right)}^{2}}{{\left({x}_{1}-{x}_{0}\right)}^{3}} \left(2 x-3 {x}_{0}+{x}_{1}\right)-{y}_{1} \frac{{\left(x-{x}_{0}\right)}^{2}}{{\left({x}_{1}-{x}_{0}\right)}^{3}} \left(2 x+{x}_{0}-3 {x}_{1}\right)\\ &\displaystyle \quad +{p}_{0} \frac{{\left(x-{x}_{1}\right)}^{2}}{{\left({x}_{1}-{x}_{0}\right)}^{2}} \left(x-{x}_{0}\right)+{p}_{1} \frac{{\left(x-{x}_{0}\right)}^{2}}{{\left({x}_{1}-{x}_{0}\right)}^{2}} \left(x-{x}_{1}\right) \end{array}$$

When all derivatives of $h$ vanish at $x=0$, and we let $g(x)=h(x)/(h(x)+h(1-x))$
then the graph of $g$ will look somewhat like this: It connects two lines smoothly, but both lines have zero slope. An antiderivative of such a function instead connects two lines with different slope (zero and nonzero). By an affine transformation, the antiderivative can be adjusted to connect any two non-parallel lines.

The following is one way to define a function $T$ such that $T'$ has the same form (except for horizontal reflection about $x=1/2$) as $g$ above (I doubt that the corresponding $h$ can be solved in closed form).

\begin{align} &w(x)=(\tanh \left((\log(1-t)^2-\log(t)^2)/3\right)+1)/2\\ &p(x)=\tanh \left((\log (2-t)-\log (t)) \cdot(1-2^{-1/2} (\log (2-t)+\log (t)))\right)+ 2t - 1\\ &q(x)=\tanh \left((\log (t+1)-\log (1-t))\cdot (1-2^{-1/2} (\log (1-t)+\log (t+1)))\right)\\ &T(x)=p(x)(1-w(x))+q(x)w(x)\end{align}

If your lines are the graphs of $\ l_1(x)=ax+b\$ and $\ l_2(x)=cx+d\$, then the transition starts at $\min(x_1,x_2)$ and ends at $\max(x_1,x_2)$, where $$x_1=\dfrac{b-d+k}{c-a}\quad\text{and}\quad x_2=\dfrac{b-d-k}{c-a}$$

The y-coordinate of the transition is given by

$$y(x)=k\cdot T\left(\frac{a x+b-c x-d+k}{2 k}\right)+c x+d-k$$

where $k$ is a scaling parameter: This is very late, but hopefully it will help someone out.

Rather than using a Sigmoid or Tanh function to transition from curve $$y_1$$ to $$y_2$$, try applying it to the gradient. Namely, aim for a smooth transition from the gradient of $$y_1$$ to the gradient of $$y_2$$.

Example: Transition from $$y_1(x)=x$$ to $$y_2(x)=5$$. Make a sigmoid connecting the gradients of $$y_1$$ and $$y_2$$ centered at the curves intersection. Integrate this to obtain the connecting curve, in this case given by: $$y_3(x) = x+5- \log (e^{5} + e^{x})$$ 