% Script A3P1

close all

% Generate data by placing all but two points on an ellipse with
% center (xc,yc), semiaxes a and b, and tilt phi
n = 30; theta = linspace(0,2*pi,n); c = cos(theta); s = sin(theta);
a = 5; b = 3; phi = pi/6; xc = 1; yc = 2;
Z = [cos(phi) -sin(phi);sin(phi) cos(phi)]*[a*c ; b*s];
x = xc+Z(1,:)';
y = yc+Z(2,:)';
x(15) = -2; y(15) = 5;
x(16) = -3; y(16) = 6;

% Compute the fitting ellipse...
[A,B,C,D,E,F,q] = IsAnEllipse(x,y);

% Display the fitting ellipse and highlight the points upon which it is
% based...
N = 100; tx = linspace(-5,7,N); ty = linspace(-4,7,N); Z = zeros(N,N);
for i=1:N
    for j=1:N
        Z(j,i) = A*tx(i)^2 + B*tx(i)*ty(j) + C*ty(j)^2 + D*tx(i) + E*ty(j) + F;
    end
end
contour(tx,ty,Z,[0 0])
title(sprintf('B = %6.3f   C = %6.3f   D = %6.3f   E = %6.3f   F = %6.3f ',B,C,D,E,F))
hold on 
plot(x,y,'*',x(1:q),y(1:q),'or')
