# MATLAB Language Drawing Pseudo 4D plot

## Example

A `(m x n)` matrix can be representes by a surface by using surf;

The color of the surface is automatically set as function of the values in the `(m x n)` matrix. If the colormap is not specified, the default one is applied.

A colorbar can be added to display the current colormap and indicate the mapping of data values into the colormap.

In the following example, the `z (m x n)` matrix is generated by the function:

``````z=x.*y.*sin(x).*cos(y);
``````

over the interval `[-pi,pi]`. The `x` and `y` values can be generated using the meshgrid function and the surface is rendered as follows:

``````% Create a Figure
figure
% Generate the `x` and `y` values in the interval `[-pi,pi]`
[x,y] = meshgrid([-pi:.2:pi],[-pi:.2:pi]);
% Evaluate the function over the selected interval
z=x.*y.*sin(x).*cos(y);
% Use surf to plot the surface
S=surf(x,y,z);
xlabel('X Axis');
ylabel('Y Axis');
zlabel('Z Axis');
grid minor
colormap('hot')
colorbar
`````` Figure 1

Now it could be the case that additional information are linked to the values of the `z` matrix and they are store in another `(m x n)` matrix

It is possible to add these additional information on the plot by modifying the way the surface is colored.

This will allows having kinda of 4D plot: to the 3D representation of the surface generated by the first `(m x n)` matrix, the fourth dimension will be represented by the data contained in the second `(m x n)` matrix.

It is possible to create such a plot by calling `surf` with 4 input:

``````surf(x,y,z,C)
``````

where the `C` parameter is the second matrix (which has to be of the same size of `z`) and is used to define the color of the surface.

In the following example, the `C` matrix is generated by the function:

``````C=10*sin(0.5*(x.^2.+y.^2))*33;
``````

over the interval `[-pi,pi]`

The surface generated by `C` is Figure 2

Now we can call `surf` with four input:

``````figure
surf(x,y,z,C)
xlabel('X Axis');
ylabel('Y Axis');
zlabel('Z Axis');
grid minor
colormap('hot')
colorbar
`````` Figure 3

Comparing Figure 1 and Figure 3, we can notice that:

• the shape of the surface corresponds to the `z` values (the first `(m x n)` matrix)
• the colour of the surface (and its range, given by the colorbar) corresponds to the `C` values (the first `(m x n)` matrix) Figure 4

Of course, it is possible to swap `z` and `C` in the plot to have the shape of the surface given by the `C` matrix and the color given by the `z` matrix:

``````figure
surf(x,y,C,z)
xlabel('X Axis');
ylabel('Y Axis');
zlabel('Z Axis');
grid minor
colormap('hot')
colorbar
``````

and to compare Figure 2 with Figure 4  PDF - Download MATLAB Language for free