Given a set of x,y,z coordinates, find the best planar fit to the points via a least squares regression. Testing script "t_fitNormal" is also included to verify functionality and demonstrate usage.
No Problem. Thanks for the feedback. A new version was just submitted and should be accepted soon. If you need to you can also pull it from git https://github.com/MathYourLife/Matlab-Tools
Thank for the feedback Amir. It's likely that your data doesn't fit in the window set by the code. It's hard coded now for [-1, 1] as you can see on this line.
I'll see if I can get that updated so that it jumps to wherever you data set is. I'll probably keep the range of all the axes equal though or it will make the plot appear as though the solution does not come out to be normal to the plane.
Thanks nicely does the job, I checked the normal on my data and it is correct. I just got an empty axis for plot feature which I do not have any idea why! Any thing to take care of, that I missed?
Thanks for the question. Could you elaborate a little on the angle you are measuring? Is it between the normal and one of the coordinate axes? or the angle between multiple normal vectors? Since the planes being fit have two normal vectors the function will produce only one of them. If possible, could you provide a sample data set that you would expect to give you an angle greater than 135 degrees?
This utility uses a least squares regression in which the formula minimizes the sum of the squares of the residuals. The residual is the difference between what the model expects and the actual measured value. So in most cases this would be the difference between the predicted z value and the actual z value.
That is all fine and dandy until your data starts to have a steep slope. In the worse case, for a plane with normal in the x-y plane the residuals of z_meas - z_exp trend to +-infinity.
The way the script handles this issue is to perform the regression three times. Each time using residuals measured in the x, y, and z directions (You'll see those as the for i=1:3 loops in the source). After the residuals are performed, if we run into a case similar to the one just described the z residual value will be very large and therefore not trustworthy.
The decision on which residual to use is made on this line.
best_fit = find(residual_sum == min(residual_sum));