How to do piecewise linear approximation from x and y points?
46 views (last 30 days)
Md. Nazrul Islam Siddique on 23 Nov 2022
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.
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.
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
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')
P2 = fit(x',y',mdl2)
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')
P3 = fit(x',y',mdl3)
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 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 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.