How to fit a line on the plane?

1 次查看(过去 30 天)
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

采纳的回答

David Goodmanson
David Goodmanson 2019-1-28
编辑:David Goodmanson 2019-2-2
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 个)

类别

Help CenterFile Exchange 中查找有关 Curve Fitting Toolbox 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by