-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsphereFit_Prem.m
31 lines (26 loc) · 990 Bytes
/
sphereFit_Prem.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function [r,a,b,c] = sphereFit(data)
%This is a solution based on linearizing the sphere equation.
%The solution is not least-squares in its truest form, however this result
%is good enough for most purposes. This can be an input for the initual
%guess while using the Gauss-Newton or the Levenberg–Marquardt algorithm
%
% The data has to be in 3 columns and at least 4 rows, first column with Xs,
% 2nd column with Ys and 3rd columns with Zs of the sphere data.
% The output
% r = radius
% a = X coordinate of the center
% b = Y coordinate of the center
% c = Z coordinate of teh center
%
% usage: sphereFit(data) % where data is a mx3 data and m>=4
xx = data(:,1);
yy = data(:,2);
zz = data(:,3);
AA = [-2*xx, -2*yy , -2*zz , ones(size(xx))];
BB = [ -(xx.^2+yy.^2+zz.^2)];
YY = mldivide(AA,BB); %Trying to solve AA*YY = BB
a = YY(1);
b = YY(2);
c = YY(3);
D = YY(4); % D^2 = a^2 + b^2 + c^2 -r^2(where a,b,c are centers)
r = sqrt((a^2+b^2+c^2)-D);