how to interpolate a fluctuated vector in matlab?

妖精的绣舞 提交于 2019-12-24 00:57:13

问题


I have two arrays

x = [0    9.8312   77.1256  117.9810   99.9979];
y = [0    2.7545    4.0433    5.3763    5.0504];
figure; plot(x, y)

I want to make more samples of x and y then I interpolated both arrays. I tried this code

xi =min(x):.1:max(x);
yi = interp1(x,y,xi);
figure; plot(xi, yi)

but the trajectory is not same as previous plot. Its because the xi is not fluctuating same as x. How should I interpolate both arrays with same trajectory as original one?


回答1:


This is an issue because when interpolating, MATLAB is going to ignore the order that you feed in the points and instead just sort them based upon their x location.

Rather than interpolating in x/y coordinates, you can instead use a parameter which represents the cumulative arc length of the line segments and use that to interpolate both the x and y coordinates. This will provide you with an interpolant that respects the order and guarantees monotonicity even for multiple values at the same x coordinate.

% Compute the distance between all points.
distances = sqrt(diff(x).^2 + diff(y).^2);

% Compute the cumulative arclength
t = cumsum([0 distances]);

% Determine the arclengths to interpolate at
tt = linspace(t(1), t(end), 1000);

% Now interpolate x and y at these locations
xi = interp1(t, x, tt);
yi = interp1(t, y, tt);


来源:https://stackoverflow.com/questions/41363651/how-to-interpolate-a-fluctuated-vector-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!