Slice; Vector Field Plots

In today’s lesson, we’re going to be covering the following topics:

1. Slice Function

2. Vector Field Plots

video coming soon

Slice Function

The built-in MATLAB function slice allows you to slice through the x, y, or z plane of a 3D shape. The slice can be orthogonal (perpendicular) to the specified plane, and there can be multiple slices along this plane. If you don’t want the slice to be along one specific plane (x, y, or z) but rather a surface (xz, yz, xy), MATLAB can do this as well.

Here’s an example of slicing a 3D shape orthogonal to all three axes x, y, and z:

Let’s go through each line:

(1) [x,y,z] = meshgrid(-5:.5:5,-5:.5:5,-5:.5:5);

You need to use meshgrid to create a 3D structure first. It will take the input vectors and turn them into a 3D shape, so in the brackets next to meshgrid you need to identify the vectors for each axis. I have used the colon operator to identify the axis for all of x, y, and z as being -5: .5 : 5. This means each axis goes from -5 to 5 in steps of 0.5, and the axis will count up like -5, -4.5, -4, -3.5, -3, -2.5, -2, -1.5, -1, -0.5, 0, 0.5, 1 …. 5. You need to set meshgrid equal to [x, y, z] or whatever three variables you’d like to call your axes.

(2) v = exp(-y.*z.^2-x.^3);

Here you will identify the equation of the shape being plotted. It will be plotted along the axes you specify in line (1).

(3) xslice = 1; yslice = 2; zslice = -2;

Here you will identify which point along which axis needs to be sliced. I have identified x = 1, y = 2, and z = -2. If you only want to slice along specific axes, set the other ones equal to 0. For example, if I only want to cut along x = 1, then I set y = 0 and z = 0.

If you want there to be multiple slices along one specific axis, just identify them in the same variable. For example, if I wanted to slice along the x-axis at the values 1, 4, 5, then I would specify x = [1 4 5] rather than x = 1 like I did for this question.

(4) slice(x,y,z,v,xslice,yslice,zslice)

Here you will use all the variables you named in the lines above, and input them in this order to the built-in function slice. You will use x,y,z from line (1), v from line (2), and xslice, yslice, and zslice from line (3).

This is what the output looks like:

Vector Field Plots

A Vector Field Plot is a collection of arrows, kind of like this:

This image has an empty alt attribute; its file name is Screen-Shot-2020-08-07-at-10.01.53-PM.png

What does it mean?

Used in higher level mathematics and physics, a Vector Field Plot shows the direction of flow for a specific situation. Recall that vectors are shown as arrows pointing in a specific direction, and when you want to visualize the direction of electric fields, magnetic fields, fluid flow, or other phenomenons in math and physics, you can use these plots to show how each individual vector within the entire space is moving. Then the entire collection of vectors for that space becomes a Vector Field Plot. You may hear of this being called Quiver Plots in MATLAB as well.

How do we get MATLAB to create this for us? There are 3 steps to this. I have created an example code here, and we will go through each of the steps using this example:

This image has an empty alt attribute; its file name is Screen-Shot-2020-08-07-at-10.13.02-PM.png

(1) [X,Y] = meshgrid(0:0.5:5, 0:0.5:5)

Within the brackets after meshgrid, you will define the points of the variables X and Y (or whatever names you gave the variables on the left side of the equation). MATLAB uses these points to plot vectors (arrows) at each of these coordinates X,Y. You will need to make sure you have the same number of points in both X and Y.

Note: Meshgrid is a built-in function that creates multi-dimensional matrices from the input vectors, by repeating them over and over to create a matrix. Click here if you missed the lesson on meshgrid.

I used the colon operator to create spaced out points 0:0.5:5. Spacing using the colon operator was discussed in a previous lesson, but it works using first number:spacing:last number. So with 0:0.5:5 it indicated to MATLAB that I wanted the variable X and Y to go from 0 to 5 in steps of 0.5. This creates a vector of numbers which will look like [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5].

(2) A = 2*sin(X).*Y; and B = 2*cos(X).*Y;

These two equations will create the X and Y components of the vectors that you are plotting at the coordinates you describe in part (1). I have named my components A, B rather than X, Y to avoid confusion with the X, Y coordinates from part (1).

(3) quiver(X,Y,A,B)

This line uses the built-in function quiver to create the vector field plot using the inputs X, Y, A and B. These variables must be the 4 variables you created earlier. I had to use X and Y from the first line of code, because they signify the coordinates of the arrows, then A and B from the second and third lines because they signify the X and Y components of the vectors being plotted.

Running the code I showed above outputs this graph:

This image has an empty alt attribute; its file name is Screen-Shot-2020-08-07-at-10.11.42-PM.png