Lab 3
Lab 3
Programming in Matlab
Programming in Matlab
switch(o)
switch(o)
Switch<expression>
case
case'+'
'+'
case <condition>,
x+y
x+y
<statement>
otherwise< condition
>,
< statement>
case
case'-''-'
x-y
x-y
case
case'*'
'*'
end
x*y
x*y
case
case'/''/'
x/y
x/y
end
Programming in Matlab
Plotting in Matlab
Programming in Matlab
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
the abscissa (horizontal axis) and the
(vertical axis).
x values on
and
coordinates of the
Programming in Matlab
PLOT OF GIVEN DATA
Given data:
x
7.5
10
6.5
5.5
Programming in Matlab
PLOT OF GIVEN DATA
Programming in Matlab
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)
Programming in Matlab
plot(x,y,line specifiers)
Line
Style
Solid
dotted
dashed
dash-dot
Specifier
:
--.
Line
Color
red
green
blue
Cyan
magenta
yellow
black
Specifier
r
g
b
c
m
y
k
Marker Specifier
Type
plus sign
circle
asterisk
point
square
diamond
+
o
*
.
s
d
Programming in Matlab
LINE SPECIFIERS IN THE plot() COMMAND
10
plot(x,y,r)
plot(x,y,--y)
plot(x,y,*)
plot(x,y,g:d)
Programming in Matlab
PLOT OF GIVEN DATA USING LINE
SPECIFIERS IN THE plot() COMMAND
Year
1988
1989
1990
1991
1992
1993
1994
Sales (M)
127
130
136
145
158
178
211
Line Specifiers:
dashed red line and
asterisk markers.
11
Programming in Matlab
PLOT OF GIVEN DATA USING LINE
SPECIFIERS IN THE plot() COMMAND
12
Programming in Matlab
13
Programming in Matlab
Plot title EXAMPLE OF A FORMATTED 2-D PLOT
Light Intensity as a Function of Distance
1200
14
Legend
Theory
Experiment
y axis
label
1000
Text
Tick-mark
INTENSITY (lux)
800
Comparison between theory and experiment.
600
400
Data symbol
200
10
12
x axis
label
14
16
18
DISTANCE (cm)
20
22
24
Tick-mark label
Programming in Matlab
FORMATTING 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.
15
Programming in Matlab
FORMATTING COMMANDS
title(string)
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.
16
Programming in Matlab
FORMATTING COMMANDS
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.
17
Programming in Matlab
EXAMPLE OF A FORMATTED PLOT
Syntax:
colo
r
line
marker
plot(x1,
plot(x1, y1,
y1, 'clm1',
'clm1', x2,
x2, y2,
y2, 'clm2',
'clm2', ...)
...)
Example:
x=[0:0.1:2*pi];
x=[0:0.1:2*pi];
y=sin(x);
y=sin(x);
z=cos(x);
z=cos(x);
plot(x,y,x,z)
plot(x,y,x,z)
title('Sample
title('Sample Plot','fontsize',14);
Plot','fontsize',14);
xlabel('X
xlabel('X values','fontsize',14);
values','fontsize',14);
ylabel('Y
ylabel('Y values','fontsize',14);
values','fontsize',14);
legend('Y
data','Z
legend('Y data','Z data')
data')
grid
grid on
on
18
Programming in Matlab
19
Sample Plot
Title
Ylabel
Grid
Legend
Xlabel
Programming in Matlab
20
Programming in Matlab
One Plot Contains Multiple Curves
Example:
x=[0:0.1:2*pi];
x=[0:0.1:2*pi];
y=sin(x);
y=sin(x);
z=cos(x);
z=cos(x);
plot(x,y,x,z)
plot(x,y,x,z)
grid
grid on
on
x=[0:0.1:2*pi];
x=[0:0.1:2*pi];
y=sin(x);
y=sin(x);
z=cos(x);
z=cos(x);
plot(x,y,b)
plot(x,y,b)
hold
hold on
on
plot(x,z,g)
plot(x,z,g)
hold
hold off
off
grid
grid on
on
21
Programming in Matlab
Sample Plot
22
Programming in Matlab
23
Subplots
subplot divides the current figure into rectangular panes that
are numbered rowwise.
subplot(rows,cols,index)
Syntax: subplot(rows,cols,index)
subplot(2,2,1);
subplot(2,2,1);
subplot(2,2,2)
subplot(2,2,2)
...
...
subplot(2,2,3)
subplot(2,2,3)
...
...
subplot(2,2,4)
subplot(2,2,4)
...
...
Programming in Matlab
One Figure Contains Multiple Plots
Example:
x=[0:0.1:2*pi];
x=[0:0.1:2*pi];
y=sin(x);
y=sin(x);
z=cos(x);
z=cos(x);
subplot(1,2,1);
subplot(1,2,1);
plot(x,y)
plot(x,y)
subplot(1,2,2)
subplot(1,2,2)
plot(x,z)
plot(x,z)
grid
grid on
on
24
Programming in Matlab
25
Programming in Matlab
26
Programming in Matlab
27
Programming in Matlab
28
Programming in Matlab
29
Programming in Matlab
30
Programming in Matlab
31
Programming in Matlab
THE END
32