0% found this document useful (0 votes)
4 views

Engineering Workshop Lab3

Uploaded by

almautulamreeka
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Engineering Workshop Lab3

Uploaded by

almautulamreeka
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

ENGINEERING WORKSHOP

(EL-1006)

LABORATORY MANUAL

INTRODUCTION TO MATLAB III


(LAB # 03)
ENGR. Shehzad Ahmad

ENGR. Tooba

Student Name: ______________________________________________


Roll No: ________________ Section: ____
Date performed: _____________, 2025Thank you! I appreciate the opportunity and look forward to
contributing.

MARKS AWARDED: ________ / 10

____________________________________________________________________________________________________________________________________________________________

NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES, ISLAMABAD

Prepared by: Engr. Tooba Version: 2.00


Last Edited by: Engr. Shehzad Ahmad
Verified by: Updated: Jan 10, 2025
Introduction to MATLAB IIl LAB 3

Lab # 0#: Introduction to MATLAB III


PLOTTING:
Objective: In this Matlab lab, you will learn:
 Two dimensional plot
 Plot formatting
 Displaying multiple plots

Tools Used: MATLAB

PLOTTING

TWO-DIMENSIONAL plot() COMMAND

plot (x,y)

 where x is a vector (one dimensional array), and y is a vector. Both vectors must have the
same number of elements.

 The plot command creates a single curve with the x values on the abscissa (horizontal axis)
and the y values on the ordinate (vertical axis).

 The curve is made from segments of lines that connect the points that are defined by the x
and y coordinates of the elements in the two vectors.

PLOT OF GIVEN DATA:

x 1 2 3 5 7 7.5 8 10

y 2 6.5 7 7 5.5 4 6 8

 A plot can be created by the commands shown below. This can be done in the Command
Window, or by writing and then running a script file.

>> x=[1 2 3 5 7 7.5 8 10];


>> y=[2 6.5 7 7 5.5 4 6 8];
>> plot(x,y)

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 2 of 13


Introduction to MATLAB IIl LAB 3
 Once the plot command is executed, the Figure Window opens with the following plot as
shown in “Fig 2.3”

“Fig 2.3”

LINE SPECIFIERS IN THE plot() COMMAND:

Line specifiers can be added in the plot command to:


 Specify the style of the line.
 Specify the color of the line.
 Specify the type of the markers (if markers are desired).

plot(x,y,’line specifiers’)

The following tables lists the line specifiers:


Line Style Specifiers:

Specifier Line Style


- Solid line (default)
-- Dashed line
: Dotted line

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 3 of 13


Introduction to MATLAB IIl LAB 3
-. Dash dot line

Marker Specifier:

Specifier Marker Type


+ plus sign
O Circle
* Asterisk
. dot
s square
d diamond

Color Specifiers:

Specifier Line Color


r red
g green
b blue
c cyan
m magenta
y yellow
k black

 The specifiers are typed inside the plot() command as strings.

 Within the string the specifiers can be typed in any order.

 The specifiers are optional. This means that none, one, two, or all the three can be included
in a command.

Example:
 plot(x,y) A solid blue line connects the points with no markers.

 plot(x,y,’r’) A solid red line connects the points with no markers.

 plot(x,y,’--y’) A yellow dashed line connects the points.

 plot(x,y,’*’) The points are marked with * (no line between the points.)

 plot(x,y,’g:d’) A green dotted line connects the points which are marked with
diamond markers.

Plot of given data using the line specifiers in the plot ( ) command

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 4 of 13


Introduction to MATLAB IIl LAB 3
Year 1988 1989 1990 1991 1992 1993 1994

Sales 127 130 136 145 158 178 211


(M)
>>year = [1988:1:1994];
>> sales = [127, 130, 136, 145, 158, 178, 211];
>> plot(year,sales,'--r*')

“Fig 2.4”

Formatting the Plots:


A plot can be formatted to have a required appearance.

With formatting you can:


 Add title to the plot.
 Add labels to axes.
 Change range of the axes.
 Add legend.
 Add text blocks.
 Add grid.

Formatting Commands:

title(‘string’)

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 5 of 13


Introduction to MATLAB IIl LAB 3
Adds the string as a title at the top of the plot.

xlabel(‘string’)
Adds the string as a label to the x-axis.

ylabel(‘string’)
Adds the string as a label to the y-axis.

axis([xmin xmax ymin ymax])


Sets the minimum and maximum limits of the x- and y-axes.

legend(‘string1’,’string2’,’string3’)
Creates a legend using the strings to label various curves (when several curves are
in one plot). The location of the legend is specified by the mouse.

text(x,y,’string’)
Places the string (text) on the plot at coordinate x,y relative to the plot axes.

Example of Formatted Plot:

x=[0:0.1:2*pi];
y=sin(x);
z=cos(x);
plot(x,y,x,z)
title('Sample Plot','fontsize',14);
xlabel('X values','fontsize',14);
ylabel('Y values','fontsize',14);
legend('Y data','Z data')
grid on

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 6 of 13


Introduction to MATLAB IIl LAB 3

“Fig 2.5”

Displaying the Multiple Plots:

 Three typical ways to display multiple curves in MATLAB (other combinations are
possible…)
 One figure contains one plot that contains multiple curves
o Requires the use of the command “hold” (see MATLAB help)

 One figure contains multiple plots, each plot containing one curve
o Requires the use of the command “subplot”

 Multiple figures, each containing one or more plots, each containing one or more
curves
o Requires the use of the command “figure” and possibly “subplot”

One Plot Contains Multiple Curves:

Example:

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 7 of 13


Introduction to MATLAB IIl LAB 3
x=[0:0.1:2*pi];
y=sin(x);
z=cos(x);
plot(x,y,x,z)
grid on

Or

x=[0:0.1:2*pi];
y=sin(x);
z=cos(x);
plot(x,y,’b’)
hold on
Plot(x,z,’g’)
hold off
grid on

 The plot figure produced by above code is shown in “Fig 2.5”.

Subplots:

 Subplot divides the current figure into rectangular panes that are numbered row wise.
 Syntax:
subplot(rows,cols,index)

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 8 of 13


Introduction to MATLAB IIl LAB 3

subplot(2,2,1);

subplot(2,2,2)
...

subplot(2,2,3)
...

subplot(2,2,4)
...

“Fig 2.6”

Example of Subplot:

x=[0:0.1:2*pi];
y=sin(x);
z=cos(x);
subplot(1,2,1);
plot(x,y)
subplot(1,2,2)
plot(x,z)
grid on

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 9 of 13


Introduction to MATLAB IIl LAB 3

“Fig 2.7”

Multiple Figures:

Example:

x=[0:0.1:2*pi];
y=sin(x);
z=cos(x);
figure;
plot(x,y)
figure;
plot(x,z)
grid on

MATLAB AS A SIGNAL GENERATOR


%%
% Creating a WAV file from scratch and reading it back
%%
clear all
Fs=5000; % sampling rate
t=0:1/Fs: 5; % time parameter
y=0.1*cos(2*pi*2000*t)-0.8*cos(2*pi*2000*t.^2); % sinusoid and
chirp

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 10 of 13


Introduction to MATLAB IIl LAB 3
%% writing chirp.wav file
audiowrite('chirp.wav',y, Fs)
%% reading chirp.wav back into MATLAB as y1 and listening to it
[y1, Fs] = audioread('chirp.wav');
sound(y1, Fs) % sound generated
figure(4)
plot(t(1:1000), y1(1:1000))

EXERCISES:
Check the functionality of the following and write in your words what each command does in
MATLAB:
b ceil()
b floor()
b round()
b fix()
b iseven()
Hint: Use Product help

TASKS:

Q. No. 01:

Plot : e^(-t).*sint.

 t=0:2*pi/30:2*pi;
 Use Subplot command.

Figure Requirements:

1. The first portion of figure should contain the plot of e^(-t);


2. The second portion should contain the plot of sin(t)
3. Third portion of figure should contain plot of e^(-t).*sin(t) .

Note:

 Use different line styles, markers, and colors for each plot.
 Include title, labels, legend, and annotations where necessary.

Q. No. 02:

Plot: y=t2⋅cos(t)y

 t= 0:π/20:2π
 Use the subplot command.

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 11 of 13


Introduction to MATLAB IIl LAB 3
Figure Requirements:
4. The first subplot should contain the plot of t^2.
5. The second subplot should contain the cos(t) plot.
6. The third subplot should contain the plot of t^2⋅cos(t).

Note:

 Use different line styles, markers, and colors for each plot.
 Include title, labels, legend, and annotations where necessary.

Q. No. 03:

Plot: y=e^−0.5t⋅cos(2t)y

 t=0:π/25:4π
 Use the subplot command.

Figure Requirements:
1. The first subplot should contain the plot of e^{-0.5t}.
2. The second subplot should contain the plot of cos(2t).
3. The third subplot should contain the plot of e^{-0.5t}⋅cos(2t).

Note:

 Use different colors, markers, and line styles for each subplot.
 Add appropriate titles, axis labels, legends, and annotations.

Q. No. 04:

Plot: y=sin(t)+cos(2t)

 t=0:π/15:3/π
 Use the subplot command.

Figure Requirements:
1. The first subplot should contain the plot of sin(t).
2. The second subplot should contain the plot of cos(2t).
3. The third subplot should contain the plot of sin(t)+cos(2t).

Note:

 Use different line styles, markers, and colors for each plot.
 Include a title, labels, legend, and text annotations for clarity.

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 12 of 13


Introduction to MATLAB IIl LAB 3

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 13 of 13

You might also like