最小二乘圆拟合circfit m sci 代码

 

//m文件 sci文件


function [xc,yc,R,a] = circfit(x,y)
//CIRCFIT Fits a circle in x,y plane
// [XC, YC, R, A] = CIRCFIT(X,Y)
// Result is center point (yc,xc) and radius R.A is an

// optional output describing the circle's equation:
// x^2+y^2+a(1)*x+a(2)*y+a(3)=0

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));
endfunction

 

调用示例:

N=5;

t=0.01:1/N:1;

x=1.6*sin(t*2*3.14159);

y=1.4*cos(t*2*3.14159);

[xc,yc,R,a]=circfit(x,y)

 

posted @ 2015-03-22 12:32  JkReader  阅读(405)  评论(0编辑  收藏  举报