# MATLAB Plotting Basics coving sine waves and MATLAB subplots

(Last Updated On: September 9, 2021)

## Plotting sine wave in the MATLAB:

Matlab plotting- MATLAB is the most widely used simulation software by engineers throughout the world and in this article will learn how to plot a simple sine wave which is function of time. Now you will learn how to plot a simple sine wave having different frequencies, different sampling frequencies and different time periods. So, first of all, let’s discuss the basic syntax of sine wave function in MATLAB. It is sin (2*pi*(f/fs)*n) where f is the frequency, fs is the sampling frequency and n is the time index. The value of sampling frequency must be at least two times the frequency “f” and by carefully selecting the value of “n” which is a vector of time you can plot desired number of time periods of the sine wave. So as an example I am going to plot a sine wave having frequency 50 Hertz and I select the sampling frequency of 1000 which is 20 times the frequency of the sine wave as you increase the sampling frequency you will get a clearer and crisp sine wave plot and then the next thing is the time vector. So the value of this time vector depends on how many time periods you want to plot this sine wave. In our case since the sampling frequency is twenty times the frequency of the sine wave therefore if you set a length of this time vector equal to twenty then the command which automatically plot one time period of this sine wave. So if I select the time vector of length twenty starting from 0 up to 19 and I plot this sine wave so you will see that I get a single time period of this sine wave.

Now if you want to plot two time periods of the same frequency sine wave then you just need to change the length of this time vector and if I set it 39 by changing the value of n. So the total length becomes 40 it will plot two time periods on the same sine wave every frequency of 50 Hertz.

So you can see now the frequency is the same right but we are getting two periods of this sine wave. Now for the same command if you want to change the frequency of the sine wave you just need to change the value of “f” which is currently 50. So I change it to 25 now when I change it to quantify the sampling frequency remains the same. So the sampling frequency is now 40 times the frequency of the sine wave and since the length of the time vector is equal to 40. So I will be getting a single time period. So you can see here that I am getting a single time period.

Now for example if I want to have four time periods of this particular sine wave having frequency of 25 Hertz then I need to increase my time interval four times as compared to the original one. So it must have the length of 160. so I will change the value of “n” from 0 to 39 to 0 to 159. So that the total and becomes 160 now you will see that I will get four time periods of this sine wave.

Now if you want to change the amplitude of the sine wave you will simply put a scalar value for example I want to change the amplitude from 1 to 5. So I will write 5 with the sin wave and see now that the amplitude of the sine wave becomes in the range of plus minus 5 as shown by this y-axis right.

Finally if you want to change the phase of this sine wave you will simply need to place the phase at the end of the argument. So if I want to introduce a phase of PI by two and so I will write +pi/2.

## MATLAB plotting basics:

Now, I will be telling you about some very important basics of plotting graphs in matlab. This includes how to plot a graph, modify its style plotting, multiple graphs in single figure and adding axis labels and figure titles. First of all, as I have showed you how to plot a sine wave as discussed in the topic above. I have already explained the detailed method to plot a sine wave here i will be quickly plotting the sine wave and then modify its style. So I will write the command for plotting the sine wave which is:

The following command will plot the sine wave like this:

Let me maximize it now i will be showing you how to modify the style of this particular graph to do this first of all select the arrow to edit the plot.

Now click on the graph and now right-click on this graph to go to the color if you want to change the color of this graph you can do so by selecting the color of your choice.

For example, I am going to select the green color from here.

Now click on Ok and the color of the wave will be change.

So it will change the color of this graph or you can change the color by your choice.

Now if you want to place markers on this particular graph again right click and go to the marker and there are many choices available in this menu and you can select any marker of your choice for example I am selecting this square marker.

The wave will be changed to:

If you want to change the size of this marker again right click and go here into the marker size option here many options are available starting from 2 to 48.

So if i select 10 from here you can see that the size of the marker is increased.

Now if i want to change the width of this line then again right click and go to the line width option and from here you can select the line width of your choice for example if i select 2 from here.

You can see that the graph width is now increased.

Another option is you can also change the line style again right click and go to the line style and select the option of your choice. There are four options available here:

for example, i select dash from here so you can see that the solid line is replaced with a dashed line.

Similarly, you can also place a grid in this particular graph to do this simply right-click anywhere in this figure and click on this grid option.

You can see that the grid is now visible.

Now i will be telling you how to include the x and y-axis labels and figure titles to do this go to the insert option and select the title from this menu and write the title of your choice.

For example, I am writing this is a sine wave the title will appear on the wave.

Now if you want to insert the x and y-axis labels again go to insert and click x label. So here i can write the x label of my choice. For example, i write on the wave time as x label:

and to place the y label again go to insert and click y label and write here the label of your choice i am writing here sine wave.

One very important thing that i want to mention here that some people are worried about the fact if they want to write the x and y labels in the form of subscript or superscript. They find it very difficult but it is not actually very difficult it is very easy. For example if i want to change the y label and i want to write s subscript one. So i will simply write s and place underline and then write one.

Click on the graph anywhere so you can see that s subscript 1 is written over here at the Y axis.

Similarly if you want to write superscript you simply place this sign “^” the power sign and write one.

Just click anywhere on the graph and this will result in superscript in the y label this method can be used in x label y label and title as well.

and also it can be used in the legend.

Now i will be telling you how to plot multiple graphs in this same figure. So let us plot another graph in the same figure. So go to the command window again and recall the previous command and just replace the x sign with y because we want  to generate a new plot and let us rename the variable as y.

y=sin(2*pi*(f/fs)*n);

Now to plot y in the same figure the command is hold on so I am writing here hold on and press enter.

Now if i run the command plot y and enter you can see that the new plot is plotted in the same figure and the color is automatically changed.

So in the newer versions of MATLAB automatically changes the color of the other graph if you want to change the color by selecting the color of your choice you can always do this by using the same procedure as i have already told you. Whenever you plot multiple graphs it is almost necessary to include a legend to show the type of each graph. So to do this we go over here on the left side of the arrow here is the option to insert legend click this option

and you can see that the legend is appearing over here you can drag this legend to any place inside this figure and also you can modify the contents of this legend for example double click the first one because the first one was sine wave so I am writing here sine and then click on the outer side and then click double click on the second option and write here.

You can modify the legend as well and whenever you change the color or any characteristic of this plot the legend will be automatically updated for example if i change the color of the second graph from red to let’s say green.

So you can see that the color in the legend is automatically changed.

So this is a brief overview of how to plot multiple graphs in a single figure how to modify the styles of these graphs and how to insert x and y labels and figure titles.

## Subplots in MATLAB:

Now to draw different waves on same graph separately we will use subplot commands. For example we have two wave and we want to draw it on the same graph then we will use the following command:

In the commond subplot(2,1,1) 2 shows the number of rows in graph, 1 shows the number of columns and other 1 show the number of waveform you want to plot. For example if we want to 3 waveform in three rows then we write the command subplot(3,1,1).

So by using this subplot command we can draw various types of waves on the same figure.