function planefull(data) % DRAWPLANE.M Draws a localized least-squares planar approximation for a set of points % Darren Pais, Caltech, July 23, 2005 % INPUT: a nx3 matrix where n is the number of points used - Minimum no of % pts is 3 ! % OUTPUT: a mesh grid planar approximation of the points, RMS of residuals, plane dip angle % calculate centroid of give data centroid=mean(data) % translate points to form matrix M M=[(data(:,1) - centroid(1) ) (data(:,2) - centroid(2) ) (data(:,3) - centroid(3) ) ] ; % perform singular value decomposition (SVD) on matrix M=USV' [U,S,V]= svd(M) ; %find the smallest singular value of M and its position i [small,i]=min(diag(S)); %find the singular vector corresponding to singular value small, this %vector is the vector normal to the best plane. since the columns of S are %orthonormal, the singular vector we find will correspond to the direction %cosines of the normal to the plane. dircos=V(:,i) % calculate 'D' in plane equation Ax+By+Cz = D D=sum(centroid*dircos) ; % form localized grid mesh bounds. xmin=min( data(:,1)) ; xmax=max( data(:,1)) ; ymax=max( data(:,1)) ; ymax=max( data(:,2)) ; ymin=min( data(:,2)) ; % this scaling is variable dependent on the data you input x=xmin:1000:xmax ; y=ymin:1000:ymax ; % draw plane [X,Y]=meshgrid(x,y) ; Z = (D-dircos(1,1)*X - dircos(2,1)*Y)/dircos(3,1) ; hold on ; mesh(X,Y,Z) %determine plane dip angle i.e., the z-direction gradient of the plane plane_dip_angle = acosd(dircos(3,1)) %determine the residuals, i.e., the distances of given data points to %best-fit plane [m,n] = size(data) ; for i=1 : 1 : m distmat(i,1)= data(i,1)*dircos(1,1)+data(i,2)*dircos(2,1)+data(i,3)*dircos(3,1) - D ; distmat(i,1)=distmat(i,1)*-1 ; end %Calculate RMS value of the residuals sum1=0 ; for i=1 : 1 : m sum1=sum1+distmat(i)^2 ; end %Display RMS value RMS = ( sum1 / m )^.5 %Bar plot for data point vs. distance of point from plane. figure ; bar(distmat,1) colormap summer xlabel('Data Point Index') ; ylabel('Distance in Meters') ;