How to do piecewise linear approximation from x and y points?

47 views (last 30 days)
I have x and y points as follows:
x = [20 60 240 480 1440 2880];
y = [0.0278 0.1626 1.8126 4.006 18.2491 44.4084]
I am trying to do the piecewise linear approximation to get the equation of the line. How can I proceed? I have used polyfit function and got the slope and interception for the line. However, I am not sure if it is correct or not? For example, if value of x is < 60, I am getting a negative value of y from the defined fuction from polyfit.

Accepted Answer

John D'Errico
John D'Errico on 23 Nov 2022
Edited: John D'Errico on 23 Nov 2022
x = [20 60 240 480 1440 2880];
y = [0.0278 0.1626 1.8126 4.006 18.2491 44.4084];
You have only 6 data points. Hoping for any reasonable piecewise linear approximation to this is wildly wishing on a star.
The polynomial (straight line) fit will of course produuce a negative y-intercept on that daya.
ans = 1×2
0.0156 -1.8417
So at x == zero, it would predict -1.8417.
But that just means a straight line model may have some lack of fit. Since the data does have some curvature, a linear fit will not make sense. Even a quadratic fit fails to predict a positive value at x==0.
format long g
ans = 1×3
1.98278909699252e-06 0.0099272840717487 -0.549621036882257
You don't really have enough data to have much predictive value here. But I will point out that the positive curvature of your data will make ANY linear fit have a a negative y intercept at x == 0.
What you MIGHT do is fit a quadratic polynomial, without a constant term. So a model of the form
y = a1*x^2 + a2*x
Clearly, when x == 0, this model will always pass exactly through zero. And it will have positive curvature to fit your limited data.
% I can use backslash to estimate the model, but perhaps the curve fitting
% toolbox may be a better idea, as it is easier to use.
% P2 = [x'.^2, x']\y'; % this would fit a quadratic model, with no constant term
mdl2 = fittype('a2*x.^2 + a1*x','indep','x')
mdl2 =
General model: mdl2(a1,a2,x) = a2*x.^2 + a1*x
P2 = fit(x',y',mdl2)
Warning: Start point not provided, choosing random start point.
P2 =
General model: P2(x) = a2*x.^2 + a1*x Coefficients (with 95% confidence bounds): a1 = 0.008991 (0.006946, 0.01104) a2 = 2.248e-06 (1.468e-06, 3.029e-06)
xpred = linspace(min(x),max(x));
ypred2 = P2(xpred);
I don't know how much noise is in your data. But another idea is to try a cubic model, that again has no constant term.
mdl3 = fittype('a3*x.^3 + a2*x.^2 + a1*x','indep','x')
mdl3 =
General model: mdl3(a1,a2,a3,x) = a3*x.^3 + a2*x.^2 + a1*x
P3 = fit(x',y',mdl3)
Warning: Start point not provided, choosing random start point.
P3 =
General model: P3(x) = a3*x.^3 + a2*x.^2 + a1*x Coefficients (with 95% confidence bounds): a1 = 0.005566 (0.004167, 0.006964) a2 = 6.449e-06 (4.854e-06, 8.043e-06) a3 = -1.051e-09 (-1.447e-09, -6.555e-10)
ypred3 = P3(xpred);
I don't think your data even justifies that curve fit, but it does seem to fit your data.

More Answers (2)

Walter Roberson
Walter Roberson on 23 Nov 2022
Edited: Walter Roberson on 23 Nov 2022
A piecewise linear approximation could have a piecewise break at or between any pair of points, and is therefore not unique. You would have to constrain the approximation to have a hope.
For example you could require a break exactly half-way between two points if the previous segment was length 2 or more and the projected estimate at the additional point would be more than 10% different than the point.
(Notice that this rule has potential problem, such as what to do if that would leave an isolated point; notice too that because it depends upon value of the point, the effect of the difference depends on how close to the axes you are. A difference of 10 might be immaterial if the y coordinate were a million -- but on the other hand if you translated the coordinates down by a million and had the same relative values near the axes, is a difference of 10 suddenly relevant?)

Image Analyst
Image Analyst on 23 Nov 2022
I really see no point in fitting two lines to data where there is only 6 points. With so few points there is essentially no confidence that the lines will be much like what they would be if you had hundreds of points. So why bother? What are you going to do with this information?
Nonetheless, you can try my piecewise fitting demo, attached.




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!