# Draw the sphere to find the radius of the sphere

6 views (last 30 days)

Show older comments

### Accepted Answer

John D'Errico
on 3 Feb 2023

Estimation of the sphere parameters is not too difficult. First, since you have shown no data, I'll make some up.

XYZ = randn(50,3); XYZ = XYZ./sqrt(sum(XYZ.^2,2));

XYZ = 2.5*XYZ + [1 3 5] + randn(size(XYZ))/20;

plot3(XYZ(:,1),XYZ(:,2),XYZ(:,3),'o')

axis equal

grid on

box on

xlabel 'X'

ylabel 'Y'

zlabel 'Z'

The result should be a sphere of radius 2.5, centered at the point [1 3 5].

You can use the tool I attached called spherefit.

[C,R] = spherefit(XYZ)

As you can see, the code recovered the original parameters well enough. Now, to plot the sphere...

fimplicit3(@(x,y,z) (x - C(1)).^2 + (y - C(2)).^2 + (z - C(3)).^2 - R.^2)

hold on

plot3(XYZ(:,1),XYZ(:,2),XYZ(:,3),'ro')

axis equal

grid on

box on

xlabel 'X'

ylabel 'Y'

zlabel 'Z'

hold off

I could have built a surface directly, perhaps using meshgrid. But fimplicit3 is just too easy.

##### 3 Comments

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!