How to plot the best fitted ellipse or circle?
Show older comments
Hi all,
I have a data set (attached here) that has two arrays. I want to plot them in a polar graph and want to find out the best fitted a) ellipse, and b) circle.
x(:,1) is the x and x(:,2) is the y for the plot.
If anyone can help me out here, I will be very grateful.
xy = load("EllipseData.mat");
x = xy.x(:,1);
y = xy.x(:,2);
plot(x,y,'o')
axis equal
5 Comments
Ashfaq Ahmed
on 24 Oct 2023
Edited: Ashfaq Ahmed
on 24 Oct 2023
Image Analyst
on 25 Oct 2023
Do you want to find the convex hull points and then fit those to an ellipse (so all of them are inside), or do you want to find the ellipse that contains a vertain percentage of the points?
Ashfaq Ahmed
on 25 Oct 2023
Edited: Ashfaq Ahmed
on 25 Oct 2023
Image Analyst
on 25 Oct 2023
I see you accepted @Matt J's answer. You can adjust/control the approximate number of points within the ellipse by changing the 0.95 in this line of code:
b=boundary(XY,0.95);
Accepted Answer
More Answers (3)
Image Analyst
on 24 Oct 2023
1 vote
See the FAQ:
- Compute the center of gravity of the point cloud. Call it (x',y').
- Compute the point of your point cloud with the greatest distance to (x',y'). Call the distance R.
- Define the circle that encloses the point cloud by (x-x')^2 + (y-y')^2 = R^2.
6 Comments
Circular fitting is way easier based on @Torsten's approach. Elliptical fitting is much harder, as you need to determine the semi-major and semi-minor axes. However, you can gradually decrease the length of the semi-minor axis until the farthest point in the y-direction is contained within the ellipse, or use @Matt J's approach.
% Load data
xy = load("EllipseData.mat");
x = xy.x(:,1);
y = xy.x(:,2);
% Find center of a single cluster based on Euclidean
xbar = sum(x)/length(x); % mean(x)
ybar = sum(y)/length(y); % mean(y)
center = [xbar ybar]
% Find max distance of data from the center
radius = max(sqrt((x - xbar).^2 + (y - ybar).^2))
% Plot data, the center, and a circle
plot(x, y, '.', 'markersize', 3), hold on
plot(xbar, ybar, 'xk', 'markersize', 12, 'linewidth', 3)
viscircles(center, radius, 'LineStyle', '--') % requires Image Processing Toolbox™
grid on, xlabel('x'), ylabel('y')
axis([-8 8 -8 8])
axis equal
Ashfaq Ahmed
on 25 Oct 2023
xy = load("EllipseData.mat");
x = xy.x(:,1);
y = xy.x(:,2);
rho = sqrt(x.^2 + y.^2);
theta = atan(y./x);
polarplot(theta, rho, '.', 'markersize', 3, 'Color', '#aa4488')
Sam Chak
on 25 Oct 2023
@Les Beckham 👍. Should have posted this as an Answer so that I can upvote yours.
Les Beckham
on 25 Oct 2023
Thanks
Image Analyst
on 25 Oct 2023
Les Beckham
on 25 Oct 2023
Moved: Image Analyst
on 25 Oct 2023
xy = load("EllipseData.mat");
x = xy.x(:,1);
y = xy.x(:,2);
rho = sqrt(x.^2 + y.^2);
theta = atan2(y,x); % <<< use 4 quadrant atan2
polarplot(theta, rho, '.', 'markersize', 3, 'Color', '#aa4488')
Categories
Find more on Scatter Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



