Question #5836   Submitted by Answiki on 01/30/2022 at 03:36:18 PM UTC

How to plot a sphere in Matlab ?

Answer   Submitted by Answiki on 01/30/2022 at 03:51:53 PM UTC

In Matlab, there is no function to draw a sphere. Therefore, the sphere keyword returns the—ready-to-draw—coordinates of a sphere. The coordinates can be plotted with the surf function:

[X,Y,Z] = sphere;
surf(X,Y,Z);
axis square equal;


To specify the center and the radius of the sphere, use the following code :

[X,Y,Z] = sphere;
radius = 3;
center = [4,3,2]
X = center(1) + X*radius
Y = center(2) + Y*radius
Z = center(3) + Z*radius
surf(X,Y,Z);
axis square equal;