# Linear approximation of data set's points.

37 views (last 30 days)
alessio morabito on 8 Apr 2021 at 19:48
Commented: alessio morabito on 11 Apr 2021 at 10:01
i have a data set (x,y) that i have to approximate with a piecewise linear function, such that lower and upper bounds are satisfied. the image show what's the result I would like to achieve (in red the data set's points; in grey, the linear continuous approximation i would like to describe)image of result to achieve.
Using Matlab, i've divided all points in groups (one for every linear segment) and approximated every linear segment, with needed constraints, in the sense of least squares. But i need to have a continuous function and I am stuck in solving the problem. There's someone that have an idea of what i can do to solve the problem?
##### 2 CommentsShowHide 1 older comment
alessio morabito on 11 Apr 2021 at 10:01
Ok John, but I have already written a working algorithm to approximate tangent circles, when the lines are already defined and intersect correctly. for this I was looking for a tool that could define a piecewise linear approximation of the points that belong to the lines, because I already know how to approximate the points that belong to the circle. Maybe, correct me if i'm wrong, there might be a way to use your tool to do this (approximate with a continuous linear function (with N pieces), the N survey point groups that belongs to a straight). or, maybe, i have to work with lsqlin individually for each straight, defining the condition of intersection with the next straight line in a point that is included between them.
hoping to have better explained the problem, best regards

John D'Errico on 8 Apr 2021 at 22:33
Edited: John D'Errico on 8 Apr 2021 at 22:35
Don't do it that way!!!!!!! You cannot achieve a continuous piecewise linear function that way. Instead, use a tool designed to solve that problem. For example:
x = rand(1,100);
y = sin(pi*x);
slm = slmengine(x,y,'knots',0:0.2:1,'degree','linear','plot','on');
So piecewise linear segments, fit using least squares. Breaks at the points 0:.2:1.
You can use the resulting structure, evaluating the function, or you could even extract the linear segments.
alessio morabito on 9 Apr 2021 at 20:17
thank you so much John, it's very interesting and useful.
I'll try to explain much better my problem. i'm studying the vertical profile of a railway. in a nutshell it's composed by a succession of straights and circles (tangents to the straights between which they are included). my data set is composed by survey points, measured along the railway with an uncertainty; my goal is to approximate this data set with a succession of straights and tangent circles (with the center on the bisector of the two straights in which it's included), such that certain constraints are respected (the most important concerns deviations, so i have to set lower and upper bounds).
I'm developing an algorithm that recognize (or try to) if the survey points belong to a straight or a circle (simply, computing of the change rate of gradient). after having cataloged all the points in the single lines and circles (i have the point group that belongs to the first straight, the point group that belongs to the second straights and the point group that belongs to the first circle, tangent to the first to straights.... and so on...). So, i need to approximate all the point group that belongs to tangents with a continuous piecewise linear function, respecting lower and upper bounds constrains.
I'm not sure how could i use your tool to help me. Maybe there's a way, and i'm sure we can create a nice discussion on it.
Best Regards