其他分享
首页 > 其他分享> > 拟合平面,并求法向量

拟合平面,并求法向量

作者:互联网

最小二乘法拟合平面的代码

%% 最小二乘法拟合平面
x = rand(1,10);
y = rand(1,10);
z = (3-2*x-5*y)/4;

Xcolv = x(:);
Ycolv = y(:);
Zcolv = z(:);

Const = ones(size(Xcolv));
Coefficients = [Xcolv Ycolv Const] \ Zcolv;
XCoeff = Coefficients(1);
YCoeff = Coefficients(2);
CCoeff = Coefficients(3);
L=plot3(x,y,z,'ro');

  

 

matlab自带的函数拟合平面的方法

%% 用matlab的regress命令进行平面拟合
x = [1 5 6 3 7]';
y = [2 9 3 5 8]';
z = [4 3 5 11 6]';
scatter3(x,y,z, 'filled');
X = [ones(5,1) x y];
% z = 6.5642 - 0.1269x - 0.0381y
b = regress(z, X);
xfit = min(x):0.1:max(x);
yfit = min(y):0.1:max(y);
[XFIT, YFIT] = meshgrid(xfit,yfit);% 绘制网络
ZFIT = b(1) + b(2)*XFIT + b(3)*YFIT;
surf(XFIT, YFIT, ZFIT) % 绘制平面

  

已经知道几个点,拟合平面,并求平面的法向量

%% 已知道某个点的周围的局部几个点,拟合平面,并求平面的法向量
x = rand(1,10);
y = rand(1,10);
z = (3-2*x-5*y)/4;
Xcolv = x(:);
Ycolv = y(:);
Zcolv = z(:);
Const = ones(size(Xcolv));
Coefficients = [Xcolv Ycolv Const] \ Zcolv;
XCoeff = Coefficients(1);
YCoeff = Coefficients(2);
CCoeff = Coefficients(3);

%% 平面的单位法向量
% XCoeff是x前面的系数, YCoeff是y前面的系数
Norm = [-XCoeff, -YCoeff, 1] / sqrt(XCoeff*XCoeff+YCoeff*YCoeff+1);

  

 

标签:Xcolv,XCoeff,求法,YCoeff,向量,拟合,平面,Coefficients
来源: https://www.cnblogs.com/wylwyl/p/10940094.html