How to fit a line on the plane?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Mr M.
el 24 de En. de 2019
Editada: David Goodmanson
el 2 de Feb. de 2019
How to fit a line onto dots on the plane? polyfit is not the solution, because it is good for functions and not for points on the plane. ax+b function fit fails for vertical lines for example, and noise model also differs
0 comentarios
Respuesta aceptada
David Goodmanson
el 28 de En. de 2019
Editada: David Goodmanson
el 2 de Feb. de 2019
Hi M,
Here is a way to fit a line in 2d with the equation
a1*x1 + a2*x2 = 1
where (x1,x2) are (x,y). This representation gets rid of infinite slope problems.
The same code works in general in m dimensions to fit an m-1 dimensional plane
a1*x1 + a2*x2 + ... am*xm = 1.
For n points, let X be an nxm matrix where each row in X represents a point in m dimensions.
X = rand(20,2) % for example
Xcm = mean(X); % coordinates of center of mass
[~,s,v] = svd(X-Xcm,'econ');
n = v(:,end); % unit vector for the smallest singular value (they are sorted)
L = Xcm*n; % displacement of the best fit plane from the origin
if L < 0 % turn displacement into distance
L = -L;
n = -n;
end
a = n/L; % fitted plane, a1*x1 + a2*x2 + ... am*xm = 1.
drms = rms(X*n -L);
% L = 1/norm(a), so drms also equals (1/norm(a))*rms(X*a-1)
drms is the rms distance of the set of points to the plane.
For older versions of Matlab, in the svd line you would have to use
X-repmat(Xcm,size(X,1),1) in place of X-Xcm
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Curve Fitting Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!