0% found this document useful (0 votes)
19 views43 pages

Matlab Part 2

Uploaded by

skylarww998
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views43 pages

Matlab Part 2

Uploaded by

skylarww998
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Lab of COMP 406

Introduction of Matlab (II)


Graphics and Visualization

Teaching Assistant: Pei-Yuan Zhou


Contact: cspyzhou@comp.polyu.edu.hk
Lab 2: 19 Sep., 2014
1
Review
• Find the Matlab under the folder
• 1. Y:\Win32\Matlab\R2012a
• 2. Double click it and open Matlab
• Or open Matlab on your computer
• 1. Click 'Start'
• 2. Click 'Run'
• 3. Input 'nalwin32'
• 4. Find the Matlab under the folder /Network Application
Packages/Statistical & Mathematical/Matlab

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);

Until you use hold off hold on


or close the window, y2 = cos(x);
all plots appear in the plot(x,y2,'r--')
current figure window. legend('sin','cos')
xlabel('x')
title('Plot of the Sine and Cosine Function') 8
Line Plots
• You can label the axes and add a title.
title
legend

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

• Plot two functions, cos(x)+sin(x) and exp(-x) for


variable x in one figure.
• The value of x is from 0 to pi, and the interval
between two points is 0.1.
• Use line format ‘--*’ for function 1, and ‘--o’for
function 2.
• Provide the figure a title, legends of two functions,
and Labels for x-axis and y-axis
17
Exercise - answer
• x=0:0.1:2*pi;
• y1=sin(x)+cos(x);
• y2=exp(-x);
• plot(x,y1,'--*',x,y2,'--O');
• xlabel('time');
• ylabel('value of sin(x)+cos(x) and exp(-x)');
• title('Function Plots of "sin(x)+cos(x)" and
"exp(-x)"');
• legend('sin(x)+cos(x)','exp(-x)');
18
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
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

peaks is a function to produce a 25-by-25 matrix


obtained by translating and scaling Gaussian
distributions, which is useful for demonstrating
MESH, SURF, PCOLOR, CONTOUR, etc
z = peaks(25);
mesh(z) plot the colored parametrix mesh defined
mesh(z); by matrix z.
colormap(hsv);
colormap is a function that sets the colormap
property of a figure. See also hsv, caxis, spinmap,
brighten, rgbplot, figure, colormapeditor.
21
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);

contour(Z) is a contour plot of matrix Z treating the values in Z as heights


above a plane.
* contour(Z,N), draws N contour lines, overriding the automatic value.25
Quiver
x = -2:.2:2;
y = -1:.2:1;
[xx,yy] = meshgrid(x,y);
% meshgrid replicates the grid
vectors x and y to produce the
coordinates of a rectangular grid
(xx,yy)
zz = xx.*exp(-xx.^2-yy.^2);
[px,py] = gradient(zz);
quiver(x,y,px,py,2);

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

• t = 0:pi/2:4*pi; [X,Y,Z] = cylinder(5*sin(t));

• Plot this 3-D function into four subplots for each


axis and the function.

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

• Plot six functions in one window/figure, and give


the title of them

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

• What’s the transpose of A? A’


• What’s the result of (A+B+10)/3? (A + B + 10)/3
• Concatenate A and B horizontally, then vertically.
[A, B] [A;B]
• Subtract the 2nd row vector from A. A(2, :);
• Please clear A and B from Matlab workspace. clear

36
Ex 2
• Make some simple plots:
y = cos(x), x∈ [0, 2π]

• Try to make it smoother by making the ‘step’ smaller


• Label X-axis and Y-axis
• Add a title
• Add a legend

37
Ex 2
• Make some simple plots: y = cos(x), x∈ [0, 2π]
x = 0:2*pi; y = cos(x); plot(x,y)

• Try to make it smoother by making the ‘step’ smaller


x = 0:0.1: 2*pi; y = cos(x); plot(x,y)

• Add x-axis label ‘x-axis’ and y-axis label ‘y-axis’


xlabel(‘x-axis’) ; ylabel(‘Y-axis’);

• Add a title title(‘The plot for function cos(x)’);


• Add a legend legend(‘cos(x)’);
38
Ex 2

39
Ex 3

• Make a more complicated plot


y = e-0.4x sin x, x ∈ [0,2π]

• Label the axes, create a nifty title and legend.


• Type help plot and use the information you get to make
another plot of just the unconnected data points.

40
Ex 3

• Make a more complicated plot: y = e-0.4x sin x, x ∈ [0,2π]


x = 0:0.01:2*pi; y = exp(-0.4*x) .*sin(x); plot(x,y)

• Label the axes, create a nifty title and legend.


xlabel(‘x-axis’); ylabel(‘y-axis’)
title(‘y=exp(-0.4*x)sin(x)’);
legend(‘y=exp(-0.4*x)sin(x)’);

• 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.

Hint: Create the vectors t, x, y, and z, then use the command


plot3(x,y,z).

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

You might also like