15 lines
475 B
Mathematica
15 lines
475 B
Mathematica
|
function [c,r] = fitcircle2(x,y)
|
||
|
% Function to plot a linear least squares circle on a data set
|
||
|
% x = vector of x-coordinates
|
||
|
% y = vector of y-coordinates
|
||
|
% c = center of the circle
|
||
|
% r = radius of circle (not relevant)
|
||
|
n=length(x); xx=x.*x; yy=y.*y; xy=x.*y;
|
||
|
A=[sum(x) sum(y) n;sum(xy) sum(yy) sum(y);sum(xx) sum(xy) sum(x)];
|
||
|
B=[-sum(xx+yy) ; -sum(xx.*y+yy.*y) ; -sum(xx.*x+xy.*y)];
|
||
|
a=A\B;
|
||
|
xc = -.5*a(1);
|
||
|
yc = -.5*a(2);
|
||
|
r = sqrt((a(1)^2+a(2)^2)/4-a(3));
|
||
|
c = [xc;yc];
|
||
|
end
|