CL2014 - MATLAB Programming - Lec05
CL2014 - MATLAB Programming - Lec05
MATLAB Programming
Lecture: 05
Course Instructor
Engr Muhammad Umar Khan Niazi
Office: C1-21, Ext.: 218, Email: [email protected]
1
2 Plots in MATLAB
Probably the most common form of plot is plot(x, y) where x and y are
vectors of the same length, e.g.,
x = 0:pi/40:4*pi;
plot(x, sin(x))
4 Plots in MATLAB
Grid adds/removes grid lines to/from the current graph. The grid state
may be toggled
text(x, y, ’text’) writes text in the graphics window at the
point specified by x and y. If x and y are vectors, the text is written at
each point.
title(’text’) writes the text as a title on top of the graph.
xlabel(’horizontal’) labels the x-axis.
ylabel(’vertical’) labels the y-axis
9 Plots in MATLAB
To plot x against y
x 1 2 3 5 7 7.5 8 10
y 2 6.5 7 7 5.5 4 6 8
Once the plot command is executed, the Figure Window opens with the
following plot.
10 Plots in MATLAB
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’)
Line Specifier Line Specifier Marker Specifier
Style Color Type
x = -pi:pi/10:pi;
y = tan(sin(x)) - sin(tan(x));
figure
plot(x,y,'--
gs','LineWidth',2,'MarkerSize',10,'MarkerEdgeColor','b','MarkerFace
Color',[0.5,0.5,0.5])
13 Plots in MATLAB
subplot(2,2,1)
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
title('Subplot 1: sin(x)')
subplot(2,2,2)
y2 = sin(2*x);
plot(x,y2)
title('Subplot 2: sin(2x)')
subplot(2,2,3)
y3 = sin(4*x);
plot(x,y3)
title('Subplot 3: sin(4x)')
subplot(2,2,4)
y4 = sin(8*x);
plot(x,y4)
title('Subplot 4: sin(8x)')