spline approximation with constraints
2 views (last 30 days)
Show older comments
Yoav Shimshi
on 4 Apr 2018
Commented: Yoav Shimshi
on 13 Apr 2018
Hey everyone. I have a dense set of points x,y that seem to fall on a single curve:

I would like to find a smooth function to mach the data. Specifically, I would like it to fulfill the following criteria:
-It is the closest to the data (say in a least squares sense)
-Its first and second derivatives vanishes only once (hopefully at the maximum and inflection points of the original data)
I have tried doing that using smoothing spline approximation (not sure if this is the correct approach here with such a dense dataset, I any of you have a better idea i would like to hear). So far my best results were obtained by manually picking ~50 points from the data, using:
[xi,yi] = ginput;
and fitting a spline to them, using
splinetools(xi,yi)
And playing with the parameter in the smoothing spline approximation method until the spline matches.
Obviously that's very crude and doesn't satisfy my criteria. How are things like that typically done? Is there a way to put constraints on the number of critical and inflection points?
0 Comments
Accepted Answer
John D'Errico
on 4 Apr 2018
Edited: John D'Errico
on 4 Apr 2018
There are several problems in what you are trying to do.
The obvious one is that splines abhor singularities, and you have one on the left end of that curve. Made up of polynomial segments, you can expect them to fail there.
As well, a standard least squares spline, or a smoothing spline both assume the error is in y only. But you clearly have a cloud that spreads out along the curve in both directions. So any least squares or smoothing spline must perform poorly. So I would not even bother trying to use my own SLM toolbox on this data.
Orthogonal regressions using splines are difficult.
I see that you have supplied the data, so I'll take a look at it. Perhaps I can make a stab at something...
7 Comments
John D'Errico
on 11 Apr 2018
Edited: John D'Errico
on 11 Apr 2018
NO. Unique is NOT a solution. Unique just sorts the points, sorting them on increasing x. In fact, interp1 may not even be a good solution.
The problem is, those points on the vertical line may not be in order, even if you sort them on x. There is no assurance that they will be. kmeans will pick points from the cloud. But depending on the noise in that cloud, you should imagine that some of those points will vary back and forth in x. The noise might be large enough that along that line, IF you sort them in x, they will be out of order in y.
So why is that a problem? Interp1 will sort the points on x anyway. So you need not bother using unique, as that is a waste of time, since interp1 is doing an internal sort. But the points won't be in order.
Let me show you an example. I've generated a set of points such as you might find from the m-means procedure. See that the x axis is scaled VERY tightly.

So x varies by only a tiny amount. But it jitters and jogs back and forth somewhat randomly. This is what I would expect to see from your data, when k-means is applied. Here I've allowed MATLAB to autoscale the axes, so we can see what happens if you treat these points improperly.

Now, see what happens when you sort them on x. Again, that is what unique will do. It is what interp1 does anyway.
xint = linspace(0,0.12,1000);
plot(x,y,'b:o',xint,interp1(x,y,xint,'linear'),'r-')
grid on

Look carefully at the differnce between the two lines. The dotted clue line is the one that connects them in the proper order, thus, along there, for increasing y. The red line is what happens when a sort on x intervenes.
Again, all of this is due to the use of k-means on that cloud. It is also why I tried to tell you to use a traveling salesman solver to sort the points properly. The TSP solver will try to sort them in their natural order.
Finally, this is why simple use of interp1 can easily fail, because no matter what, if those points along the vertical stretch do not come out with increasing x, then interp1 will create garbage.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


