-2

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
Community
  • 1
  • 1
NESHOM
  • 899
  • 16
  • 46
  • 2
    Your variables are `a` `b` `c` `d` and `e` you have observations `x` and `y` from which you can easily generate `x^2` and `y^2` and solve using least squares – Shai Nov 12 '14 at 21:07
  • Can you refer to any code or link as sample? – NESHOM Nov 12 '14 at 21:20
  • 2
    just follow the example in the thread you linked to – Shai Nov 12 '14 at 21:29
  • So I need to change x.*y to zeros(numel(x),1). is this right? – NESHOM Nov 12 '14 at 21:35
  • 1
    The question you linked to is a duplicate of your question. "not sure how to change the parameters" is not very specific. What have you actually tried and where is your code? Try writing our the two forms on pencil and paper. I'm sure that you can transform one to the other. Try a test case with know solution. – horchler Nov 12 '14 at 21:37
  • If you look at my equation again, it has no xy opposed to the equation that I linked to. xy is related to the tilt of the ellipse which in my case is zero. Now, I'd like to changed the solution into that link for my equation? – NESHOM Nov 12 '14 at 21:41
  • @M0HS3N just remove the `x*y` column from the matrix. But you should really put some code in your question (see [ask]). – David Nov 12 '14 at 23:00
  • @David - I added the code!!! I removed x*y column, however it does not return the correct solution. Any other suggestion? – NESHOM Nov 13 '14 at 01:19

1 Answers1

1

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!

David
  • 8,449
  • 1
  • 22
  • 32