Matlab Part 2
Matlab Part 2
2
Review
• Matrix and Array
>>a = [1 2 3 4] / a=[1,2,3,4]
>> a = [1 2 3; 4 5 6; 7 8 10]
>>z = zeros(5,1)
>>a’ / inv(a) / a*a / a.*a / a.^3 / a^3
• Calling functions
>>c=sin(a)
>>max(a) % a is a vector
• Save workspace variables
>>save myfile.mat
>>load myfile.mat
3
Outline
-Graphics and Visualization
2-D Plots
Line Plots of a Chirp, Bar Plot of a Bell Shaped Curve
Stairstep Plot of a Sine Wave, Errorbar Plot
Stem Plot Scatter Plot
3-D Plots
Mesh, Surface (Surfacel), Contour, Quiverm Slice
Sub-Plots
4
Outline
2-D Plots
Line Plots of a Chirp, Bar Plot of a Bell Shaped Curve
Stairstep Plot of a Sine Wave, Errorbar Plot
Stem Plot Scatter Plot
3-D Plots
Mesh, Surface (Surfacel), Contour, Quiverm Slice
Sub-Plots
5
Line Plots
• To create two-dimensional line plots, use the plot function.
For example, plot the value of the sine function from 0 to
2π:
x = linspace(0,2*pi);
% 100 points from 0 to 2pi
y = sin (x); % calculate sin(x)
plot(x,y);
6
Line Plots
X=linspace(0,2*pi); % 100 points from 0 to 2pi
plot(x,sin(x),x,cos(x),x,xin(x)+cos(x)); % multiple lines
7
Line Plots
x = 0:pi/100:2*pi;
% from 0 to 2pi, the interval between two
To add plots to an points is pi/100
existing figure, use
y = sin(x);
hold on.
plot(x,y);
label 9
Line Plots
• By adding a third input argument to the plot function,
you can plot the same variables using a “green star” line.
x = 0:0.05:7;
y=log(x);
plot(x,y,'g*')
xlabel('x')
ylabel(‘log(x)')
title('Plot of the log Function')
10
Line Plots of a Chirp
x = 0:0.05:5;
y = sin(x.^2);
plot(x,y);
xlabel('Time')
ylabel('Amplitude')
plot(sin((0:0.05:5).^2)). 11
Bar Plot of a Bell Shaped Curve
x = -3:0.2:3;
bar(x,exp(-x.*x));
12
Stairstep Plot of a Sine Wave
x = 0:0.25:10;
stairs(x,sin(x));
13
Errorbar Plot
x = -2:0.1:2;
y = erf(x);
e = rand(size(x))/10;
errorbar(x,y,e);
The errorbar function draws a
line plot of x and y values and
superimposes on each
observation a vertical error bar
14
Stem Plot
x = 0:0.1:4;
y = sin(x.^2).*exp(-x);
stem(x,y)
15
Scatter Plot
x = -10:0.5:10;
y = x.^3;
scatter(x,y,'bx');
ylabel('X-axis');
xlabel('Y-axis');
doc line,
doc scatter,
doc line_props
16
Exercise
2-D Plots
Line Plots of a Chirp, Bar Plot of a Bell Shaped Curve
Stairstep Plot of a Sine Wave, Errorbar Plot
Stem Plot Scatter Plot
3-D Plots
Mesh, Surface (Surfacel), Contour, Quiverm Slice
Sub-Plots
19
3-D Plots
• Three-dimensional plots typically display a surface defined by a
function in two variables, z = f (x,y).
• To evaluate z, first create a set of (x,y) points over the domain of the
function using meshgrid.
[X,Y] = meshgrid(-2:.2:2);
Z = X .* exp(-X.^2 - Y.^2);
• Then, create a surface plot.
surf(X,Y,Z) / mesh(X,Y,Z)
• Both the surf function and its companion mesh display surfaces in
three dimensions. surf displays both the connecting lines and the
faces of the surface in color. mesh produces wireframe surfaces that
color only the lines connecting the defining points. 20
Mesh Plot of Peaks
z = peaks(25);
mesh(z);
colormap(hsv);
22
Surface Plot of Peaks
z = peaks(25);
surf(z);
colormap(jet);
23
Surface plot with shading of peaks
z = peaks(25);
surfl(z);
shading interp;
colormap(pink);
24
Contour Plot of Peaks
z = peaks(25);
contour(z,16);
colormap(hsv);
Plots velocity vectors as arrows with components (px,py) at the points (x,y).
*And automatically scales the arrows to fit within the grid and then stretches
them by 2. Use 0 to plot the arrows without the automatic scaling.
26
Outline
2-D Plots
Line Plots of a Chirp, Bar Plot of a Bell Shaped Curve
Stairstep Plot of a Sine Wave, Errorbar Plot
Stem Plot Scatter Plot
3-D Plots
Mesh, Surface (Surfacel), Contour, Quiverm Slice
Sub-Plots
28
Subplot You can display
multiple plots in
different subregions
• [X,Y] = meshgrid(-2:.2:2); of the same window
using the subplot
• Z = X .* exp(-X.^2 - Y.^2);%3D function function.
• subplot(2,2,1); mesh(X); title('X'); % the plot for x-axis
• subplot(2,2,2); mesh(Y); title('Y'); % the plot for y-axis
• subplot(2,2,3); mesh(Z); title('Z'); % the plot for z-axis
• subplot(2,2,4); mesh(X,Y,Z); title('X,Y,Z'); % the plot for the
3D function
The first two inputs to the subplot function indicate the number of plots
in each row and column. The third input specifies which plot is active.
29
Subplot
30
Exercise
31
Answer
• t = 0:pi/2:4*pi;
• [X,Y,Z] = cylinder(5*sin(t));
• subplot(2,2,1); mesh(X);
title('X');
• subplot(2,2,2); mesh(Y);
title('Y');
• subplot(2,2,3); mesh(Z);
title('Z');
• subplot(2,2,4); mesh(X,Y,Z);
title('X,Y,Z');
32
Try this example
x=[0,2pi], y4=exp(x);
y1=sin(x); y5=log(x);
y2=cos(x); y6=x.^3
y3=sin(x)+cos(x);
33
Answer
• x=0:pi/10:2*pi;
• y1=sin(x);
• y2=cos(x);
• y3=sin(x)+cos(x);
• y4=exp(-x);
• y5=log(x);
• y6=x.^(1/3);
• subplot(2,3,1); plot(y1); title('sin(x)');
• subplot(2,3,2); plot(y2); title('cos(x)');
• subplot(2,3,3); plot(y3);
title('sin(x)+cos(x)');
• subplot(2,3,4); plot(y4); title('exp(x)');
• subplot(2,3,5); plot(y5); title('log(x)');
• subplot(2,3,6); plot(y6); title('x^1^/^3'); 34
Ex 1
• Please add variable A and B to Matlab workspace
• Create A and B.
• What’s the transpose of A?
• What’s the result of (A+B+10)/3?
• Concatenate A and B horizontally, then vertically.
• Subtract the 2nd row vector from A.
• Please clear A and B from Matlab workspace.
35
Ex 1
• Please add variable A and B to Matlab workspace
A=[1 2 3 4; 5 6 7 8; 11 12 13 14; 15 16 17 18];
B=A+4*j
36
Ex 2
• Make some simple plots:
y = cos(x), x∈ [0, 2π]
37
Ex 2
• Make some simple plots: y = cos(x), x∈ [0, 2π]
x = 0:2*pi; y = cos(x); plot(x,y)
39
Ex 3
40
Ex 3
• Type help plot and use the information you get to make
another plot of just the unconnected data points.
plot(x, y, ‘:’) / plot (x,y, ‘--’)
41
Ex 3
• Make a 3-dimensional plot of the helix x = sin t, y = cos t,
z = t.
t = 0:0.01:2*pi;
x = sin(t); y = cos(t);
z = t;
plot3(x, y, z);
grid on;
42
What We have Learned?
1. 2-D Plot
2. 3-D Plot
3. Sub-Plot
43
Contact: cspyzhou@comp.polyu.edu.hk
Lab 2: 19 Sep., 2014
44