MATLAB中直线与二维点的拟合
我尝试在MATLAB中计算一条线,它可以用二维坐标拟合给定的几个点。但结果并不是我所期望的。也许有什么我理解错了。有人能帮我吗?谢谢。代码如下:MATLAB中直线与二维点的拟合,matlab,curve-fitting,Matlab,Curve Fitting,我尝试在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 fitte
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;
输出如下所示。但我所期望的是,在这种情况下,装配线应该垂直。我认为线路安装有问题。
我认为问题基本上在于不能以坡度截距形式表示垂直线。如果您在合适的情况下翻转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;
我认为问题基本上是,你不能用斜率截距的形式表示垂直线。如果您在合适的情况下翻转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;
不可能将任何合理的多项式拟合到此数据,如下所示:
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;
不可能将任何合理的多项式拟合到此数据,如下所示:
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;