MATLAB
MATLAB
Advanced Plotting
and Model Building
5-2
More? See
pages 260261.
5-4
(continued )
(continued )
5-8
5-10
5-11
Example:
z = 0.3 + 0.7*i
n = [0:0.001:100];
plot(z.^n)
xlabel('Real')
ylabel('Imaginary')
axis equal
0.6
Imaginary
0.4
0.2
-0.2
-0.4
5-12
-0.4
-0.2
0.2
Real
0.4
0.6
0.8
The fplot smart command for plotting functions - plots function specified as string.
It automatically analyzes the function to pick proper spacing, so that plot will show all
the features of the function adaptive plotting.
Syntax is fplot(string,[xmin xmax ymin ymax])
[x,y] = fplot(cos(tan(x)) tan(sin(x)),[1,2]) - automatically
picked denser points in the more complicated region
5-13
5-14
50
40
30
20
10
0
-10
5-15
-20
-2
-1
-0.5
0
x
0.5
1.5
5-16
5-18
5-19
Subplots
Matlab can create array of rectangular panes subplots.
The syntax is subplot(m,n,p).
This command divides the Figure window into an array of
rectangular panes with m rows and n columns.
The variable p tells MATLAB to place the output of the plot
command following the subplot command into the pth pane.
For example, subplot(3,2,5) creates an array of six panes,
three panes deep and two panes across, and directs the next
plot to appear in the fifth pane (in the bottom-left corner).
5-20
5-21
More on
subplots?
See page
271.
5-22
Overlay Plots
Simple Overlay:
x = -1:0.01:2;
y1 = x.^2 - 1;
y2 = x.^3 - 2;
plot(x,y1,x,y2)
legend('y1','y2')
6
y1
y2
5
4
3
2
1
0
-1
-2
-3
-1
-0.5
0.5
1.5
Matrix Overlays:
plot(A) plots the columns of A vs indices and generates n curves where A is matrix with m rows
and m columns.
plot(x,A) plots the matrix A vs the vector x, where x is either a row vector or column vector and
A is a matrix with m rows and n columns.
If the length of x is m, then each column of A is plotted vs vector x
If the length of x is n , then each row
of A is plotted vs vector x
plot(A,x) plots the vector x vs the matrix A
If the length of x is m, then x is plotted vs each column of A
If the length of x is n , then x is plotted vs each row
of A
plot(A,B) plots columns of the matrix B vs columns of the matrix A
5-23
5-24
Data markers
Dot (.)
Asterisk (*)
Cross ()
Circle ()
Plus sign ()
Square ( )
Diamond ( )
Five-pointed star (w)
.
*
s
d
p
Solid line
Dashed line
Dash-dotted line
Dotted line
Colors
.
.
Black
Blue
Cyan
Green
Magenta
Red
White
Yellow
Other data markers are available. Search for markers in MATLAB help.
Useful when you plot many different data types on the same plot
5-26
k
b
c
g
m
r
w
y
x1 = -1:0.1:2;
x2 = 0:0.1:1.5;
y1 = -x1.^2 + 1;
y2 = x2.^3 - 1;
plot(x1,y1,x2,y2,'*:')
legend('y1','y2')
y1
y2
2
-1
-2
-3
-1
-0.5
0.5
1.5
More?
See
pages
273-274.
5-27
5-29
The gtext and text commands are also useful. Figure 5.25
Another way to distinguish graphs is to
place text next to each one.
GTEXT('string') displays the graph
window, puts up a cross-hair, and waits
for a mouse button or keyboard key to be
pressed.
The cross-hair can be positioned with the
mouse (or with the arrow keys on some
computers).
Pressing a mouse button or any key
writes the text string onto the graph at
the selected location.
Example
gtext({first line','second line'})
gtext({'First line','Second
line'},'FontName','Times','Fontsize',12)
5-30
Example:
Circuit representation of a power supply and a load. Example 5.2-1. Figure 5.26
5-31
Plot of the load line and the device curve for Example 5.21.
Figure 5.27
5-32
5-33
(continued )
(continued )
5-36
5-37
Logarithmic Plots
It is important to remember the following points when
using log scales:
1) You cannot plot negative numbers on a log scale,
because the logarithm of a negative number is not
a real number.
2) You cannot plot the number 0 on a log scale,
because log10 0 ln 0 .
You must choose an appropriately small number as
the lower limit on the plot.
(may just rescale your data to start with 1)
(continued)
5-39
(continued)
5-40
5-41
5-42
70
70
power
exp
log
60
60
50
50
40
40
30
30
20
20
10
10
1.5
2.5
10
10
10
10
10
10
-1
10
0 0
10
-1
1.5
2.5
10
10
RC = 0.1;
s = [ 1:100 ] * i;
M = abs(1./(RC*s+1));
loglog(imag(s),M)
At omega about 10 rad/sec the amplitude
of any signal with freq greater than this
one, will decrease by more than 30%
=> low pass filter
See
pages
287-289.
set(gca,XTicklabel,[Jan; Feb
set(gca,XTick,[1:6])
])
Description
bar(x,y)
plotyy(x1,y1,x2,y2)
polar(theta,r,type)
stairs(x,y)
stem(x,y)
5-43
A polar plot showing an orbit around Sun (at the origin) having an
eccentricity of 0.5.
Polar plot r = r(theta) very
hard to do by hand!!!
See pages
290-291.
5-48
5-51
The Figure window with the Figure and Plot Edit toolbars
displayed. Figure 5.42
5-52
5-54
8
linear
power
x = linspace(0.1,2,10);
y1 = 1 + 0.5*x;
y2 = 2*x.^2;
y3 = 10.^(0.3*(1+x))
subplot( 2, 2, 1)
plot(x,y1,'d-.',x,y2,'*-',x,y3,'o:')
legend('linear','power','10^x')
grid on
subplot( 2, 2, 2)
semilogx(x,y1,'d-.',x,y2,'*-',x,y3,'o:')
grid on
subplot( 2, 2, 3)
semilogy(x,y1,'d-.',x,y2,'*-',x,y3,'o:')
grid on
subplot( 2, 2, 4)
loglog(x,y1,'d-.',x,y2,'*-',x,y3,'o:')
grid on
10
0.5
1.5
10
10
10
10
10
10
-1
-1
10
10
-2
10
0 -1
10
-2
0.5
1.5
10
-1
10
10
10
5-57
5-60
(continued)
5-61
5-62
Description
p =
polyfit(x,y,n)
5-63
5-64
(5.55)
w p1z p2
5-65
(5.56)
w p1z p2
My Example
Stretch of a spring data
390 ];
p = polyfit(x,y,1)
xv = linspace(min(x),max(x),1000);
yv = polyval(p,xv);
residuals = y - polyval(p,x);
dmax = max(abs(residuals))
D = sqrt( sum(residuals.^2) / length(x))
subplot(2,1,1)
plot(x,y,'o',xv,yv)
grid on
title(['D = ',num2str(D),
dmax = ',num2str(dmax)])
subplot(2,1,2)
plot(x,residuals,'*:')
grid on
title('Residuals')
Fitting line
D = 4.1596 dmax = 8.4741
400
200
0
-200
10
20
30
40
50
60
70
80
90
100
60
70
80
90
100
Residuals
10
5
0
-5
-10
10
20
30
40
50
400
300
%Fitting line
p = 4.0693 *x -25.4071
%Fitting quadratic, just make it polyfit(x,y,2):
p = 0.0049*x^2 + 3.5629*x -17.1145
200
100
0
10
20
30
40
50
60
70
80
90
100
60
70
80
90
100
Residuals
2
0
-2
-4
10
20
30
40
50
5-67
5-68
x = [ 0:100:800 ];
% x is applied force
y = [ 0 0.09 0.18 0.28 0.37 0.46 0.55 0.65 0.74];
% y is beam deflection
p = polyfit(x,y,1)
xv = linspace(min(x),max(x),1000);
yv = polyval(p,xv);
k = 1/p(1)
residuals = y - polyval(p,x);
dmax = max(abs(residuals))
D = sqrt( sum(residuals.^2) / length(x))
subplot(2,1,2)
plot(x,residuals,'*:')
grid on
title('Residuals')
p=
0.5
0
-0.5
100
200
300
400
Force
Residuals
500
600
700
800
100
200
300
400
500
600
700
800
-3
0.0009*x - 0.0018
1
Defection
subplot(2,1,1)
plot(x,y,'o',xv,yv)
grid on
title(['D = ',num2str(D),' dmax =
',num2str(dmax)])
xlabel('Force'); ylabel('Deflection');
x 10
( f = k*x )
-5
subplot(2,2,2)
semilogy(x,y,'o-')
xlabel('Time (sec)');
p = polyfit(x,log10(y),1)
m = p(1)
b = 10^p(2)
xv = linspace(min(x),max(x),1000);
yv = b*10.^(m*xv);
p = -0.0002 x + 1.8889
m = -1.5563e-004
b = 77.4361
The model is T 68 = b*10^(m*t)
80
60
40
20
1.6
10
10
1.4
10
4000
10
10
1.8
10
subplot(2,2,4)
plot(xv,yv+68,x,y+68,'o',t120,120,'*')
xlabel('Time (sec)'); ylabel('Relative Temperature (F)');
subplot(2,2,3)
semilogy(xv,yv+68,x,y+68,'o',t120,120,'*')
xlabel('Time (sec)'); ylabel('Relative Temperature (F)');
4000
4000
4000
160
140
120
100
80
5-69
5-70
Extrapolation:
V = 36
f36 = 0.2356
filltime = 4.2452
-0.7
10
-0.8
10
-0.9
10
10
Volume (cups)
9
Fill time (sec)
disp('Extrapolation:')
V = 36
f36 = b*V^m
filltime = 1/f36
---------------------------------------------------p = 0.4331*x - 1.3019
m = 0.4331
b = 0.0499
subplot(2,1,2)
plot(xv,1./yv,x,1./y,'o')
xlabel('Volume (cups)'); ylabel('Fill time (sec)');
axis('tight')
8
7
6
5
10
15
20
25
Volume (cups)
30
35
40
Flow rate and fill time for a coffee pot. Figure 5.57
5-71
i1
[ f (xi ) yi ]2
(5.61)
We can use this criterion to compare the quality of the curve fit
for two or more functions used to describe the same data.
The function that gives the smallest J value gives the best fit.
5-72
5-73
The least squares fit for the example data. Figure 5.62
5-74
See pages
312-315.
10
9
8
7
6
5
4
3
2
1
0
5-75
The program
is on pages
315 to 316.
5-76
J = 2048.368
Polyn deg = 2
120
120
100
100
80
80
60
60
Polyn deg = 1
40
40
20
20
J = 179.9441
0
0
Polyn deg = 3
10
J = 8.9246
Polyn deg = 4
120
120
100
100
80
80
60
60
40
40
20
20
10
J = 0.27338
0
0
10
10
5-77
J = 11.7413
1
0
-1
-2
-2
0
Polyn deg = 5
-3
J = 4.6253
Polyn deg = 6
J = 9.784e-024
0
x
-2
-2
-4
J = 10.2753
-4
Polyn deg = 4
-4
0
-6
5-78
i1
(yt y )2
(5.62)
(5.63)
20
20
10
10
0
-10
-5
-10
-5
10
10
20
10
x
10
0
-10
-5
20
x
-10
0
10
-20
-5
10
More? See
pages 320321.
5-82
5-83
See
pages
321322.
5-81
Example 5.6.1
Estimation of Traffic Flow
x = [ 1990:1999 ];
y= [ 2.1 3.4 4.5 5.3 6.2 6.6 6.8 7 7.4 7.8];
p = polyfit(x,y,3)
Warning: Polynomial is badly conditioned. Add points with distinct X
values, reduce the degree of the polynomial, or try centering
and scaling as described in HELP POLYFIT.
x = [ 1990:1999 ] - 1990;
y= [ 2.1 3.4 4.5 5.3 6.2 6.6 6.8 7 7.4 7.8];
ym = mean(y)
p = polyfit(x,y,3)
residuals = y - polyval(p,x);
subplot(2,1,2)
plot(x,residuals,'o:')
xlabel('year'); ylabel('Cars (millions)');
axis('tight')
title('Residuals')
p=
Cars (millions)
subplot(2,1,1)
plot(xv,yv,x,y,'o')
xlabel('year'); ylabel('Cars (millions)');
axis('tight')
7
6
5
4
3
0
Cars (millions)
xv = linspace(min(x),max(x),1000);
yv = polyval(p,xv);
5
year
Residuals
0.1
0
-0.1
0
year
Traffic Flow
Alternative approach using mu
x = [ 1990:1999 ] ;
y= [ 2.1 3.4 4.5 5.3 6.2 6.6 6.8 7 7.4 7.8];
[p,s,mu] = polyfit(x,y,3)
xv = linspace(min(x),max(x),1000);
yv = polyval(p,xv,s,mu);
7
6
5
4
3
1990
Cars (millions)
residuals = y - polyval(p,x,s,mu);
subplot(2,1,2)
plot(x,residuals,'o:')
xlabel('year'); ylabel('Cars (millions)');
axis('tight')
title('Residuals')
Cars (millions)
subplot(2,1,1)
plot(xv,yv,x,y,'o')
xlabel('year'); ylabel('Cars (millions)');
axis('tight')
1991
1992
1993
1994
1995
year
Residuals
1996
1997
1998
1999
1991
1992
1993
1994
1995
year
1996
1997
1998
1999
0.1
0
-0.1
1990
See pages
325-326.
5-84
See
pages
329-331.
5-85
5-86
5-87
5-88
More?
See
pages
331-334.
7-32
% P 446 my example
clear all; clf;
% temperature measured every
% 2 hours, but need it every hour
x = 0:2:24;
y = 60 + 20*sin(2*pi*x/24);
xv = linspace( min(x),max(x),1000);
yv = 60 + 20*sin(2*pi*xv/24);
xint = 0:24;
yint = interp1(x,y,xint);
plot(xv,yv,x,y,'o',xint,yint,'*')
80
75
70
65
60
55
50
45
40
10
15
20
25
7-33
More? See
pages 444-449.
7-35
54.55
Cubic-spline interpolation:
The following session produces and plots a cubic-spline
fit, using an increment of 0.01 in the x values.
>>x = [7,9,11,12];
>>y = [49,57,71,75];
>>x_int = [7:0.01:12];
>>y_int = spline(x,y,x_int);
>>plot(x,y,o,x,y,--,x_int,y_int),...
xlabel(Time (hr)),ylabel(Temperature
(deg F),...
title(Temperature Measurements at a
Single Location),...
axis([7 12 45 80])
This produces the next figure.
7-36
7-37
80
75
70
65
60
10
15
20
25
7-38
7-39
7-40
300
breaks = 7 14 21 28 35 42
coeffs =
-0.0004 0.6102 0.4619 8.0000
-0.0004 0.6020 8.9476 41.0000
-0.0972 0.5939 17.3190 133.0000
0.0626 -1.4469 11.3476 250.0000
0.0626 -0.1327 0.2905 280.0000
m= 5
n=
4
250
200
150
100
50
10
15
20
25
30
35
40
45
3-D plots
We look at three basic types:
line, surface and contour plots.
>>t = [0:pi/50:10*pi];
>>plot3(exp(-0.05*t).*sin(t),exp(-0.05*t).*cos(t),t),...
xlabel(x),ylabel(y),zlabel(z),grid
t goes from 0 to 10*pi
sin/cos go through 5 cycles
x^2 + y^2 = exp(-0.1t) decreases.
x and y decrease!
5-89
Surface Plots:
z = f(x,y) represents a surface in 3D.
mesh, surf etc generate surface plots.
First, need to generate a grid (mesh) of points in x-y plane and evaluate f on it.
x = xmin: dx : xmax; y = ymin: dy : ymax;
[X,Y] = meshgrid(x,y) generates such a grid with
(xmax,ymax)
Matrices X and Y contain coordinate pairs of every point on the grid
If x and y are the same, can use [X,Y]
= meshgrid(x)
The following session shows how to generate the surface plot of the function
z = xe-[(x-y2)2+y2], for -2 <= x <= 2 and -2 <= y <= 2, with a spacing of 0.1.
This plot appears in Figure 5.82.
>>[X,Y] = meshgrid(-2:0.1:2);
>>Z = X.*exp(-((X-Y.^2).^2+Y.^2));
>>mesh(X,Y,Z),xlabel(x),ylabel(y),zlabel(z)
5-91
5-92
0.5
0.4
0.3
0.2
0.1
0
2
1
0
-1
y
-2
-2
-1
x
Contour Plots
Topographic plots show the contours of the land by means of
constant elevation lines contour lines.
The following session generates the contour plot of the function
whose surface plot is shown in Figure 5.82;
namely, z xe[(xy2)2y2], for 2 x 2 and 2 y 2, with a
spacing of 0.1.
>>[X,Y] = meshgrid(-2:0.1:2);
>>Z = X.*exp(-((X- Y.^2).^2+Y.^2));
>>contour(X,Y,Z),xlabel(x),ylabel(y)
5-93
More? See
page 337.
5-94
Description
Creates a contour plot.
mesh(x,y,z)
meshc(x,y,z)
meshz(x,y,z)
surf(x,y,z)
surfc(x,y,z)
waterfall(x,y,z)
5-95
5-96
5-97
Figure P27
5-98
Figure P28
5-99
Figure P56
5-100