[Math] Smooth transition between linear functions

elementary-functionsexponential functionfunctions

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

enter image description here

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?

Best Answer

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 pictureenter image description here

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}$$

Related Question