Question #5837
Submitted by

**Answiki**on 01/30/2022 at 03:54:13 PM UTC# How to write a Matlab function that draw a sphere?

Merged questions

Answer
Submitted by

**Answiki**on 01/30/2022 at 03:58:12 PM UTCThe following Matlab function draws a sphere in the current figure:

```
function [X,Y,Z] = drawSphere(center, radius)
%drawSphere Draw a sphere
% center are the coordinates of the center [x,y,z]
% radius is the radius of the sphere
% The function returns the points of the sphere
[X,Y,Z] = sphere;
X = center(1) + X*radius
Y = center(2) + Y*radius
Z = center(3) + Z*radius
surf(X,Y,Z);
end
```

Here is an example of the use of the function:

```
drawSphere ([1,2,3],3);
axis square equal;
```

The previous script displays the following figure: