一种方法是最小二乘曲线拟合。
您需要拟合参数化曲线[x(t), y(t)],而不是简单曲线y(x)。根据您的链接,您似乎正在尝试拟合一条简单的曲线y(x)。
MATLAB 文件交换here 提供了一个方便的最小二乘样条拟合工具SPLINEFIT。
使用此工具,以下是一个简单示例,说明如何使用最小二乘样条拟合将平滑曲线拟合到一组噪声数据。在这种情况下,我生成了 10 个随机扰动的圆形数据集,然后以最小二乘的方式将 5 阶样条拟合到数据中。
function spline_test
%% generate a set of perturbed data sets for a circle
xx = [];
yy = [];
tt = [];
for iter = 1 : 10
%% random discretisation of a circle
nn = ceil(50 * rand(1))
%% uniform discretisation in theta
TT = linspace(0.0, 2.0 * pi, nn)';
%% uniform discretisation
rtemp = 1.0 + 0.1 * rand(1);
xtemp = rtemp * cos(TT);
ytemp = rtemp * sin(TT);
%% parameterise [xtemp, ytemp] on the interval [0,2*pi]
ttemp = TT;
%% push onto global arrays
xx = [xx; xtemp];
yy = [yy; ytemp];
tt = [tt; ttemp];
end
%% sample the fitted curve on the interval [0,2*pi]
ts = linspace(0.0, 2.0 * pi, 100);
%% do the least-squares spline fit for [xx(tt), yy(tt)]
sx = splinefit(tt, xx, 5, 'p');
sy = splinefit(tt, yy, 5, 'p');
%% evaluate the fitted curve at ts
xs = ppval(sx, ts);
ys = ppval(sy, ts);
%% plot data set and curve fit
figure; axis equal; grid on; hold on;
plot(xx, yy, 'b.');
plot(xs, ys, 'r-');
end %% spline_test()
您的数据显然比这更复杂,但这可能会让您入门。
希望这会有所帮助。