Approximation of non linear function with multiple linear functions

How can a non-linear function be approximated by an appropriate amount of linear functions?

In the picture below, it would be quite easy to draw 10-15 linear functions to describe all data points quite accurately, so once the points are separated into sections, I could easily get very accurate results with linear regression.

However, both, the number of required functions and the length of each part varies. So is there an non-manual algorithm / way to separate the function into sections?

Thank you!

• Do you mean regression splines? – Frans Rodenburg Apr 17 at 9:18

I'm not sure if your data requires local lines or local constants. But, in general, there is Local Regression, in which you can also fit local degree-k polynomials. When $$k=1$$, this boils down to local linear regression. It's a non-parametric method like Kernel Regression (you don't have to deal with the end-points of your local curves etc.), which you can also use and specify different kernels functions.