File Exchange

image thumbnail


version (39.3 KB) by Richard Brown
Fits circles to 2D data using nonlinear least squares to minimise geometric error


Updated 21 May 2007

View License

Although a linear least squares fit of a circle to 2D data can be computed, this is not the solution which minimizes the distances from the points to the fitted circle (geometric error). The linear solution minimizes the algebraic error of a function something like
f(x) = ax'x + b'x + c = 0

Minising the geometric error is a nonlinear least squares problem. fitcircle allows you to compute either - it uses the algebraic fit as the initial guess for the geometric error minimization.

x = randn(2, 10);
% Linear least squares fit
[z, r] = fitcircle(x, 'linear')

% True best fit (minimizing geometric error)
[z, r] = fitcircle(x)

For more information look at the published demo file.

This submission is based on the paper:
"Least-squares fitting of circles and ellipses", W. Gander, G. H. Golub, R. Strebel, BIT Numerical Mathematics, Springer 1994

A similar submission for ellipses should be forthcoming

Cite As

Richard Brown (2021). fitcircle.m (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (24)

Julian Guinane

Jonghoon Ryu

lone Ishfaq

I am not getting plot of the circle, can anyone help?

PraveenKumar M


works better than CircleFitByTaubin, CircleFitByPratt, and Landau_Smith, does not overfit

Aaron Greenbaum

Richard Brown

@Tolga, thanks -- and yes, I know. I deliberately wanted to avoid any toolbox dependencies.

I haven't done any comparisons of efficiency -- if you do you could let us know! But accuracy is simply dependent on the tolerance you specify.

Tolga Birdal


I appreciate your effort and this great function. I would just want to remind that (and I am sure you already know) for the nonlinear part it is also possible to use:

options = optimset('Jacobian','on', 'Algorithm','levenberg-marquardt');
u = lsqnonlin(@sys,u,[],[],options);

Did you do any comparisons of accuracy? (Because Walter states that Gauss-Newton performed similarly, while being more efficient)


Richard Brown

My previous comment got lost -- the 2 norm residual is the sum of the squared perpendicular distance from each data point to the fitted circle

Richard Brown

Also, @Graeme, the accuracy of the fitted circle depends on the kinds of errors that are in your data. If you assume there is a true underlying set of parameters that you're trying to find, and that your data is normally distributed, then the accuracy will decrease with number of data points as something like s / sqrt(n), where s is the standard deviation of perpendicular distances to the fitted circle, and n the number of data points. The error will then probably follow some kind of t distribution.



Hi Richard,

Thanks you for sharing your script.

i was wondering if it possible to modify your script to fit an ellipse. if so, do you have any suggestions?

thanks again,

Graeme Crouchley

Hi all,

I was wondering what exactly the 2 norm residual is? Is it the sum of the residuals?

Also how would I use this value (or any other) to determine the accuracy of the fitted circle. i.e what accuracy has the radius of the circle been determined to?





Richard Brown

@Daniel. Yes, it is very easy to do this, and you don't need my file (it has a simple solution).

Assume that you want to force the centre to be 0,0. (if something else, then you can just change coordinates to solve the problem, then change back afterward).

Let your points be in a 2xN array called x. Then the objective you're minimising is
f(r) = \sum_i (\norm{x_i} - r)^2
where x_i is the i-th column of x.

You can minimise using basic calculus
(1) f'(r) = -2\sum_i (\norm{x_i} - r) = -2\sum_i \norm{x_i} + 2Nr
(2) f''(r) = 2N

(1) has a unique minimiser at
r = \frac{\sum_i \norm{x_i}}{N}
and (2) confirms that the function is convex

i.e. in Matlab notation:
r = sum(hypot(x(1, :), x(2, :)) / N;





Great and simple. Any idea if it would be easy to force the center to zero,zero (or anything other coordinate)?

Akash Yalagach

Richard Brown

Hi Brian

This code computes a least squares fit, which is not robust to large outliers - the results you got are not unexpected, nor are they wrong.

Because the objective being minimised is a function of the square of the distance, big outliers will skew the fit. If your data is known to have significant outliers, least squares is not a good choice of objective to minimise.



Brian Wheeler

To test thus function I fed it data for a 360 point unit circle. As expected it returned a unit circle centered on zero.

Distorting one point of the circle to a radius of 6 gave expected results:
radius 1.01408739256055
center [ 3.76743045176789e-17 0.0281775669475842]

Distorting the same point of the circle to a radius of 7 gave UNEXPECTED results:
radius 3.258619140676808
center [1.25049755875451e-15 3.17478833187820]

Thoughts anyone?

SengChoy Liew

if i have a gray scale picture with circles of various diameter touching each other. How can i make use of this fitting to evaluate the distribution in histogram of circles in that pictures ?

I could not find any codes in image processing toolbox for it.

Anyone out there for help ?

Kristopher White

Great work.

Levi Kilcher

This function is fantastic!

Great work, thanks.

Ali Özgül

+Publis M-files application and theory (5)
+Graphical program interface (5)
+Workable program description (5)
+Works references (3)

+M-files complexity (3)
+Mlint error message (5)
+Help problems (4)
+Filename of clashes (5)
+Clone the code line (5)
+Program's symbolic manuscript (3)
+Program samples and runing test (5)
+System requirements (5)
+Program running speed (4)

Files archive:
Seleceted file name (5)
Per files comment (5)
All files usefuly (4)

Very nice application and documentation.
Good works.

MATLAB Release Compatibility
Created with R14SP3
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!