Matlab

MATLAB Plotting Basics coving sine waves and MATLAB subplots

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.

n=[0:19];

f=50;

fs=1000

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

plot(n,x)

Matlab plotting

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.

n=[0:39];

f=50;

fs=1000

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

plot(n,x)

Matlab plotting

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.

n=[0:39];

f=25;

fs=1000

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

plot(n,x)

 

Matlab plotting


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.

n=[0:159];

f=25;

fs=1000

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

plot(n,x)

 

Matlab plotting

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.

n=[0:159];

f=25;

fs=1000

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

plot(n,x)

 

Matlab plotting



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.

n=[0:39];

f=50;

fs=1000

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

plot(n,x)

 

Matlab plotting

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:

n=[0:19];

f=50;

fs=1000

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

plot(n,x)

The following command will plot the sine wave like this:

Matlab plotting

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.

Matlab plotting


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.

Matlab plotting

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

Matlab plotting

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

Matlab plotting

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.

Matlab plotting


The wave will be changed to:

Matlab plotting

 

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.

Matlab plotting

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

Matlab plotting

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.

Matlab plotting

You can see that the graph width is now increased.

Matlab plotting

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:

Matlab plotting


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

Matlab plotting

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.

Matlab plotting

You can see that the grid is now visible.

Matlab plotting

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.

Matlab plotting



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

Matlab plotting

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:

Matlab plotting

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.

Matlab plotting

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.

Matlab plotting



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

Matlab plotting

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

Matlab plotting

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.

Matlab plotting

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.

n=[0:39];

f=50;

fs=1000

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

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

hold on

plot(y)

Matlab plotting

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

Matlab plotting

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.

Matlab plotting

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.

Matlab plotting

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

Matlab plotting

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:

n=[0:39];

f=50;

fs=1000

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

subplot(2,1,1)

plot(n,x)

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

subplot(2,1,2)

plot(n,y)

 

Matlab plotting

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).

n=[0:39];

f=50;

fs=1000

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

subplot(3,1,1)

plot(n,x)

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

subplot(3,1,2)

plot(n,y)

z=sin(2*pi*(f/fs)*n+pi/4);

subplot(3,1,3)

plot(n,z)

 

Matlab plotting

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

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button