Findind function y=f(x) to for piecewise linear functions

I am looking for way(s) of finding a general method to solve the following problem:

Given certain piecewise linear functions y=ki * x, how to find a general function y=f(x) that, error of f(x) to these piecewise lines are minimum?

For example, on [-1,0) is y=x, on [0,1] is y=2x, how to find a function y=f(x) on [-1,1] that has least quare root of errors.

2 Attachment(s)

Using a polynomial to represent piecewise functions

A polynomial can pretty accurately fit piecewise functions.

Excel can do a polynomial up to 6 degrees (x^6 - x^5 . . . . ) The higher the degree polynomial the better the fit.

I used a 6 degree polynomial for the case you described in your posting, and came up with an R^2 value pretty close to 1. Check out the attached files and see if this helps you.

Polynomial representation

I think that Maple and other math software will automatically generate polynomials to represent piecewise functions up to the degree of your choice. Remember the higher the degree of the polynomial the more accurate the curve will be. As you can see in the previous post the curve does not go through 0,0. In excel as you are adding the trendline, you can choose to force the curve through 0,0.

Good luck

May I have more details on your topic

Thank you hpe! Your reply is very interesting, sorry to miss it for so many days! Is there any paper touching on this subject in depth, or would it be so trouble to provide more details...