Approximating a piece-wise function

104 Views Asked by At

I would like to approximate a piece-wise function. The aim is to get a function as $f(x) \approx ...$ without piece-wise definition (only one expression, not depending of $x \leq 1$ or $x \geq 1$), and if possible composed of well-known functions (polynomials, exponentials, ...).

Here it is : $f:x\mapsto \left\{ \begin{array}{c l}1-x & x \geq 1 \\ -\ln(x)& x \leq 1 \end{array} \right.\qquad x \in [\frac{1}{2};2]$

I thought of Taylor Series but I don't know how to do it with a function defined by parts.

I also tried to interpolate a function with the points $(0.5,\ln(2))$, $(1,0)$ and $(2,-1)$, resulting in $f(x) \approx 0.2575296\cdot x^2-1.772589\cdot x+1.515059$ . But I think it is not good enough (cf function curves) : in red is my interpolation, in blue the log part and in black the linear part.

Can you help me ? Thanks.

2

There are 2 best solutions below

0
On BEST ANSWER

My approach would be:

$$f(x)=-\ln(x)(1-H(x-1))+(1-x)H(x-1)=-\ln(x)+(1-x+\ln(x))H(x-1)$$ where $H(x)$ is the Heaviside step function.

There are many analytical approximations of $H(x)$, my favorite one is $$H(x)=\lim_{k\to \infty}\frac{\tanh(kx)+1}2$$.

Thus, $$f(x)\approx -\ln(x)+(1-x+\ln(x)) \frac{\tanh(k(x-1))+1}2$$ for large $k$.

3
On

You could approximate the $-\ln(x)$ with a taylor series around $1$ ,

$-\ln(x) \approx -(x-1)+\frac{(x-1)^2}{2}-2\frac{(x-3)^3}6+6\frac{(x-1)^4}{24}-24\frac{(x-1)^5}{120}+120\frac{(x-1)^6}{720}\cdots $

Your function becomes

$f(x)=\begin{cases}1-x, &&x\ge 1\\-(x-1)+\frac{(x-1)^2}{2}-2\frac{(x-3)^3}6+6\frac{(x-1)^4}{24}-24\frac{(x-1)^5}{120}+120\frac{(x-1)^6}{720}\cdots&&x\le 1\end{cases}$

The plotted graph looks like this;

where red is the approximation [appoximated to order 17] and blue is the function enter image description here

EDIT: You could adjust the number of terms in the polynomial of $-\ln(x)$ as needed , if you're restricting the domain less terms will be needed.

For $-\ln(x) , \; x\in[\frac12,1]$ order 5 gives you a very close approximation as shown below

enter image description here