How to determine the general equation of an ellipse using lsqnonlin

5 views (last 30 days)
Hello.
Given a set of points (xk,yk) how to determine de general equation of an ellipse: P(x,y) = Ax + Bxy + Cy^2 + Dx + Ey + F = 0
I've tried a few methods (direct least square, Kanatani's hypernormalization, Ellipse fitting with sampson distance). However, this time, I am trying to use de lsqnonlin command. The main difficulty is to guarantee that the coefficients really represent an ellipse, i.e., how do I add the condition: B^2-4*A*C<0 to the lsqnonlin ?
Thank you in advance.

Accepted Answer

Matt J
Matt J on 28 Jun 2014
You would probably have to use fmincon, rather than lsqnonlin. It can accommodate more general nonlinear constraints like B^2-4*A*C<=0.
  7 Comments
Matt J
Matt J on 30 Jun 2014
Edited: Matt J on 30 Jun 2014
It should look like
tHeta = fmincon(@(tHeta) fit_simp(tHeta,xData,yData),...
tHeta0,[],[],[],[],[],[],@condfun,options);
with objective function
function val = fit_simp(tHeta,xData,yData)
a = tHeta(1);
B = tHeta(2);
c = tHeta(3);
d = tHeta(4);
e = tHeta(5);
f = tHeta(6);
diff = a.*xData.^2 + B.*xData.*yData + c.*yData.^2 + d.*xData + e.*yData + f;
val=norm(diff(:))^2;
end
Matt J
Matt J on 30 Jun 2014
Edited: Matt J on 30 Jun 2014
Even better, use more linear algebra ops,
xData=xData(:);
yData=yData(:);
M=[xData.^2, xData.*yData, yData.^2, xData ,yData ];
M(:,end+1)=1;
tHeta = fmincon(@(tHeta) norm(M*tHeta(:))^2,...
tHeta0,[],[],[],[],[],[],@condfun,options);
Note that this way, you can get rid of fit_simp.m completely.

Sign in to comment.

More Answers (0)

Categories

Find more on Systems of Nonlinear Equations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!