Experiment 7 Plotting Function
Experiment 7 Plotting Function
______________________________________________________________________________________________
Experiment No. 7
Plotting Function
MATLAB has extensive facilities for displaying vectors and matrices as graphs, as
well as annotating and printing these graphs.
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 50 100 150 200 250
(7 - 1)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7
(7 - 2)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
Color Marker Line style
b Blue . Point - Solid
g Green o Circle : Dotted
r Red x x-mark -. Dashdot
c Cyan + Plus -- Dashed
m Magenta * Star (none) No line
y Yellow s Square
k Black d Diamond
v Triangle (down)
> Triangle (left)
p Pentagram
h hexagram
Example:
x1 = 0:pi/100:2*pi;
x2 = 0:pi/10:2*pi;
plot (x1, sin (x1) , 'r:', x2, cos (x2), 'r+');
1
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7
(7 - 3)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
hold on MATLAB does not replace the existing graph when you issue another
plotting command; it adds the new data to the current graph, rescaling the axes if
necessary.
Example:
x1 = 0:pi/100:2*pi;
x2 = 0:pi/10:2*pi;
plot (x1, sin(x1), 'r:')
hold on
plot (x2, cos(x2), 'r+')
1
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7
(7 - 4)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
the figure window, then the second row, and so on. For example, these statements
plot data in four different sub regions of the figure window.
t = 0:pi/10:2*pi;
x=sin(t); y=cos(t);
z= 2*y-3*x; v=5-z;
subplot (2,2,1); plot(x)
subplot (2,2,2); plot(y)
subplot (2,2,3); plot(z)
subplot (2,2,4); plot(v)
0.8
0.6
0.4
0.2
sin(t)
-0.2
Note the odd symmetry
-0.4
-0.6
-0.8
-1
-3 -2 -1 0 1 2 3
x-axis
Exercises:
1- Plot Sinc function, where sinc (x) = (sin(x) )/ x, and -2π < x <2π
2- Plot sin(x) and cos(x) on the same figure, then on the same axis using
different colors. Also, plot both of them on two figures separately, where
0 < x <2π.
(7 - 6)