Introduction to Matlab sphere()
MATLAB incorporates the function sphere() which can be used to create a 3-D sphere. Since the function deals with a 3D graph, the output from the function is obtained with 3-axes co-ordinates.
The function sphere() in MATLAB, can be implemented in the code using any of the syntaxes described below. The syntaxes are developed based on input arguments and output arguments used to use the function.
|sphere||Creates a unit sphere i.e. a sphere with a radius of value 1.|
|[X,Y,Z]=sphere||This syntax does not plot the graph rather it returns the x,y, and z coordinates of the sphere in the form of 21X21 matrices.|
|[X,Y,Z]=sphere(n)||This syntax does not plot the graph rather it returns the x,y and z coordinates of the sphere in the form of (n+1)X(n+1) matrices.
Where n represents the number of faces, consisted of the sphere.
|Sphere(—-)||Creates a unit sphere i.e. a sphere with being customized with the values given as input arguments in the function.|
|Sphere(ax,—-)||Creates a unit sphere i.e. a sphere with being customized with the values given as input arguments in the function with the given new co-ordinates ax.|
Examples of Matlab sphere()
Following are the examples are given below:
The above code results in creating a unit plot with a uniform axis setting.
The attributes that are supported by sphere()
There are two input arguments that can be used while implementing the function sphere.
|Number of Faces (n)||Used to decide the number of faces to use with the sphere plot|
|Target axes (ax)||Used to define new axes object|
Usage of different syntax, attributes, and resultant behavior from the function call is described in the examples defined below:
Using the syntax sphere
In the below code the 3D sphere plotting function is called using the syntax ‘sphere’.
The command generates a unit sphere i.e. a sphere with a radius of value ‘1’.
Using the syntax [X, Y, Z] =sphere
The below code snippet generate x, y, z coordinates in order to generate the sphere plot using the functions ‘surf’ or ‘mesh’.
[P,Q,R] = sphere;
The above command has generated p, q, and r-coordinates and is given as the value for the input argument, the center of the graph, to the function surf().
Using the syntax [X,Y, Z] =sphere(n)
The below code snippet is defined to generate p, q, and r- coordinates using the function sphere with the value of n=25.
[P,Q,R] = sphere(25);
The value for the attribute n i.e. number of faces is set as 25 in the code. Hence it results in creating sphere with unit radius and 26-by-26 i.e. ((n+1)-by-(n+1)) faces.
Using the syntax Sphere(—-)
The below code snippet generates a sphere with a unit radius with value for origin as an input argument (n=30) to the function sphere.
The above command generates a unit sphere with 31-by-31i.e. ((n+1)-by-(n+1)) faces.
Using the syntax Sphere(ax,—-)
The below code snippet generates a sphere with unit radius with value for origin and user-defined axes ‘px’ as an input argument to the function sphere.
The plot from the above command consists of a unit sphere in one of the cells of the 2X1 frame with a new axis ‘px’.
Creating a Sphere with Different Measurements for Radius and Origin Apart from Unity Value:
The 3D sphere plotting function sphere can be used to generate a sphere having a radius of value more than one and shifted origin measure.
The code snippet defined below generate a plot for the sphere with a radius of 30000 and origin at (30000,30000,30000)
[p,q,r] = sphere;
p = p*30000;
q = q*30000;
r = r*30000;
The MATLAB code defined below to generate two spheres in the same plane having two different radius and origin.
[P,Q,R] = sphere;
rad = 5;
P2 = P * rad;
Q2 = Q * rad;
R2 = R * rad;
The resultant plot contains a unit sphere having the origin at (0,0,0) and another sphere having radius of size 5 and origin at (5, -5,0).
Creating Multiple Spheres in Different Cells of A Single Layout
MATLAB supports creating a multiple numbers of 3D sphere plots single layout, representing each graph in a different cells of it.
The MATLAB code generates 4 different spheres displaying them in 4 segments of a single layout.
px1 = nexttile;
title('With default number of faces 20X20')
px2 = nexttile;
title('With number of faces 40X40')
px3 = nexttile;
title('With number of faces 60X60')
px4 = nexttile;
title('With number of faces 80X80')
Setting Radius for Multiple Spheres from An Array/matrix Input
As we have discussed earlier, the function ‘sphere’ can also be used to create multiple spheres in a single layout. The radius and origin can be set differently for each of those spheres by declaring the radius values as an array or matrix.
The below code snippet is designed to create 3 different spheres with 3 different radius and origin defined an array.
[p q r] = sphere;
nx=[3 3 3 3;-3 -3 -3 3;0 4 -2 2];
Creating Transparent Sphere
The sphere generated from the MATLAB function ‘sphere’ can be made transparent by regulating the attribute value FaceAlpha.
The below example demonstrates the impact of the variations in the value of the attribute FaceAlphaon the transparency of a unit sphere that is created by the function ‘sphere’.
Case 1: With FaceAlpha = 0.5
[x,y,z] = sphere;
Case 2: With FaceAlpha = 0.2
[x,y,z] = sphere;
Thus MATLAB programming is supported by creating 3D sphere plots using the function sphere. The function also provides flexibility to customize the display of the plots during the creation as well as editing the display after the plot is created.
This is a guide to Matlab sphere(). Here we also discuss the Introduction and syntax of matlab sphere() along with different examples and code implementation. You may also have a look at the following articles to learn more –
- Matlab Plot Circle
- Matlab stem()
- MATLAB Derivative of Function
- Histogram in Matlab
- MATLAB Indexing | Top 5 Examples
- Matlab Figure | Examples