-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathplane_fit.m
33 lines (31 loc) · 1.12 KB
/
plane_fit.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function [A,B,C]=plane_fit(x,y,z)
% function [A,B,C]=plane_fit(x,y,z)
% ------------------------------------------------------------------------
% Fit a plane to x,y,z data.
% [A,B,C]=plane_fit(x,y,z) calculates the coefficients A,B,C that fit the data
% defined by the vectors x,y,z.
%
% Uses command svd
%
% %EXAMPLE:
%
% [x,y]=meshgrid(linspace(0,10,20),linspace(0,10,20));
% a=1; b=2; c=-2;
% z=(a*x)+(b*y)+c;
% x=x(:); y=y(:); z=z(:);
% z=z+(randn(length(z),1));
% [A,B,C]=plane_fit(x,y,z);
% [X,Y]=meshgrid(linspace(min(x),max(x),20),linspace(min(y),max(y),20));
% Z=(A*X)+(B*Y)+C;
% plot3(x,y,z,'r.'); hold on; grid on;
% surf(X,Y,Z,'FaceColor','g'); alpha(0.5);
% title(['a=',num2str(a), ', A=',num2str(A),', b=',num2str(b),', B=',num2str(B),', c=',num2str(c),', C=',num2str(C)]);
%
% Kevin Mattheus Moerman
% kevinmoerman@hotmail.com
% 14/08/2008
% ------------------------------------------------------------------------
P=[mean(x),mean(y),mean(z)];
[U,S,V]=svd([x-P(1),y-P(2),z-P(3)],0);
N=-1/V(end,end)*V(:,end);
A=N(1); B=N(2); C=-P*N;