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

circlecurve fitting

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.');

Best Answer

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