How to plot the best fitted ellipse or circle?
조회 수: 23 (최근 30일)
이전 댓글 표시
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
Image Analyst
2023년 10월 25일
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);
채택된 답변
Matt J
2023년 10월 25일
편집: Matt J
2023년 10월 25일
The code below uses ellipsoidalFit() from this FEX download,
Is this the kind of thing you are looking for?
xy=load('EllipseData.mat').x;
p=prunecloud(xy);
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
추가 답변 (3개)
Les Beckham
2023년 10월 25일
이동: Image Analyst
2023년 10월 25일
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')
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Scatter Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!