我有许多行表示"z“值,并希望在它们之间插入一个特定的值,在一个特定的x轴位置,以得到y-值。我正在尝试interp2(),但它会抛出一个“单调递增”的错误。
下面的数据集是一个子集。我把它分解成xyz-1和xyz-2,只是为了在这个问题中简单地绘图(即,做一个可重复的例子)。如何修复我的interp2()或输入?
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 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);

发布于 2020-11-06 09:23:13
函数griddata是您完成这些任务的朋友,它在幕后使用scatteredInterpolant,但在我看来,它对用户更友好。
保留您提供的相同示例代码,将最后一行替换为:
>> need_y = griddata(x_all,z_all,y_all,want_x, want_z)
need_y =
0.329506024096386如果需要查询多个点,该函数可以获取want_x和want_z的向量输入,并返回need_y的向量输出。
还可以指定插值方法(linear、cubic等.)。
为了确保它能按需要工作:
>> F = scatteredInterpolant(x_all.', z_all.', y_all.', 'linear'); %NOT y_all, z_all
need_y = F(want_x, want_z)
need_y =
0.329506024096386 % same result, yay!有关使用griddata的更多细节,您可以查看与您的问题非常相似的my answer (只是措辞有点不同):Interpolation between two curves (matlab)
发布于 2020-11-06 00:10:08
您可以使用以下方法插入该值:
x1 = [0.02, 0.04, 0.09, 0.184, 0.309, 0.667];
y1 = [0.586, 0.447, 0.34, 0.279, 0.256, 0.256];
z1 = [50, 50, 50, 50, 50, 50];
x2 = [0.022, 0.044, 0.076, 0.125, 0.184, 0.293, 0.509, 0.667];
y2 = [0.747, 0.6, 0.49, 0.41, 0.363, 0.326, 0.303, 0.3];
z2 = [35, 35, 35, 35, 35, 35, 35, 35];
want_x = 0.2;
want_z = 40;
y1_ = interp1(x1, y1, want_x);
y2_ = interp1(x2, y2, want_x);
want_y = interp1([50 35], [y1_ y2_], want_z);发布于 2020-11-06 01:05:46
下面回答了这个问题:https://www.mathworks.com/matlabcentral/answers/637955-interp2-monotonically-increasing-error
如果链接在将来中断,请引用该答案:
interp2()只适用于网格上的二维解释,而不是向量的插值.你需要像F= scatteredInterpolatn(x_all,z_all,y_all,‘线性’);%NOT y_all,z_all need_y = F(want_x,want_z);
https://stackoverflow.com/questions/64706657
复制相似问题