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

1. ## 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. ## 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.

3. ## 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

4. Thanks for the help! However, this topic is for theorectical study purpose, that a general formula is prefered. I am looking for analytical tool that can solve this kind of problem in general. Not sure if Variation calculas, Integeral equations, or partial differential equations could be of help here, are there any papers or books touch on such topic?

In real world of control, models can be represented in piecewise linear models and controller based on such kind of model are hard to design. However, if a general function to approximate these piecewise models is available, in analytical form, things may get easier.

5. Originally Posted by WangTaoSG
I am looking for analytical tool that can solve this kind of problem in general.
The special piecewise linear function f(x) = max(0,x) may be approximated by the smooth function $
g(x) = (\sqrt{\epsilon^2 + x^2}+x)/2
$
with arbitrary $\epsilon > 0$. General piecewise linear functions can then be approximated as superpositions of this function. An example is shown in the picture, with $\epsilon = 0.1$.

6. ## 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...

7. Originally Posted by WangTaoSG
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...
Suppose you want to approximate a piecewise linear function that has corners at $a_1 < a_2 < \dots < a_n$ and that the function is given by $f(x) = k_0x + b$ for $x, continuing with slope $k_1$ for $a_1 < x < a_2$, slope $k_2$ for $a_2 < x < a_3$ and so on.

Fix the constant $\epsilon > 0$ and set $g(x) = (\sqrt{\epsilon^2 + x^2}+ x)/2$ as before.

Then the function $\tilde f(x) = k_0 x + b + \sum_{i=1}^n (k_i - k_{i-1})g(x-a_i)$ does a reasonable job. If n is large and the $a_i$ are closely spaced, you have to choose $\epsilon$ sufficiently small.

8. ## I start to understand...

and I believe it is the one I am looking for, but how do you come up with this solution, very impressive indeed!

Thanks very much again, HPE!