1

我尝试计算一条线,它可以在 MATLAB 中用二维坐标拟合给定几个点。但结果出乎我的意料。可能有什么我理解错了。谁能帮我吗?非常感谢。代码如下:

ptsAroundVCP_L=[180,188;177,191;174,191;171,191;168,188;] % points with 2-d coordinate 
curLinePar_L=polyfit(ptsAroundVCP_L(:,2),ptsAroundVCP_L(:,1),1); % parameter of the fitted line

%% begin to plot
plotx=1:256;    
figure(11);hold on;
plot(ptsAroundVCP_L(:,2),ptsAroundVCP_L(:,1),'sb');    
ploty_L=polyval(curLinePar_L,plotx);
plot(plotx,ploty_L,'r');
hold off;

输出如下所示。但我期望的是,在这种情况下,拟合线应该是垂直的。我认为线路拟合有问题。 在此处输入图像描述

4

2 回答 2

4

给定的数据不可能拟合任何合理的多项式:

    X     Y
   188   180
   191   177
   191   174
   191   171
   188   168

进行转置,你会得到一些合理的东西:

ptsAroundVCP_L=[180,188;177,191;174,191;171,191;168,188;]

y = ptsAroundVCP_L(:,2);
x = ptsAroundVCP_L(:,1);

p = polyfit(x, y, 2);

plotx= linspace(150, 200, 101);

figure(11);

plot(x, y, 'sb');    
hold on

ploty = polyval(p, plotx);
plot(plotx, ploty, '-');
hold off;

在此处输入图像描述

于 2011-08-05T20:44:50.410 回答
3

我认为问题基本上是您不能以斜率截距形式表示垂直线。如果您在适合的情况下翻转 x/y,您会得到正确的结果:

ptsAroundVCP_L=[180,188;177,191;174,191;171,191;168,188;] % points with 2-d coordinate 
curLinePar_L=polyfit(ptsAroundVCP_L(:,1),ptsAroundVCP_L(:,2),1); % parameter of the fitted line

%% begin to plot
plotx=168:180;
figure(11);hold on;
plot(ptsAroundVCP_L(:,1),ptsAroundVCP_L(:,2),'sb');
ploty_L=polyval(curLinePar_L,plotx);
plot(plotx,ploty_L,'r');
hold off;
于 2011-08-05T20:43:53.720 回答