如果 x 坐标从增加变为减少,则使用 splines() 会形成问题

Using splines() forms problem if x coordinates go from increasing to decreasing

我有两列数据,xy。现在我想按照它们在列中出现的顺序连接这些数据点。假设我有 x=[1 2 3 4 3 2]y=[3 4 2 1 3 3]。现在,如果我使用样条曲线创建平滑曲线,它会 'sorts' 列以递增顺序排列。我希望它只获取数据点,因此首先 x(1),y(1) 并将它们连接到 x(2), y(2) 等等。

这可能吗?

spline 生成一个从实数到实数的函数。这意味着更一般的曲线不能表示为 y = f(x) 但我们需要将其参数化为 (x(t), y(t)):

x=[1 2 3 4 3 2];
y=[3 4 2 1 3 3];
plot(x,y,'o-');
% cannot be represented as function y=f(x) 
% because x=2 and 3 have two different y values
% -> parametrize x and y:
t = 1:numel(x);
tt = linspace(min(t), max(t), 1000);;
tx = spline(t,x,tt);
ty = spline(t,y,tt);
hold on
plot(tx,ty,'-');