Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7178273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:53:16+00:00 2026-05-28T16:53:16+00:00

I am trying to implement least squares circle fitting following this paper (sorry I

  • 0

I am trying to implement least squares circle fitting following this paper (sorry I can’t publish it). The paper states, that we could fit a circle, by calculating the geometric error as the euclidean distance (Xi”) between a specific point (Xi) and the corresponding point on the circle (Xi’). We have three parametres: Xc (a vector of coordinates the center of circle), and R (radius).

Circle fitting
Equations

I came up with the following MATLAB code (note that I am trying to fit circles, not spheres as it is indicated on the images):

function [ circle ] = fit_circle( X )
    % Kör paraméterstruktúra inicializálása
    %   R  - kör sugara
    %   Xc - kör középpontja
    circle.R  = NaN;
    circle.Xc = [ NaN; NaN ];

    % Kezdeti illesztés
    % A köz középpontja legyen a súlypont
    % A sugara legyen az átlagos négyzetes távolság a középponttól
    circle.Xc = mean( X );
    d = bsxfun(@minus, X, circle.Xc);
    circle.R  = mean(bsxfun(@hypot, d(:,1), d(:,2)));
    circle.Xc = circle.Xc(1:2)+random('norm', 0, 1, size(circle.Xc));

    % Optimalizáció
    options = optimset('Jacobian', 'on');
    out = lsqnonlin(@ort_error, [circle.Xc(1), circle.Xc(2), circle.R], [], [], options, X);
end
%% Cost function
function [ error, J ] = ort_error( P, X )
    %% Calculate error
    R = P(3);
    a = P(1);
    b = P(2);

    d = bsxfun(@minus, X, P(1:2));      % X - Xc
    n = bsxfun(@hypot, d(:,1), d(:,2)); % || X - Xc ||
    res = d - R * bsxfun(@times,d,1./n);
    error = zeros(2*size(X,1), 1);
    error(1:2:2*size(X,1)) = res(:,1);
    error(2:2:2*size(X,1)) = res(:,2);
    %% Jacobian
    xdR = d(:,1)./n;
    ydR = d(:,2)./n;
    xdx = bsxfun(@plus,-R./n+(d(:,1).^2*R)./n.^3,1);
    ydy = bsxfun(@plus,-R./n+(d(:,2).^2*R)./n.^3,1);
    xdy = (d(:,1).*d(:,2)*R)./n.^3;
    ydx = xdy;

    J = zeros(2*size(X,1), 3);
    J(1:2:2*size(X,1),:) = [ xdR, xdx, xdy ];
    J(2:2:2*size(X,1),:) = [ ydR, ydx, ydy ];
end

The fitting however is not too good: if I start with the good parameter vector the algorithm terminates at the first step (so there is a local minima where it should be), but if I perturb the starting point (with a noiseless circle) the fitting stops with very large errors. I am sure that I’ve overlooked something in my implementation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:53:17+00:00Added an answer on May 28, 2026 at 4:53 pm

    For what it’s worth, I implemented these methods in MATLAB a while ago. However, I did it apparently before I knew about lsqnonlin etc, as it uses a hand-implemented regression. This is probably slow, but may help to compare with your code.

    function [x, y, r, sq_error] = circFit ( P )
    %# CIRCFIT fits a circle to a set of points using least sqaures
    %#  P is a 2 x n matrix of points to be fitted
    
    per_error = 0.1/100; % i.e. 0.1%
    
    %# initial estimates
    X  = mean(P, 2)';
    r = sqrt(mean(sum((repmat(X', [1, length(P)]) - P).^2)));
    
    v_cen2points = zeros(size(P));
    niter = 0;
    
    %# looping until convergence
    while niter < 1 || per_diff > per_error
    
        %# vector from centre to each point
        v_cen2points(1, :) = P(1, :) - X(1);
        v_cen2points(2, :) = P(2, :) - X(2);  
    
        %# distacnes from centre to each point
        centre2points = sqrt(sum(v_cen2points.^2));
    
        %# distances from edge of circle to each point
        d = centre2points - r;
    
        %# computing 3x3 jacobean matrix J, and solvign matrix eqn.
        R = (v_cen2points ./ [centre2points; centre2points])';
        J = [ -ones(length(R), 1), -R ];
        D_rXY = -J\d';
    
        %# updating centre and radius
        r_old = r;    X_old = X;
        r = r + D_rXY(1);
        X = X + D_rXY(2:3)';
    
        %# calculating maximum percentage change in values
        per_diff = max(abs( [(r_old - r) / r, (X_old - X) ./ X ])) * 100;
    
        %# prevent endless looping
        niter = niter + 1;
        if niter > 1000
            error('Convergence not met in 1000 iterations!')
        end
    end
    
    x = X(1);
    y = X(2);
    sq_error = sum(d.^2);
    

    This is then run with:

    X = [1 2 5 7 9 3];
    Y = [7 6 8 7 5 7];
    [x_centre, y_centre, r] = circFit( [X; Y] )
    

    And plotted with:

    [X, Y] = cylinder(r, 100);
    scatter(X, Y, 60, '+r'); axis equal
    hold on
    plot(X(1, :) + x_centre, Y(1, :) + y_centre, '-b', 'LineWidth', 1);
    

    Giving:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement a linear least squares fit onto 2 arrays of
I'm not currently working on a coding project that is trying to implement this,
I am very interested in at least trying to implement NAT break through for
when trying to implement an Aspect, that is responsible for catching and logging a
I am trying to implement an algorithm that takes in two ints n and
I am trying to implement share this method. I am using the code as
I am trying to implement a content formatting feature in my website - that
Sorry for the slightly rubbish title. I could not think how to describe this
I'm trying to write a user name validation that has the following restrictions: Must
I'm trying to implement some unit tests for a couple of classes that rely

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.