ellipse fitting with matlab using fittype

Hello,
I want to use fitoptions and fittype for a staistical nonlinear least squares fitting approach. The problem is, I cant find the correct equation for my model (ellipse). I tried multiple like: to fittype ('sqrt(b^2*(1-(x^2/a^2)))' ). In this example, the square root has to be positive. I tried to tightening the upper and lower bounderies, but it doesnt work.
Can someone make an example of the following form:
fo = fitoptions('Method','NonLinearLeastSquares')
ft = fittype('ellipse equation')
[myfit, gof] = fit(x_data, y_data, ft, fo)

 Accepted Answer

Matt J
Matt J on 26 Aug 2020
Edited: Matt J on 26 Aug 2020
You could use fit() if you convert your data to polar cooridnates. In polar coordinates, an ellipse is given by an explicit function,

2 Comments

After this I converted it back to cartesian coordinates and plotted the data with my original data.
The pictures shows the original data (blue) and the ellipse fit (red).
Thank you Matt !
The problem with this method, of course, is that it assumes that the ellipse is centered at the origin. In general, that may not be true or known to be true.

Sign in to comment.

More Answers (3)

It shows you how to construct your own function correctly.

6 Comments

Thank you for your fast response.
There are only basic problems. I need a function like f(x,y)= a*x + b*x*y + c* y + d , as model function. How can I transform this function correctly ?
I tried fittype( @(a, b, c ,d, x, y) 'a*x + b*x*y + c* y + d' ), but the syntax isnt correctly, right ?
You could try using a linear fit! Because you know the values of your x’s and y’s you could define, say, Xi = xi^2 and Yi = yi^2. Then your ellipse equation becomes uX + vY = 1, where u and v are the unknown constants to be found, from which the semi-major axes, a and b, of your ellipse can be determined. No need for awkward square roots!
Just how successful the end result would be I can’t say, but it might be worth a try.
This is all I got... Do you have any suggestions ? It's not even close to an ellipse...
Thank you for all you help !!!!
I tried a^2 and a in my model. But it doesnt matter...
No, it doesn't look good! Did you subtract the mean of the x values from all the x-values and the mean of the y values from all the y-values first, to ensure you are centred on zero?
Are you able to upload the basic data that you are trying to fit? I've done some tests with arbitrarily generated ellipse data and the "linear" manipulation works out ok (I didn't use Matlab's fittype - just did a simple matrix least squares fit in core Matlab).
This is the basic data. I want to use fittype because it has so many different options to fit and compare.
Hmm. I'm struggling to see your basic data as elliptical. Nevertheless, the code below tries to fit them using an ellipse (I've deleted a few obvious non-elliptical outliers below; however, you could easily retain them if you wish):
load('EllipseData.mat')
% The next three lines remove outliers that are obviously not part of an
% ellipse! Delete the three lines if you wish to retain the outliers.
ix = find(data_x<-600); data_x(ix) = [];data_y(ix) = [];
iylo = find(data_y<-220); data_x(iylo) = [];data_y(iylo) = [];
iyhi = find(data_y>220); data_x(iyhi) = [];data_y(iyhi) = [];
% Set the origin at the means of the x and y data
x = data_x - mean(data_x);
y = data_y - mean(data_y);
% "Linearise" and do a straightforward least-squares fit
N = length(x);
X = x.^2; Y = y.^2;
M = [N -sum(X); sum(X) -sum(X.^2)];
V = [sum(Y); sum(X.*Y)];
AB = M\V; % AB = [A; B]
% Extract constants
A = AB(1);
B = AB(2);
% Create ellipse semimajor axes from "linear" constants
a = sqrt(A/B);
b = sqrt(A);
% Separate upper and lower halves of "elliptical" values.
xhi = x(x>=0); xlo = x(x<0);
yhi = b*sqrt(1 - (xhi/a).^2);
ylo = -b*sqrt(1 - (xlo/a).^2);
% Put them back together and shift the origin back
xf = [xhi xlo] + mean(data_x);
yf = [yhi ylo] + mean(data_y);
% Plot and compare Data and fit.
plot(data_x,data_y,'o',x,yf,'*'), grid
xlabel('x'),ylabel('y')
legend('Data','"Elliptical" fit')
This is the result:

Sign in to comment.

Matt J
Matt J on 26 Aug 2020
Edited: Matt J on 26 Aug 2020
You cannot use fit() for implicit function fitting, but you could use dedicated ellipse fitting methods from the File Exchange, e.g.,

1 Comment

so theres no way to use this method for ellipse fitting ?
Thank you both !!! I really appreciate your support.

Sign in to comment.

For what it's worth, attached is a paper that describes the method.
Least Squares orthogonal distances fitting of circle, sphere, ellipse, hyperbola, and parabola.pdf

Categories

Asked:

on 25 Aug 2020

Commented:

on 27 Aug 2020

Community Treasure Hunt

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

Start Hunting!