MATLAB Answers

Fitting Data to Circle (Data is only a small cutout of a circle)

7 views (last 30 days)
PAMASA456
PAMASA456 on 11 Nov 2020
Commented: PAMASA456 on 11 Nov 2020
Hi guys,
I'm currently trying to fit data to a circle, the problem is that the data is only s really small cutout of a circle with much noise. So when I try to fit the data to the circle it's really badly fitted. I use a function which I found here https://blogs.mathworks.com/pick/2008/03/14/fitting-a-circle-easily/
I don't know what to do to fit the data in a better way, maybe some of you could help me
[xfit,yfit,Rfit,a] = circfit(x,y);
ang=0:0.01:2*pi;
xp=Rfit*cos(ang);
yp=Rfit*sin(ang);
plot(xfit+xp,yfit+yp,'m.',xfit,yfit,'m.',x,y,'g.');

  0 Comments

Sign in to comment.

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 11 Nov 2020
For this case I think you'll have to put in some work. The radius of your circle is clearly very large if we're to judge from the curvature of your points. I'd go about this in these steps.
1, write a circle-fitting error-function, something like this:
function err = circ_fit_err(pars,r)
phi = atan2(r(:,2),r(:,1));
r0 = pars(1:2);
radius = pars(3);
err = sum((r(:,1)-r0(1)-radius*cos(phi)).^2+(r(:,2)-r0(2)-radius*sin(phi)).^2);
end
2, then I'd make sure that I have fminsearchbnd from the file exchange for constrained optimization for the fit (keeping the radius larger than some lower limit.)
3, rotate and shift the points such that a 2nd degree polynomial fit have the constant linear coefficient as close to zero as posible - meaning that the circle-centre should be along (or at least very close to) the y-axis.
4, this should also allow you to figure out a good start-guess for the radius.
5 the search for the circle-centre and the radius should now be much easier, you know the reasonable range of the radius, and the x-component of the circle-centre should be small so can be constrained.
HTH

  3 Comments

PAMASA456
PAMASA456 on 11 Nov 2020
First, thank you for your answer. I tried to understand what you want to do but unfornately I do not. I just begun to program with matlab.
What is the error function doing? And what is r and pars?
Also I don't understand what the fminsearchbnd should do?
Bjorn Gustavsson
Bjorn Gustavsson on 11 Nov 2020
As a task for a matlab-beginner I'd classify this as "rather challenging". Have you gone through the initial introduction-material (should be available here: on-ramp "courses"), otherwise I strongly encourage you to do so - that will bring you up to speed more efficiently than anything else, then browse through the matlab demos by typing demo at the command-line prompt.
The error-function is intended to give a sum-of-squared-of-the-residuals between your points and the circle-coordinates for a given set of [x0, y0, radius], such a function you can use to find the best circle-parameters by minimizing it with some of matlab's optimization functions (like fminsearch).
r is your points, I combined them into a single [n_points x 2] array instead of having one array for x and one for y. pars are the parameters of your circle, that is [x0, y0, radius] - the fminsearch function can do minimization of functions that depend on multiple variables (that are sent into the function as one array, in this case [x0, y0, radius]). The fminsearchbnd is supposed to minimize the error-function I suggested.
Welcome to matlab.
HTH
PAMASA456
PAMASA456 on 11 Nov 2020
Thank you for your patience! I will have a look at the initial introduction materials and will try to understand your explanations again

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!