Clear Filters
Clear Filters

interp2() 'monotonically increasing' error

3 views (last 30 days)
I have many lines that represent a "z" value and want to interpolate at a specific value between them, at a specific x-axis location, to get the y-value. I'm trying interp2() but it throws a 'monotonically increasing' error.
The dataset below is a subset. I broke it out into xyz-1 and xyz-2 just for easy plotting in this question (i.e., making a repeatable example). How can I fix my interp2() or the inputs?
x1 = [0.02, 0.048, 0.108, 0.196, 0.279, 0.401];
y1 = [0.583, 0.43, 0.32, 0.279, 0.262, 0.259];
z1 = [50, 50, 50, 50, 50, 50];
x2 = [0.02, 0.048, 0.108, 0.196, 0.279, 0.401];
y2 = [0.747, 0.591, 0.435, 0.357, 0.326, 0.305];
z2 = [35, 35, 35, 35, 35, 35];
x_all = [x1, x2];
y_all = [y1, y2];
z_all = [z1, z2];
plot(x1, y1, 'blue', 'DisplayName', 'z1')
hold on
plot(x2, y2, 'magenta', 'DisplayName', 'z2')
xlabel('x')
ylabel('y')
legend
want_x = 0.2;
want_z = 40;
need_y = interp2(x_all, y_all, z_all, want_x, want_z, 'linear')
Error:
Error using griddedInterpolant
The grid vectors must be strictly monotonically increasing.
Error in interp2>makegriddedinterp (line 228)
F = griddedInterpolant(varargin{:});
Error in interp2 (line 128)
F = makegriddedinterp({X, Y}, V, method,extrap);

Accepted Answer

Walter Roberson
Walter Roberson on 6 Nov 2020
Edited: Walter Roberson on 6 Nov 2020
interp2() is only for two-dimensional interpretation over a grid, not for interpolation of vectors. You need something like
F = scatteredInterpolant(x_all, z_all, y_all, 'linear'); %NOT y_all, z_all
need_y = F(want_x, want_z);
  1 Comment
newbie9
newbie9 on 6 Nov 2020
thank you, worked like a charm (sans a small typo, scatteredInterpolatn). and thank you for including the commented remined on the order

Sign in to comment.

More Answers (0)

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!