# How to plot the best fitted ellipse or circle?

14 vues (au cours des 30 derniers jours)
Ashfaq Ahmed le 24 Oct 2023
Modifié(e) : Matt J le 25 Oct 2023
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.
x = xy.x(:,1);
y = xy.x(:,2);
plot(x,y,'o')
axis equal
##### 5 commentairesAfficher 3 commentaires plus anciensMasquer 3 commentaires plus anciens
Ashfaq Ahmed le 25 Oct 2023
Modifié(e) : Ashfaq Ahmed le 25 Oct 2023
Hi @Image Analyst, the secoond option. It would be if they are plotted in the polarplot first and then creating the ellipsoid.
Image Analyst le 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);

Connectez-vous pour commenter.

### Réponse acceptée

Matt J le 25 Oct 2023
Modifié(e) : Matt J le 25 Oct 2023
Is this the kind of thing you are looking for?
p=prunecloud(xy);
Warning: Polyshape has duplicate vertices, intersections, or other inconsistencies that may produce inaccurate or unexpected results. Input data has been modified to create a well-defined polyshape.
I=all(~isnan(p.Vertices),2);
e=ellipticalFit(p.Vertices(I,:)');
%Display -- EDITED
XY=e.sample(linspace(0,360,1000));
[t,r]=cart2pol(xy(:,1),xy(:,2));
[T,R]=cart2pol(XY{:});
polarplot(t,r,'ob',T,R,'r-')
function [p,XY]=prunecloud(xy)
for i=1:3
D2=max(pdist2(xy,xy,'euclidean','Smallest', 10),[],1);
xy(D2>0.1,:)=[];
end
XY=xy;
b=boundary(XY,0.95);
p=polyshape(XY(b,:));
end
##### 4 commentairesAfficher 2 commentaires plus anciensMasquer 2 commentaires plus anciens
Ashfaq Ahmed le 25 Oct 2023
Hi @Matt J, thank you for the suggestion. I can see it is working now. I have a request. Can you please help me plot it on a polar plane?
Matt J le 25 Oct 2023
Modifié(e) : Matt J le 25 Oct 2023

Connectez-vous pour commenter.

### Plus de réponses (3)

Image Analyst le 24 Oct 2023
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

Torsten le 24 Oct 2023
Modifié(e) : Torsten le 24 Oct 2023
1. Compute the center of gravity of the point cloud. Call it (x',y').
2. Compute the point of your point cloud with the greatest distance to (x',y'). Call the distance R.
3. Define the circle that encloses the point cloud by (x-x')^2 + (y-y')^2 = R^2.
##### 6 commentairesAfficher 4 commentaires plus anciensMasquer 4 commentaires plus anciens
Les Beckham le 25 Oct 2023
Thanks
Image Analyst le 25 Oct 2023
I moved @Les Beckham's comment to be an answer.

Connectez-vous pour commenter.

Les Beckham le 25 Oct 2023
Déplacé(e) : Image Analyst le 25 Oct 2023
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')
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by