This code has a serious flaw with it.
For a model with multiple breaks in it, this tool finds the points in the left most section, then applies polyfit to that, and then sequentially works across the data, solving for each segment, one at a time.
While this may seem logical, it fails to recognize that data in one segment should influence the shape of the curve in another neighboring segment. In fact, a point near one end of a segment SHOULD influence the next line segment.
As well, by solving these as sub-problems in a sequential manner, we would get a different result if we ran the sequences in a reverse order. That is silly.
Finally, it looks like the code will fail if a segment just happens to completely lack any data points, since then the slope in that segment will be undefined. Since this code allows fminsearch to choose the breaks, that can indeed happen.
The fact is, all of this can be surmounted. The entire fit can be written in a single linear algebra operation, thus a call to backslash, solving for all segments at once. This resolves the issue of order of solution. It resolves the issue of neighboring points influencing another interval.
Even better, if you add in a tiny amount of regularization to the linear algebra (still a linear problem) then the process will survive a segment with no points in it.
Wrapping the segmented solver inside an optimizer like fminsearch is still necessary if you will ask it to choose the break placement. Be very careful however, as fminsearch tends to be a terribly slow thing with more than about 6 variables to search over.
I will also point out that my own SLM toolbox allows the equivalent broken stick regression, without the flaws I explain above. It does use fmincon for the break placement.
15 Apr 2014
A line consisting of connected straight sections is fitted to a cloud of data points.
can you please explain how you calculated the rr matrix (i.e. the matrix of residuals)? You mentioned in a previous post that it simply is the squared sum of residuals, however I tried following your code but didn't manage to understand.
Thanks in advance
15 Apr 2014
An easy-to-use set of functions for quaternion calculus. Examples and demo
One question about the translation vector (regParams.t):
I have the following 3 points before rotation:
A1 = (400, 400, 200,)
A2 = (600, 600, 200,)
A3 = (2000, 2000, 2000)
and after rotation:
B1 = (400, 400, 200,)
B2 = (600, 600, 200,)
B3 = (2000, 2000, 2001)
so all point are the same, just B3z is 2001 instead of 2000. Then the computed translation vector using your code is: (162, -76,4001)
What doesn't make sense to me? Because just one single point changed it's z-value from 2000 to 2001 what is just a difference of 1...?
Nevertheless, the result seems to be right, because:
b(:,1) = regParams.R * a(:,1) + regParams.t