最小二乘法拟合平面

输入:三维点集(X1,Y1,Z1),...,(Xn,Yn,Zn)

输出:平面方程 Z=AX+BY+D 中参数A,B,D

解:

\begin{pmatrix} \sum Xi^{2} & \sum XiYi & \sum Xi \\ \sum XiYi & \sum Yi^{2} & \sum Yi \\ \sum Xi & \sum Yi & N \end{pmatrix} \begin{bmatrix} A \\ B \\ D \end{bmatrix} = \begin{bmatrix} \sum XiZi \\ \sum YiZi \\ \sum Zi \end{bmatrix}

% 最小二乘法拟合平面 Z=AX+BY+D  v=[A,B,D]'
% 不适用 AX+BY=c 的平面拟合
clear all;
clc;
X = [1.0, 0.0, 0.0, 0.333];
Y = [0.0, 1.0, 0.0, 0.333];
Z = [0.0, 0.0, 1.0, 0.333];
N = length(X);
Xsum = sum(X);
Ysum = sum(Y);
Zsum = sum(Z);
XXsum = dot(X, X);
XYsum = dot(X, Y);
YYsum = dot(Y, Y);
XZsum = dot(X, Z);
YZsum = dot(Y, Z);
M = [XXsum, XYsum, Xsum; XYsum, YYsum, Ysum; Xsum, Ysum, N];
u = [XZsum, YZsum, Zsum]';
v = M^(-1) * u;

参考:最小二乘法拟合平面_konglingshneg的博客-CSDN博客_最小二乘法拟合平面

你可能感兴趣的:(拟合,最小二乘法,平面,线性代数)