The subplot command is as follows: `subplot(m,n,p)`

where the parameters m and n are the dimensions of an m-by-n matrix of small subplots and where p represents the number of the subplot for the current plot. This may be hard to understand at first, but let’s look at an example to make things simpler.

Say you wanted to plot the sine and cosine curves into one figure, you’d use the following commands:

x = 0:pi/20:2*pi; subplot(2,1,1); plot(sin(x)) subplot(2,1,2); plot(cos(x))

The output would be **Figure 3.6**.

Let’s look at what happens when we change the m-parameter (of 2) to 3. What happens? Well, you just end up with the same figure, except with some unutilised space at the bottom. If you changed the n-parameters (of 1) to 2 or 3, then the locations of your plots change accordingly. This is best demonstrated by means of an exercise.

**Worked Exercise 3.1**

We will be looking at some trig functions and some of their transformations. Don’t worry if you’ve forgotten some of these, we won’t be doing anything technical; we’re just looking at how the subplot function in MATLAB operates!

Let’s consider what happens when you inverse the sine, cosine and tangent curves. We would like to see the results graphically, and we would like them to be all output to one figure. We would also like the layout to be such that you have the original curves in the first column, and so that you have the transformed ones in the second column. How do we go about doing this?

Well, let’s set a range of x values that we would like to consider first. Let’s use the range: -π ≤ x ≤ π . To do this, we’d use the command:

x = -pi:pi/100:pi;

Remember, we need to use a small enough spacing for the best approximation.

Let’s also say that we want the sine curve on the first row, the cosine curve on the second row and the tangent curve on the third row.

Let’s first consider how many plots we will have in the figure. Since we will have three graphs, along with each of their transformations, then we will have six plots. The arrangement of these six plots will be like a 2×3 matrix, as shown below:

Therefore, if we again consider the command `subplot(m,n,p)`

, we can see that m is 3, n is 2 because there are two columns, and that p will vary depending on which plot we are considering (either one or two). MATLAB indexes the value of p row by row, from left to right. Therefore, sin(x)’s p value is 1, its inverse would be 2, and so on.

Let’s first look at the sine curve. Its position as shown by the above matrix will correspond to a p value of 1. After setting the x-range as shown above, we would use the following command to insert the sine curve into the designated area in the figure:

subplot(3,2,1); plot(x,sin(x));

This would output a figure with one of six available areas utilised, and the rest empty, as shown in **Figure 3.7**.

What about the sine-inverse function? How can we get it to show up in the 2/6 spot of **Figure 3.7**? To do that, we’d use a similar command but we’ll change the p parameter to 2 this time.

```
subplot(3,2,2); plot(x,asin(x));
xlabel('-\pi < x < \pi');
```

Note: to find the sine-inverse of a set of x values (the range specified above), we need to use the asin(x) function. The same goes to cos and tan. The figure then updates to the **Figure 3.8**.