"Roger Stafford" wrote in message <j09o0e$kvh$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <j09nfh$jce$1@newscl01ah.mathworks.com>...
> > "Samanbarak" wrote in message <j09aqm$94j$1@newscl01ah.mathworks.com>...
> > > I have x y z data and I want to fit a cylinder on these data. I tried surface fitting tool and I tried different equations in custom equation part but they did not work! Would you tell me if I can use that tool for this purpose or not? which equation should I use?
> >        
> > It requires four parameters to uniquely determine a line in threedimensional space. One additional parameter is needed for the radius of a cylinder for which the line is its central axis. One way to proceed is to write a function which gives the distance of each of your data points to the nearest point on a cylinder determined by these five parameters. Then call on 'lsqnonlin' of the Optimization Toolbox to find the least squares best fitting five parameters.
> >
> > Roger Stafford
>           
> You might encounter difficulties with 'lsqnonlin' due to the fact that absolute values are involved in the distance formulas for a cylinder and the derivatives would be discontinuous. An alternative might be to compute the sum of the squares of all those distances yourself as a function of your five parameters and hand the problem to 'fminunc'.
>
> Roger Stafford
Hi Roger, i am also doing best fit cylinder and found this thread, like you have said i have calculated the four parameter a, b , e , f and the distance here is the code i have written . now i want to plot the data's i don't how to do. can you help me furthur
clear all
M=dlmread('fullmatrix.txt');
% M is a matrix of size n1*n2 x 3. n1 is the number of points
% per trace. n2 is the number of traces. The first column
% contains angle data (in radians), second column contains
% z height and third column has radial deviations
n = size(M,1)
for i=1:n
A(i,1) = cos(M(i,1));
A(i,2) = sin(M(i,1));
A(i,3) = M(i,2)*cos(M(i,1));
A(i,4) = M(i,2)*sin(M(i,1));
A(i,5) = 1;
B(i,1) = M(i,3);
end
P = A\B
a = P(1)
b = P(2)
e = P(3)
f = P(4)
r = P(5)
% the best fit cylinder is represented by
%R(theta,z) = r+((a+z*e)*cos? )+((b+z*f )*sin?)
R = (r + (a+M(:,1)*e).*cos(M(:,2)) + (b+f*M(:,1)).*sin(M(:,2)));
% now it will calculate the distances of every point from a bestfit cylinder
m = size(M,2);
for i=1:m
d(i,1) = M(i,3)  ((a+M(i,2)*e)*cos(M(i,1))+ (b+M(i,2)*f)*sin(M(i,1)) + r);
end
