Friday, December 20, 2024

Plotting with MATLAB

MATLAB is a useful software tool for engineering and computational mathematics. It also plays a very important role in scientific and engineering plots. This project deals with 2D and 3D graphics plotting in MATLAB. It also explains the audio signal manipulations using plotting in MATLAB.

Familiarisation with basic MATLAB objects like vectors and matrices will help in understanding the subject better before delving into details. MATLAB has an excellent set of graphic tools. Plotting the given data set or the results of computation is possible using very few commands.

C2F_Fig_1
Fig. 1: Output of plot ( ) function
EB8_Fig_2
Fig. 2: Time-distance plot
C82_Z88_Fig_3
Fig. 3: Plotting of sine ( ) function
B8Z_Fig_4
Fig. 4: Red colour sine wave plot
E12_Fig_5
Fig. 5: Sine wave plot in green and red colours
5AD_Fig_6
Fig. 6: The subplot ( ) function output

Plotting in MATLAB
2D plotting. To use the ‘plot’ function in MATLAB, you should first make sure that the matrices/vectors you are trying to use are of equal dimensions. Here we have taken a simple plotting example using the plot ( ) function as given below (its output plot is shown in Fig. 1):

- Advertisement -

[stextbox id=”grey”]x = [3 9 27];
t = [1 2 3];
plot (t,x);[/stextbox]

You can also label the axes of the plot in Fig. 1 by using commands as given below. The plot becomes a time-distance plot as shown in Fig. 2.

- Advertisement -

[stextbox id=”grey”]xlabel(‘Time(s)’); % label
for x axis
ylabel(‘Distance(m)’); %
label for y axis
title(‘Plot of Distance over
Time’); % giving the title
legend(‘Trial 1’);
legend(‘Location’,
’Northwest’); % move legend
to desired location[/stextbox]

Now, take an example of plotting a sine function using the following commands (its output is shown in Fig. 3):

[stextbox id=”grey”]x = 0: pi/100: pi*2;
y = sin(x);
plot(x, y);[/stextbox]

Now, if you want to change the colour of line or pattern, use the command:

[stextbox id=”grey”]plot(x,y,’r:’,’Linewidth’,4);[/stextbox]

Here ‘r’ is the colour of the line. The ‘:’ (colon) is used for the line pattern and ‘4’ is the line width. Its output is as shown in Fig. 4. If you want to know more about the colours and patterns of line in MATLAB, type ‘help plot’ in command window and you will see the entire list.

To plot another graph with green colour on the same screen, we use ‘hold’ command as given below (its output is shown in Fig. 5):

[stextbox id=”grey”]x = 0: pi/100: pi*2;
y = sin(x);
z = cos(x);
plot(x, y, ‘r’);
hold on
plot(x, z, ‘g’);[/stextbox]

The subplot ( ) function divides the current figure into rectangular panes that are numbered row-wise. An example for ‘subplot’ command is given below (its output is shown in Fig. 6):

[stextbox id=”grey”]

x = 0: pi/100: pi*2;
y = sin(x);
z = cos(x);
t = exp(-x);
u = (x.^3);
subplot(2,2,1);
plot(x,y);
subplot(2,2,2);
plot(x,z);
subplot(2,2,3);
plot(x,t);
subplot(2,2,4);
plot(x,u); [/stextbox]

An example of the polar command is given below (its output is shown in Fig. 7):

x = 0:0.2:5*pi;
y = x.^2;
polar(x, y,’*’);

Similarly, we also have other commands for 2D graphics like bar, stairs, loglog and semilogy.

4 COMMENTS

SHARE YOUR THOUGHTS & COMMENTS

EFY Prime

Unique DIY Projects

Truly Innovative Electronics

Electronics News

Latest DIY Videos

Electronics Components

Electronics Jobs

Calculators For Electronics