Interpolation when data are not strictly monotonic
Show older comments
Hi,
I have the following data points:
X = [102 106 108 109 111 112 113 115 115 116 116 116];
Y = [10 10 10 11 11 13 14 15 17 18 20 113];
I used interp1 for 1D data interpolation, however it fails because data points are not strictly monotonic (problematic point: 116,113). I was wondering how to tackle this problem by either 1- removing the problematic data point(s) or 2- make the interpolation work with some modification or smoothing on data.
Thanks in advance,
Accepted Answer
More Answers (1)
John D'Errico
on 15 Jun 2016
OK, the problem is you have a function with different y values for the same x. How can interp1 predict when there are more than one y value? Interp1 applies ONLY to single values functions, so for any x, only one y.
If this is actually a general path in two dimensions, then use tools like cscvn, or my interparc (as found on the file exchange.)
But if your goal is to produce a function that predicts y as a function of x, then you need to make the data monotonic in x. Why is it a bad thing to increment the x values by a tiny amount? If you will be using a spline interpolant, it will introduce HUGE amounts of ringing into the function. Having points so close to each other, with widely varying y values does nastiness to a spline fit.
Of course, if you are doing only linear interpolation, then the increment trick will work.
Instead, you are generally best off by choosing some way to reduce the replicates into a single point.
You might choose to take the average value, or perhaps the median of replicates. Or perhaps just the first or last among the reps. Or, the max or min. Lots of ways you could resolve the issue. But for any x, only ONE y.
A simple tool to solve these problems is my consolidator tool , also found on the file exchange. It was designed to solve exactly this class of problems, for spline fits.
X = [102 106 108 109 111 112 113 115 115 116 116 116];
Y = [10 10 10 11 11 13 14 15 17 18 20 113];
Note that consolidator can work in multiple dimensions, so it requires column vectors.
[xc,yc] = consolidator(X',Y',@mean);
[xc,yc]
ans =
102 10
106 10
108 10
109 11
111 11
112 13
113 14
115 16
116 50.333
[xc,yc] = consolidator(X',Y',@median);
[xc,yc]
ans =
102 10
106 10
108 10
109 11
111 11
112 13
113 14
115 16
116 20
As you can see, once consolidator is done, the curves can now be happily interpolated. It is pretty efficient, and it even takes a tolerance.
6 Comments
Ive J
on 15 Jun 2016
John D'Errico
on 15 Jun 2016
Edited: John D'Errico
on 15 Jun 2016
If your goal is interpolation along a general path in the (x,y) plane, then do this:
xy = interparc(100,X,Y,'pchip');
plot(X,Y,'ro',xy(:,1),xy(:,2),'b-')

See that the above interpolation did indeed generate 100 equally spaced points along that path.
You can still not use tools like that to interpolate y(x) here, because which of those y values should it return where x is replicated?
Ive J
on 15 Jun 2016
John D'Errico
on 15 Jun 2016
Note that if you use the default method for interparc, it will be fastest, and considerably so. The default method does linear interpolation between consecutive pairs of points along the path. This may be adequate for your purposes. Those who want smoother curves must use one of the alternative methods in interparc.
Ive J
on 15 Jun 2016
John D'Errico
on 15 Jun 2016
Anything I post on the FEX is there for all to use freely. If you will publish in a journal, they will often wish to see a citation. For that purpose, we some years ago choose a couple of style that seemed to make sense. The page I've linked has examples of both, as applied to one of my other codes - gridfit.
Categories
Find more on Interpolation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
