I am looking to create a best fit line through four points with x, y, and z coordinates and then measure the angle of that line with respect to the z-normal vector. I am unable to find any user functions for easily doing this.
I followed the code from another help topic (https://www.mathworks.com/matlabcentral/answers/405461-how-can-i-create-a-linear-line-of-best-fit-for-a-set-of-3-dimensional-coordinates), but the line of best fit does not look right (see attached). Does anyone know what I am doing wrong?
xyz = [Xl_n{1}(:,15:18)]'; %Xl_n is 3d coordinates matrix z = xyz(:,3); r0=mean(xyz); xyz=bsxfun(@minus,xyz,r0); [~,~,V]=svd(xyz,0); x_fit=@(z_fit) r0(1)+(z_fit-r0(3))/V(3,1)*V(1,1); y_fit=@(z_fit) r0(2)+(z_fit-r0(3))/V(3,1)*V(2,1); figure(1),clf(1) plot3(xyz(:,1),xyz(:,2),xyz(:,3),'ro') hold on plot3(x_fit(z_SI),y_fit(z_SI),z,'r')
Best Answer