Engineering Workshop Lab3
Engineering Workshop Lab3
(EL-1006)
LABORATORY MANUAL
ENGR. Tooba
____________________________________________________________________________________________________________________________________________________________
PLOTTING
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.
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.
“Fig 2.3”
plot(x,y,’line specifiers’)
Marker Specifier:
Color Specifiers:
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,’*’) 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
“Fig 2.4”
Formatting Commands:
title(‘string’)
xlabel(‘string’)
Adds the string as a label to the x-axis.
ylabel(‘string’)
Adds the string as a label to the y-axis.
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.
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
“Fig 2.5”
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”
Example:
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
Subplots:
Subplot divides the current figure into rectangular panes that are numbered row wise.
Syntax:
subplot(rows,cols,index)
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
“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
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:
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.
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.