Fit a least squares ellipse

21 ビュー (過去 30 日間)
B
B 2022 年 4 月 21 日
編集済み: Matt J 2022 年 4 月 21 日
I am trying to fit a least square ellipse to to the 'ellipse' data set that i have attached
I feel like i have everything correct (i might not have) but i am struggling plottting the ellipse function 'f'. i keep getting the following error
Warning: Error updating ImplicitFunctionLine.
Arrays have incompatible sizes for this operation.
Any help would be appreciated, Thanks in advance
load ellipse % loads x and y
%least squares fit to an ellipse
% A x^2 + B xy + C y^2 + Dx + Ey = 1
% Returns coefficients A..F
%A x^2 + B xy + C y^2 + Dx + Ey + F = 0
% where F = -1
M = [x.^2 x.*y y.^2 x y]; % design matrix
b1=ones(size(x));
MTM=M'*M;MTb=M'*b1; %normal equation
R=rref([MTM MTb]); % the coefficent are found in the last collunm
A=R(1:6);
B=R(2,6);
C=R(3,6);
D=R(4,6);
E=R(5,6);
f= @(x,y) A.*x.^2 + B.*x.*y+C.*y.^2+D.*x+E.*y-1; % ellipse function
figure, plot(x, y, '.')
hold on
fimplicit(f)
  1 件のコメント
Alex Sha
Alex Sha 2022 年 4 月 21 日
Function: A*x^2 + B*x*y + C*y^2 + D*x + E*y + 1 = 0
Parameter:
A: -0.550741506585047
B: -0.46781995608367
C: -0.804115378029277
D: -0.163329836528614
E: 2.72555198327659

サインインしてコメントする。

採用された回答

KSSV
KSSV 2022 年 4 月 21 日
編集済み: KSSV 2022 年 4 月 21 日
Replace this line:
A=R(1:6);
with
A=R(1,6);
There is a typo error in getting A. A is a vector in your case, you arenot getting the ellipse.

その他の回答 (1 件)

Matt J
Matt J 2022 年 4 月 21 日
編集済み: Matt J 2022 年 4 月 21 日
I feel like i have everything correct (i might not have)
Your ordinary least squares method is not ideal because your design matrix has stochastic errors in it (iterative total least squares is the gold standard). The method used by this toolbox is a bit better,
and also has utility functions to let you plot directly,
load ellipse
fitobj=ellipticalFit([x,y]')
fitobj =
ellipticalFit with properties:
center: [-0.9975 1.9968]
a: 3.0246
b: 1.9954
angle: -30.0868
plot(fitobj)

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by