Fitting an ellipse with no tilt on data

467 views Asked by At

I'd like to fit an ellipse with no tilt on my data. This is the equation of an ellipse with no tilt:

a*x^2 + b*y^2 + c*x + d*y = e

I found this solution (https://stackoverflow.com/a/12717181/3179989) interesting, but not sure how to change the parameters to get the solution to my problem.

Any help is appreciated.

EDIT: here is the code I am using:

[y.^2,x,y,ones(numel(x),1)]\x.^2

ans =
1.0e+04 *
-0.0000
 0.0168
-0.0014
 3.6390
1

There are 1 answers

0
David On BEST ANSWER

This does seem to work:

%// Creating some test data
x=sin(pi*(2*rand(50,1)-1))+(2*rand(size(x))-1)*.5;x=x./max(abs(x));
y=(sqrt(1-x.^2)+(2*rand(size(x))-1)*.5).*sign(rand(size(x))-0.5)+.5*x;

%// Setup Van der Monde matrices and solve equations
A=[y.^2,x.*y,x,y,ones(numel(x),1)]\x.^2
B=[y.^2,x,y,ones(numel(x),1)]\x.^2

plot(x,y,'o') %// Plot initial data
hold on
%// Plotting results the lazy way!
[X,Y]=meshgrid(1.5*(min([x;y]):.001:max([x;y])));
contour(X,Y,-X.^2+A(1)*Y.^2+A(2)*X.*Y+A(3)*X+A(4)*Y+A(5),[0 0],'b')
contour(X,Y,-X.^2+B(1)*Y.^2+B(2)*X+B(3)*Y+B(4),[0 0],'k')
hold off

The blue is the original ellipse and the black is the non-rotated ellipse

Ellipses!