Say we want to build a function which is piecewise linear $$f(x) = \sum_{\forall k} (H(x-x_k)-H(x-x_{k+1}))l_k(x)\\l_k(x) = c_{k1}x+c_{k2}$$
And also so that it fits best possibly some function $x\to g(x)$: $$l_k,x_k=\min_{l_k,x_k}\left\{\int_a^b |f(x)-g(x)|dx\right\}$$
Please note that the line end point coordinates $x_k$ we can decide for ourselves.
I've made some numerical approaches which seem promising on this, but how can one approach it algebraically/analytically?


Using a CMA-ES setup with ($\lambda = 20,\mu = 3$) over a test function
$$ f(x) = \text{If}\left[x<2.2 (x-2)^2+14,\text{If}\left[x<6.5 \sin \left(\frac{3}{2} (x+4)\right)+2,-\frac{(x-6)^2}{0.3}-4\right]\right] $$
with a chromosome composed of $2n+1$ parameters asigned as follows
$$ \phi_x = \{\Delta x_1,\cdots,\Delta x_n\}\\ \phi_y = \{y_1,\cdots,y_{n+1}\} $$
Here $\Delta x_k = x_k-x_{x-1}$ for $k = 1,\cdots, n$ with $x_0$ as the lower number in the smoothing interval $[x_{min},x_{max}]$ so $x_0 = x_{min}$. Then $x_k = x_0+\sum_{j=1}^k\Delta x_j$ uses the pairs $\{x_k,y_k\}$ to construct a reference step-wise linear basis to proceed the integration process and the fitness is calculated as
$$ \sum_{k=1}^{n}\int_{x_k}^{x_{k+1}}|f(x)-g_k(\phi_x,\phi_y,x)|dx $$
with
$$ g_k(\phi_x,\phi_y,x)=y_k + \frac{(y_{k+1}-y_k)}{(x_{k+1}-x_k)}(x-x_k) $$
Follows a series of outcomes for established $n$ values
$(n = 4)$
$(n=5)$
$(n=6)$
$(n=7)$
$(n=8)$
$(n=9)$
$(n=10)$
Follows also a typical medium fitness progress curve