Professional Documents
Culture Documents
CH 5 Matlab
CH 5 Matlab
2-D PLOTS
2
Department of Mathematics, UET, Lahore
3
Department of Mathematics, UET, Lahore
4
Department of Mathematics, UET, Lahore
5.1 THE plot COMMAND
5
Department of Mathematics, UET, Lahore
5.1 THE plot COMMAND
6
Department of Mathematics, UET, Lahore
5.1 THE plot COMMAND
Example
>> y = [ 2.1 2.1 2.1 1 0 -1 -2.1 -2.1 -2.1 ];
>> plot( y )
7
Department of Mathematics, UET, Lahore
5.1 THE plot COMMAND
8
Department of Mathematics, UET, Lahore
5.1 THE plot COMMAND
9
Department of Mathematics, UET, Lahore
5.1 THE plot COMMAND
Example
>> x=[1.1 1.8 3.2 5.5 7 7.5 8 10];
>> y=[2 6.5 7 7 5.5 4 6 8];
>> plot(x,y)
10
Department of Mathematics, UET, Lahore
5.1 THE plot COMMAND
11
Department of Mathematics, UET, Lahore
5.1 THE plot COMMAND
Line Colors
12
Department of Mathematics, UET, Lahore
5.1 THE plot COMMAND
Marker Types
13
Department of Mathematics, UET, Lahore
5.1 THE plot COMMAND
14
Department of Mathematics, UET, Lahore
5.1 THE plot COMMAND
15
Department of Mathematics, UET, Lahore
5.1.1 Plot of Given Data
16
Department of Mathematics, UET, Lahore
17
Department of Mathematics, UET, Lahore
5.1.2 Plot of a Function
18
Department of Mathematics, UET, Lahore
5.1.2 Plot of a Function
19
Department of Mathematics, UET, Lahore
5.1.2 Plot of a Function
20
5.2 The fplot Command
Department of Mathematics, UET, Lahore
fplot('function',limits,'line specifiers')
▪Independent variable in function can be
any letter, e.g.,
▪ 8*x^2+5*cos(x) or 8*t^2+5*cos(t)
▪limits – two- or four-element vector
that specifies plot axes limits
▪ [xmin xmax] – limits on x-axis
▪ [xmin xmax ymin ymax] – limits on both
axes
▪line specifiers – same as in plot
22
5.2 The fplot Command
Department of Mathematics, UET, Lahore
Example
>> fplot('x^2+4*sin(2*x)-1',[-3,3])
23
5.3 Plotting Multiple of
Department Graphs in the Same Plot UET,
Mathematics, Lahore
24
5.3.1 Using the plot of
Department Command
Mathematics, UET, Lahore
25
5.3.1 Using the plot of
Department Command
Mathematics, UET, Lahore
26
Department of Mathematics, UET, Lahore
5.3.2 Using the hold on and hold off Commands
28
Department of Mathematics, UET, Lahore
5.3.3 Using the line Command
29
Department of Mathematics, UET, Lahore
5.3.3 Using the line Command
30
Department of Mathematics, UET, Lahore
5.4 Formatting a Plot
32
5.4.1 Formatting a Plot
Department ofUsing Commands
Mathematics, UET, Lahore
33
5.4.1 Formatting a Plot
Department ofUsing Commands
Mathematics, UET, Lahore
legend('text1','text2',...,pos)
writes legend
▪For each graph (data set) displays short line in
same style as graph line and adds specified text
▪ First string goes with first graph plotted, second string
goes with second graph plotted, etc.
▪Most useful for plots having at least two graphs
▪pos values in book are obsolete as of MATLAB
7.0 (R14). Type help legend or see
documentation for new values
34
5.4.1 Formatting a Plot
Department ofUsing Commands
Mathematics, UET, Lahore
35
5.4.1 Formatting a Plot
Department ofUsing Commands
Mathematics, UET, Lahore
36
5.4.1 Formatting a Plot
Department ofUsing Commands
Mathematics, UET, Lahore
Example titles
title('\it What You Should Never See')
makes
What You Should Never See
title('What You Should{\it Never} See')
makes
What You Should Never See
37
5.4.1 Formatting a Plot
Department ofUsing Commands
Mathematics, UET, Lahore
Example titles
title('\fontname{Old English Text MT}...
My Name is Robin Hood')
makes
My Name is Robin Hood
title(...
'{\fontname{Old English Text MT}Robin Hood}...
was here')
makes
Robin Hood was here
38
5.4.1 Formatting a Plot
Department ofUsing Commands
Mathematics, UET, Lahore
39
5.4.1 Formatting a Plot
Department ofUsing Commands
Mathematics, UET, Lahore
40
5.4.1 Formatting a Plot
Department ofUsing Commands
Mathematics, UET, Lahore
Greek characters:
41
5.4.1 Formatting a Plot
Department ofUsing Commands
Mathematics, UET, Lahore
makes
Standard deviation (σ) of resistance in MΩ
42
5.4.1 Formatting a Plot
Department ofUsing Commands
Mathematics, UET, Lahore
43
5.4.1 Formatting a Plot Using Commands
Department of Mathematics, UET, Lahore
Some property-name property-value pairs
44
5.4.1 Formatting a Plot
Department ofUsing Commands
Mathematics, UET, Lahore
45
5.4.1 Formatting a Plot
Department ofUsing Commands
Mathematics, UET, Lahore
46
5.4.1 Formatting a Plot
Department ofUsing Commands
Mathematics, UET, Lahore
grid on
▪ Adds grid lines to plot
grid off
▪ Removes grid lines from plot
47
5.4.1 Formatting a Plot
Department ofUsing Commands
Mathematics, UET, Lahore
This script
48
5.4.1 Formatting a Plot
Department ofUsing Commands
Mathematics, UET, Lahore
49
5.5 Plots With Logarithmic
Department Axes
of Mathematics, UET, Lahore
50
5.5 PLOTS WITH LOGARITHMIC
Department AXES
of Mathematics, UET, Lahore
51
5.5 Plots With Logarithmic
Department Axes
of Mathematics, UET, Lahore
Example
52
5.6 Plots With Error Bars
Department of Mathematics, UET, Lahore
53
5.6 Plots With Error Bars
Department of Mathematics, UET, Lahore
errorbar(x,y,e)
▪All vectors in the command must be
same size
▪x and y are horizontal- and vertical-
axis data
▪e is error measurement at each point
▪ At each y(i), MATLAB draws vertical
error bar from y(i)-e(i) to y(i)+e(i)
54
5.6 Plots With Error Bars
Department of Mathematics, UET, Lahore
errorbar(x,y,d,u)
▪ All vectors in the command must be same size
▪ x and y are horizontal- and vertical-axis data
▪ u,d are error measurements at each point
▪ At each y(i), MATLAB draws vertical error bar from
y(i)-d(i) to y(i)+u(i)
NOTE: - third and fourth arguments in
errorbar at bottom of page should be switched,
i.e., command is errorbar(x,y,u,d)
55
5.7 Plots With Specialof
Department Graphics
Mathematics, UET, Lahore
56
5.7 Plots With Specialof
Department Graphics
Mathematics, UET, Lahore
57
5.7 Plots With Specialof
Department Graphics
Mathematics, UET, Lahore
58
5.8 Histograms
Department of Mathematics, UET, Lahore
59
5.8 Histograms
Department of Mathematics, UET, Lahore
60
5.8 Histograms
Department of Mathematics, UET, Lahore
61
5.8 Histograms
Department of Mathematics, UET, Lahore
62
5.8 Histograms
Department of Mathematics, UET, Lahore
63
5.8 Histograms
Department of Mathematics, UET, Lahore
For two forms, can also get bin centers.
Form 2:
[n xout]=hist(y,nbins)
▪ n same as before
▪ xout(i) is center of ith bin
64
5.9 Polar Plots
Department of Mathematics, UET, Lahore
65
5.9 Polar Plots
Department of Mathematics, UET, Lahore
66
5.10 Putting Multipleof
Department Plots on the Same Page
Mathematics, UET, Lahore
subplot(m,n,p)
divides Figure Window into m
rows and n columns of
subplots
▪Subplots numbered from left to
right and top to bottom, with Subplot numbers for
upper left being subplot 1 and 3x2 set of subplots
lower right subplot m*n. p in
subplot command refers to this
numbering
67
5.10 Putting Multipleof
Department Plots on the Same Page
Mathematics, UET, Lahore
subplot(m,n,p)
▪If subplots don't exist, subplot creates
them and makes subplot p the current
subplot
▪If subplots exist, subplot makes subplot p
the current one
▪When subplot defines current subplot,
next plot and formatting commands draw
in current subplot
See Sample Problem 5-2 for example of
subplot use
68
5.11 Multiple Figure Windows
Department of Mathematics, UET, Lahore
69
5.11 Multiple Figure Windows
Department of Mathematics, UET, Lahore
figure
1. Creates a new Figure Window
2. Labels the window "Figure n"
▪ n such that first window is Figure 1,
second is Figure 2, etc.
3. Makes new window the active
Figure Window
4. Brings window to front of the screen
Subsequent plotting commands
draw in the active Figure Window
70
5.11 Multiple Figure Windows
Department of Mathematics, UET, Lahore
Example
71
5.11 Multiple Figure Windows
Department of Mathematics, UET, Lahore
figure(n)
▪If Figure Window n exists, makes it the
active window
▪If Figure Window n doesn't exist, creates
it and makes it the active window
▪figure(n) useful in scripts, e.g., scripts
in which data set 1 is displayed in Figure
1, data set 2 is displayed in Figure 2, etc.
72
5.11 Multiple Figure Windows
Department of Mathematics, UET, Lahore
73
5.12 Plotting Using the
Department ofPLOTS Toolstrip
Mathematics, UET, Lahore
74
5.12 Plotting Using the
Department ofPLOTS Toolstrip
Mathematics, UET, Lahore
75
5.12 Plotting Using the
Department ofPLOTS Toolstrip
Mathematics, UET, Lahore
77