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.