Plotting ellipse from parameters

4 views (last 30 days)
yotam ohad
yotam ohad on 20 Nov 2015
Commented: Matthew Crema on 20 Nov 2015
Hello, I have a code that fits an ellipse to my data so I could extract the phase difference between two signals. I would like to plot the fitted ellipse but I can not figure out how to do it. I have the six parameters of an ellipse [A B C D E F] and the xdata and ydata.
My Code:
%
noise = 0.05;
amp = 8 ;%+ rand;
x0 = rand - 0.5;
y0 = rand - 0.5;
t = 0:0.005:20;
V1 = amp*cos(15*cos(2*pi*t)) ;% + x0 + randn(size(t))*noise;
V2 = amp*cos(15*cos(2*pi*t)+ 0.5);% + y0 + randn(size(t))*noise;
%
error_func = @(p) sum( (p(1)*V1.^2 + p(2)*V1.*V2 + p(3)*V2.^2 + p(4)*V1 + p(5)*V2 + p(6) ).^2 );
Xc = mean(V1);
Yc = mean(V2);
theta = atan(max(V1)/max(V2));
tV1 = V1*cos(theta) - V2*sin(theta);
tV2 = V1*sin(theta) + V2*cos(theta);
a = max(tV1);
b = max(tV2);
A = a^2*sin(theta)^2+b^2*cos(theta)^2;
B = 2*(b^2-a^2)*sin(theta)*cos(theta);
C = a^2*cos(theta)^2+b^2*sin(theta)^2;
D = -2*A*Xc-B*Yc;
E = -B*Xc-2*C*Yc;
F = 1;
p0 = [A B C D E F];
Aeq = [0 0 0 0 0 1];
beq = 1;
p = fmincon(error_func,p0,[],[],Aeq,beq,[],[],@cons);
phase = acos(-p(2) / (2*sqrt(p(1)*p(3))) );
Thanks for the help
  1 Comment
Matthew Crema
Matthew Crema on 20 Nov 2015
Are A through F coefficients in the general equation of a conic section? If so try searching for IMCONIC on the file exchange (but I think B should be zero if so).

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!