-4

我正在尝试将其转换为 python。我真的需要 1 行的帮助。没学过matlab

function   [xc,yc,R,a] = circfit(x,y)
%
%   [xc yx R] = circfit(x,y)
%
%   fits a circle  in x,y plane in a more accurate
%   (less prone to ill condition )
%  procedure than circfit2 but using more memory
%  x,y are column vector where (x(i),y(i)) is a measured point
%
%  result is center point (yc,xc) and radius R
%  an optional output is the vector of coeficient a
% describing the circle's equation
%
%   x^2+y^2+a(1)*x+a(2)*y+a(3)=0
%
%  By:  Izhak bucher 25/oct /1991, 
    x=x(:); y=y(:);
   a=[x y ones(size(x))]\[-(x.^2+y.^2)]; # i just need help with this line
   xc = -.5*a(1);
   yc = -.5*a(2);
   R  =  sqrt((a(1)^2+a(2)^2)/4-a(3));
4

1 回答 1

3

您可能会发现此参考很有用。

  • Matlab 数组从 1 开始索引;Python 数组从 0 开始

  • Python 没有 left-matrix-div 运算符,但 numpy.linalg.solve 执行相同的操作

.

from numpy import matrix
from numpy.linalg import solve

def circfit(xs, ys):
    a = matrix([[x,y,1.] for x,y in zip(xs, ys)])
    b = matrix([[-(x*x + y*y)] for x,y in zip(xs, ys)])
    res = solve(a,b)
    xc = -0.5 * res.item(0)
    yc = -0.5 * res.item(1)
    r = (xc*xc + yc*yc - res.item(2))**0.5
    return xc,yc,r

然后

circfit([0,1,2],[2,1,2])   # ->  (1.0, 2.0, 1.0) as expected
于 2012-06-24T14:39:33.107 回答