球体适合给定半径,matlab

时间:2015-01-15 09:06:35

标签: matlab

我希望有一个matlab函数来拟合一个X = [x,y,z]的球体。

半径是固定的/已知的。它应该作为输入而不是再次估计。半径的估计会导致额外的误差源。 我发现了两个有用的链接

http://www.mathworks.com/matlabcentral/fileexchange/45356-fitting-quadratic-curves-and-surfaces/content/spherefit.m

http://de.mathworks.com/matlabcentral/fileexchange/34129-sphere-fit--least-squared-

我可以通过使用这两个函数来拟合球体,但是如何修正我的半径而不是估计。

------------------------------------- EDITED --------- ---------------------------------------

我试过

POINTS = [...
            -1.6510    0.4190    0.1580
   -1.6410    0.4390    0.1770
   -1.6620    0.4380    0.1320
   -1.6610    0.3990    0.1980
   -1.6510    0.4180    0.2090
   -1.6460    0.4470    0.2130
   -1.6450    0.4730    0.1950
   -1.6540    0.4720    0.1480
   -1.6730    0.3960    0.1530
   -1.6790    0.3850    0.1770
   -1.6450    0.4220    0.1850
   -1.6470    0.4360    0.1540
   -1.6570    0.4600    0.1370
   -1.6770    0.4160    0.1300
   -1.6630    0.4280    0.2340
   -1.6690    0.4070    0.2250
   -1.6550    0.4540    0.2270
   -1.6430    0.4690    0.1740
   -1.6410    0.4400    0.1930
   -1.6560    0.4030    0.1880
   -1.6600    0.3990    0.1720
   -1.6400    0.4530    0.1810];

Radius_act= 0.0725;
Center_act = [-1.712637 0.448658 0.183808 ];

XYZ = bsxfun(@plus,Radius_act*POINTS, Center_act);

f = @(C)sum((sum(bsxfun(@ minus,XYZ,C)。^ 2,2)-Radius_act ^ 2)。^ 2);

estimateCenter = fminsearch(f, [0,0,0])


[Center_LSE,Radius_LSE] = sphereFit(M)

但为什么它不同?????????????

estimateCenter =   -1.7640    0.4576    0.1906


Center_LSE =   -1.7122    0.4485    0.1838


Radius_LSE =    0.0725

1 个答案:

答案 0 :(得分:1)

首次尝试时,您可以最大限度地减少来自FileExchange的 Sphere Fit 使用的相同异议功能:Sum((x-xc)^2+(y-yc)^2+(z-zc)^2-r^2)^2。 [至少它在文档中的含义......如果这是正确的选择,我不确定,但我们现在相信这个来源。]

让我们只使用最小化工具箱中的自动功能fminsearch

f = @(C) sum((sum(bsxfun(@minus,XYZ,C).^2,2)-r^2).^2);
estimateCenter = fminsearch(f, [0,0,0]);

对于示例数据:

r = 3;
realCenter = [10,20,40];
[X,Y,Z] = sphere(10);
XYZ = bsxfun(@plus,r*[X(:),Y(:),Z(:)], realCenter);

这将得到我们:

estimateCenter =
   10.0000   19.9999   40.0000

如果您想要更好的近似值,可以使用fminsearch(..., optimset('TolX',1e-4))设置梦想的容错率。