interp1: The grid vectors are not strictly monotonic increasing
9 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
In matlab I have a set of points
x = [53, 125, 179, 193, 158, 121, 115, 120, 150, 193, 246, 301, 352, 380, 385, 376, 334, 295, 298, 312, 354, 423];
y = [120, 198, 296, 392, 496, 596, 700, 743, 800, 838, 853, 839, 801, 752, 698, 599, 502, 401, 346, 301, 202, 120];
and I want to draw a spline through them all creating a sort of bulb shape. However when I attempt to simply use the command y1 = interp1(x,y,x1,'spline') I recieve the error
Error using griddedInterpolant
The grid vectors are not strictly monotonic increasing.
Error in interp1 (line 186)
F = griddedInterpolant(X,V,method);
Could someone please explain both what this error means and how I would go about fixing it? I've read in a few other posts that I should try to find a set a parametric equations that I could use to replace the vectors, but I don't really understand what that would mean practically either. This is what the points look like:

Edit: People have suggested sorting the x and y data and then drawing a spline through the sorted data. Unfortunately this produces the exact same error, and even if it were do work I believe it would simply create a series of zig-zags(like a wonky sin curve) instead of the bulb shape I need.
0 Commenti
Risposta accettata
Matt J
il 25 Ott 2016
Modificato: Matt J
il 25 Ott 2016
I've read in a few other posts that I should try to find a set a parametric equations that I could use to replace the vectors
I somewhat agree with that advice,
n=numel(x);
xfun=@(p) spline(1:n,x,p); %parametric functions
yfun=@(p) spline(1:n,y,p);
t=linspace(1,n,1000);
plot(x,y,'o',xfun(t),yfun(t),'.')
0 Commenti
Più risposte (2)
John D'Errico
il 24 Mag 2017
Modificato: John D'Errico
il 24 Mag 2017
Too late for the answer to be useful, but this still may be instructive.
The problem arises if the points are not in the form of a function, thus y=f(x), a single valued function. The hourglass curve shown has exactly that problem. It CANNOT be fit using a spline directly, and sorting the points on x will produce garbage.
The common trick here is (assuming the points are provided in the order of a polygonal curve) is to fit both x and y parametrically in terms of their piecewise linear arclength. This is what is used in my interparc code, as found on the file exchange. It is also done by the cscvn tool provided by MATLAB (I think it comes with the curve fitting toolbox.) It is also easy enough to do in only a couple of lines of code.
x = [53, 125, 179, 193, 158, 121, 115, 120, 150, 193, 246, 301, 352, 380, 385, 376, 334, 295, 298, 312, 354, 423];
y = [120, 198, 296, 392, 496, 596, 700, 743, 800, 838, 853, 839, 801, 752, 698, 599, 502, 401, 346, 301, 202, 120];
t = cumsum(sqrt([0,diff(x)].^2 + [0,diff(y)].^2));
t = t./t(end);
tint = linspace(0,1,1000);
xint = spline(t,x,tint);
yint = spline(t,y,tint);
plot(x,y,'ro',xint,yint,'b-')

The virtue of this scheme is it does not assume the points are in any way equally spaced. See for example, that Matt used a very similar scheme, but he used 1:n as the parameter. That implicitly assumes an equal spacing, yet, as you can see from the plot, the points were not at all equally spaced.
So the solution is simple. You can download my interparc. Or you can use cscvn. Or you can use the approach I show above. Any and all can and will work.
0 Commenti
Vedere anche
Categorie
Scopri di più su Get Started with Curve Fitting Toolbox in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!