Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
fit cylinder on data

Subject: fit cylinder on data

From: Samanbarak

Date: 21 Jul, 2011 13:50:14

Message: 1 of 5

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?

Subject: fit cylinder on data

From: Roger Stafford

Date: 21 Jul, 2011 17:26:09

Message: 2 of 5

"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 three-dimensional 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

Subject: fit cylinder on data

From: Roger Stafford

Date: 21 Jul, 2011 17:35:10

Message: 3 of 5

"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 three-dimensional 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

Subject: fit cylinder on data

From: prabhakaran m

Date: 7 Apr, 2012 09:16:13

Message: 4 of 5

"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 three-dimensional 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 best-fit 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

Subject: fit cylinder on data

From: prabhakaran m

Date: 12 Apr, 2012 05:05:18

Message: 5 of 5


if i do, plot3( A(i,3), A(i,4),R) i am not getting the cylinder surface. rather the plot is of sinusoidal wave. what mayb the prob??

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us