matlab拟合平面,拟合平面，并求法向量

%% 最小二乘法拟合平面

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);

07-18 2901

11-29
07-26 4318
04-12 1万+
06-14 1423
04-02 3674
05-12 1467
03-17 2429
10-31 1万+
12-15 1154
09-24 1573
12-01 4万+
06-29 2070

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助