help-octave
[Top][All Lists]

## Re:fitting an ellipse

 From: William Lash Subject: Re:fitting an ellipse Date: Sat, 21 Dec 2002 15:38:04 -0600

```It's been a while since I have done any of this, but you could
use the pinv() function to do the fit.  I don't remember if
the psuedo-inverse is equivalent to a least squares fit, but
it will provide a fit for this case.

I would rewrite your equations as:

z=A*u

where the first row of z is x and the second is y, A is
the matrix [ a 0; 0 b], and the fist row of u is sin(t)
and the second is cos(t), e.g. if a is 2 and b is 1:

t=[0:0.01:2*pi];
A=[2 0;0 1];
u=[sin(t);cos(t)];
z=A*u;
plot(z(1,:)',z(2,:)');

Now to get A from z and u, you can multiply each side
by the psuedo-inverse of u, leaving you with

Aest=z*pinv(u)

Which in the case here, returns:

Aest =

2.0000e+00    3.4534e-16
-1.7144e-18    1.0000e+00

You could add some noise to z, since the observations will have
some noise:

zobs=z+0.2*randn(size(z));

and do the psuedo-inverse again:

Aest=zobs*pinv(u)
Aest =

2.0138864  0.0061711
0.0089173  1.0124481

and then calculate the new data:

z2=Aest*u;
plot(z2(1,:)',z2(2,:)');

I guess the problem here is that you are not limiting the fit to
x=a*sin(t), but instead to x=a*sin(t)+a1*cos(t), and similarly for
y, so you may want to continue to look for something better.

Bill

-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------

```