Matlab Graphics Tutorial
-
Upload
cheng-an-yang -
Category
Education
-
view
805 -
download
8
Transcript of Matlab Graphics Tutorial
![Page 1: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/1.jpg)
Matlab Graphics
Cheng-An Yang
September 22, 2013
![Page 2: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/2.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Outline
1 2D Plots
2 The Graphical User Interface
3 Advanced Topics
4 Animation
5 3D Plots
6 More Plots
7 Extra
2 / 76
![Page 3: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/3.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Section 1
2D Plots
3 / 76
![Page 4: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/4.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Figure: create
Syntax
• To create an empty figure, callfigure();
• You can also number your figure likefigure(42);
4 / 76
![Page 5: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/5.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Figure: close
Syntax
• Close a specific figureclose(42);
• Close all figuresclose all;
5 / 76
![Page 6: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/6.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Plot
Syntax
• To create a line plot of vector y versus vector t, useplot(t,y);
• What happen if you don’t provide the x-axis data? Tryplot(y);
6 / 76
![Page 7: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/7.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Multiple Lines
Syntax
• You can input more vector pairs likeplot(t1,y1,t2,y2);
7 / 76
![Page 8: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/8.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Exercise: Multiple Lines
Visualize three elementary functions on 0 ≤ t ≤ 2π:
y1 = sin(t)
y2 = cos(t)
y3 = e−t
Plot them on the same figure.
8 / 76
![Page 9: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/9.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Adding Labels
Syntax
• Adding labels on axisxlabel(’x label’);ylabel(’y label’);
• Adding titletitle(’title for the figure’);
• Adding legendslegend(’first’,’second’,...);
9 / 76
![Page 10: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/10.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Grid
Syntax
• Turn on/off the grid linesgrid on;grid off;
10 / 76
![Page 11: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/11.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Changing the Axes Limits
Syntax
• Set the limits of each axisaxis([xmin xmax ymin ymax]);
• If you want to adjust only x-axis or y-axis,xlim([xmin xmax]);ylim([ymin ymax]);
11 / 76
![Page 12: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/12.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Customize Plots
• Using the string specifier to change the line style.• The string specifiers contains
• Line style: {’-’,’--’,’:’,’-.’,’none’}• Marker symbol: {’+’,’o’,’*’,’.’,’x’} and more.• Color: {’r’,’g’,’b’,’w’,’k’}.
• For example,plot(t,x,’--or’);plot(t,x,’r’,t,y,’-.xk’);
12 / 76
![Page 13: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/13.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Exercise: The Envelop
Please duplicate the figure shown below:
0 5 10−1
−0.5
0
0.5
1The blue line is
x = t2 cos(5t)e−t . (1)
The envelope of x(t) is
y = ±t2e−t . (2)
13 / 76
![Page 14: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/14.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Plot Matrix Data
Syntax
• By default plot(Y) will plot each column of Y.• When specifying the x vector,
plot(x,Y);
will try to match the dimension of x and Y.
14 / 76
![Page 15: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/15.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Exercise: Plot Matrix Data
LetY = [ y1; y2; y3 ];
• What is the dimension of Y?• Try plot(t,Y) and plot(Y). Can you anticipate the
outputs?
15 / 76
![Page 16: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/16.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Plot Complex Data
Syntax
• To plot complex array x, useplot(x);
• It is equivalent toplot(real(x),imag(x));
16 / 76
![Page 17: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/17.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Exercise: The Eigenvalues of Random Matrices
Gaussian Random MatrixA Gaussian Random Matrix H is a matrix with standard normalcomponents hij
d= N (0, 1).
h11
h22
h12 h21TX1
TX2
RX1
RX2
• Arises in many applications.• Wireless communication.• Channel gain hij is random.
17 / 76
![Page 18: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/18.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Exercise: The Eigenvalues of Random Matrices
Visualizing the distribution of theeigenvalues of random matrices.
• Generate H = randn(n,n).• [V,D] = eig(H).• Plot its eigenvalues as dots on the
complex plane.• Increase n form 10 to 1, 000. Can
you tell what’s the pattern of theeigenvalues?
18 / 76
![Page 19: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/19.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Plotting Multiple Lines On the Same Axes
• If you call plot twice, the first plot will be erased.• To retain current graph when adding new graph, tell Matlab to
hold on;
• If you want different lines to have different color, usehold all;
• The default ishold off;
19 / 76
![Page 20: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/20.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Exercise: Exponent and Convexity
0 0.2 0.4 0.6 0.8 10
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
x
xn
0.10.51210
• The function xα is convexwhen α ≥ 1.
• When 0 < α < 1 is itconcave.
• Use a for loop and hold toverify this.
• α = {0.1, 0.5, 1, 2, 10}.
20 / 76
![Page 21: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/21.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Section 2
The Graphical User Interface
21 / 76
![Page 22: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/22.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
The GUI
22 / 76
![Page 23: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/23.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Saving and Loading
Some tips:• Save your file in vector formats, such as .eps and .pdf.• Use ‘copy figure’ between applications.• Keep a .fig copy so you can edit it later.
23 / 76
![Page 24: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/24.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Customization Using GUI
• Click the ‘Edit Plot’ icon.• Double click on the figure to
enter the editing mode.• Select an object, and the
property editor will appear.
24 / 76
![Page 25: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/25.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Exercise: Frequency Response
• The transfer function of a second order system has the form
H(s) =ω2
ns2 + 2ζωns + ω2
n,
where ζ is the damping ratio and ωn is the natural frequency.• The frequency response of a system is characterized by
• The magnitude |H(ω)|• The phase ∠H(ω)
25 / 76
![Page 26: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/26.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Exercise: Frequency Response
10−1
100
101−100
−50
0
50
Frequency (rad)
Mag
nitu
de (
dB)
10−1
100
101−π
−π/2
0
Frequency (rad)
Pha
se (
rad)
• Let ωn = 1, ζ = 0.2.• Frequencies from 10−1 to 10
(rad).• The unit imaginary number
in Matlab is 1j and 1i.• Note the magnitude
response is defined as20 log(|H(ω)|).
• You may want to useangle and logspace.
26 / 76
![Page 27: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/27.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Graded Task 1
The Bessel functions Jα(x) of the first kind and order α is thesolution to theBessel’s Equation
x2 d2ydx2 + x dy
dx + (x2 − α2)y = 0.
• Separable solution to many important PDE in cylindricalcoordinate.
• Useful in physics, signal processing and statistics.
27 / 76
![Page 28: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/28.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Graded Task 1
Bessel function has the following representation
Bessel Functions of the First Kind
Jα(x) =∞∑
m=0
(−1)m
m! Γ(m + α + 1)
(12x)2m+α
.
Matlab has built-in function for Jα(x)
Syntax
J = besselj(alpha, x);
28 / 76
![Page 29: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/29.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Graded Task 1
Visualizing Jα(x) with different α. Please create a figure like this:
0 1 2 3 10 15
0.0
1.0
x
Jα(x)
α = 0
α = 1α = 2
α = 3
α = 10
• α = {0, 1, 2, 3, 10}.• 0 ≤ x ≤ 15. Note that some
tick labels are omitted.• Use \alpha for α.• Insert a text box near the
maximum amplitude of eachfunction.
29 / 76
![Page 30: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/30.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Exploring Data
• Change view point, zoomin/out, pan.
• Create multiple datatips.• Select/Brush Data tool.
30 / 76
![Page 31: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/31.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Select And Linking Data
• Open the Variable Editor.• Click the ‘Link Plot’ icon.• Click the ‘Select/Brush
Data’ icon.• Select the data of interest.
31 / 76
![Page 32: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/32.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Exercise: Wave Editing
• Load the chirp.mat file.• Type sound(y,Fs) to
play it.• Use data selection tool to
select and ‘mute’ the chirps.• Play the modified recording.
32 / 76
![Page 33: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/33.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Section 3
Advanced Topics
33 / 76
![Page 34: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/34.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Why Learning Commands?
• Almost everything can be adjusted using GUI, why learningcommands?
• If you only need to edit a figure or two, use GUI.• If you have a dozen of figures, let computers do the work.• To create an animation, you have to know the commands.• Programmer’s pride.
34 / 76
![Page 35: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/35.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Hierarchy of The Graphic Objects
A Matlab plot is composed of (at least) three objects:
(a) Figure window. (b) Axis object. (c) Line series object.
35 / 76
![Page 36: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/36.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Get a Handle on Objects
• Each object has a unique identifier, called the handle.• You can ask Matlab to get and set the properties of the object
with its handle.• To get the handle of the current figure, type
h_fig = gcf;
• To get the handle of the current axis, typeh_ax = gca;
• To get the handle of the line series, useh = plot(t,x);
36 / 76
![Page 37: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/37.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Get and Set Property
• Get the value of the property:value = get(h,’PropertyName’);
• Set using property-value pair:set(h,’PropertyName’,value);
37 / 76
![Page 38: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/38.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Exercise: Batch Process
• The commandsaveas(h_fig, ’file_name’, ’fig’);
will save the figure h fig as file name.fig.• You can replace the third argument by other format. Type
help saveas for more information.• Write a script that does the following:
• For each frequency ω = {1, 2, . . . , 10}, plot sin(ωt) on0 ≤ t ≤ 2π in 10 separate figures.
• Save and name them as freq1.fig, freq2.fig,...freq10.fig files.
• Hint: the command sprintf(’freq%d.fig’,I) willgenerate the desired file name, where I is an integer.
38 / 76
![Page 39: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/39.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Subplot
Syntax
subplot(m,n,1);plot(t,x);
Will create a m-by-n subplot and place the plot of x versus t atlocation 1.
39 / 76
![Page 40: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/40.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Exercise: Sine Wave Matrix
Create 25 subplots of sine wave with increasing frequency.
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
40 / 76
![Page 41: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/41.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Section 4
Animation
41 / 76
![Page 42: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/42.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Making Animated Sequences
• The simplest way: continually erase and redraw your figure.• Key ingredients:
• Loop.• Update data.• Erase and redraw.• Pause.
Example
for I = 1:N % Loop.x = sin(t-I); % Update data.plot(t,x); % Erase and Redraw.pause(0.1); % Pause for 0.1 sec.
end
42 / 76
![Page 43: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/43.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Example: Vibrating String
Standing WaveAs oppose to a travelling wave, a standing wave oscillates in placewithout propagating.
Mathematically it is described by
y = A cos(ωt) sin(kx),
where ω is the angular frequencyand k is the wave number.
• Let ω = 5, k = 1.5,A = 2.• Let 0 ≤ x ≤ 2π.• Make an animation of such
a standing wave.
43 / 76
![Page 44: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/44.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Graded Task 2
Standing WaveA standing wave y can be seen as the result of interferencebetween two waves y1 and y2 travelling in opposite directions.
Mathematically,
y1 = A sin(kx − ωt)
y2 = A sin(kx + ωt)
and
y = A sin(kx−ωt)+A sin(kx+ωt).
• A: amplitude.• k: wave number.• ω: angular velocity.
44 / 76
![Page 45: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/45.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Graded Task 2
Create an animation of a standing wave.
0 2 4 6 8 10
−2
−1.5
−1
−0.5
0
0.5
1
1.5
2
• A = 1, k = 1, ω = 3.• Red wave: from left to right.• Blue wave: from right to
left.• Black wave: the resulting
standing wave.• Try update rate dt = 0.05.• Add a reference line x = 0.• Bonus problem: labelling the
nodes. (the red circles)
45 / 76
![Page 46: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/46.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Section 5
3D Plots
46 / 76
![Page 47: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/47.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
3D Line Plot
For parametric data (x(t), y(t), z(t)), we can use the 3D versionof the plot function.
Syntax
• Plot lines in 3Dplot3(x,y,z);
47 / 76
![Page 48: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/48.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Exercise: Brownian Motion
−500
50
−20
0
20−20
0
20
The position vector xn of aBrownian particle at time n isgiven by
xn = xn−1 + v ,
where v is a standard normalrandom vector.
• Simulate the path of twoBrownian particles startingat the origin.
• Take N = 500 steps.
48 / 76
![Page 49: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/49.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Representing Matrix Data
−100
10
−10
0
10−0.5
0
0.5
1
• Line series are representedby vectors.
• Data with 2D indices arerepresented by matrices.
• Think of the values ofmatrix Z as “height”.
• The indices of x-axis andy-axis are stored in matrix Xand Y.
49 / 76
![Page 50: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/50.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Mesh Grid and Surface Plot
To visualize a function f (x , y) on (x , y) ∈ [a, b]× [c, d ],• First we need to create a rectangular grid.• Matlab provides a useful function to create such a grid:
[X,Y] = meshgrid(x,y);
where x and y are the sampling points on both axis.• Then we can use
surf(X,Y,f(X,Y));
to produce a surface plot.
50 / 76
![Page 51: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/51.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Exercise: The 3D Sinc Function
3D Sinc Function
sinc(r) :=sin(r)
r , where r2 = x2 + y2.
• Visualize the 3D sinc function on (x , y) ∈ [−8, 8]× [−8, 8].• One way to avoid the divided-by-zero error is to use
sin(r)./(r+esp) instead of sin(r)./r.• esp is the smallest representable floating point number in
Matlab.
51 / 76
![Page 52: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/52.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Contour Plot
• Surface plot is fancy but not suitable for putting in your paper!• Contour plot might be more appropriate.
Syntax
• The basic syntax iscontour(Z);
• You can specify the number of contour levelcontour(Z, n_level);
52 / 76
![Page 53: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/53.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Exercise: Scalar Field
Letf (x , y) = x2 − 3 sin(xy).
Use contour plot to visualize f (x , y) on [−2, 2]× [−2, 2].
53 / 76
![Page 54: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/54.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Vector Field
A 2D vector field is a vector-valued function
z(x , y) =
(zx (x)zy (y)
).
Matlab provides a function to visualize vector files.
Syntax
• Plot the vector field Zx and Zy as a function of X and Y
quiver(X,Y,Zx,Zy);
54 / 76
![Page 55: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/55.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Exercise: Scalar Field and its Gradient
• The gradient of a scalar field f (x , y) is defined as
∇f =
(∂∂x f∂∂y f
).
• Numerically, we can approximate the partial derivative by
∂
∂x f (x , y) ≈ f (x + h, y)− f (x , y)
h
for some small number h.• In Matlab, we can use gradient(F) to find the gradient of
the scalar field F. See help for more detail.
55 / 76
![Page 56: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/56.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Exercise: Scalar Field and its Gradient
−2 −1 0 1−2
−1
0
1
• Find the gradient of thescalar field
f (x , y) = x2 − 3 sin(xy).
• Plot f (x , y) usingcontour().
• Plot its gradient usinggradient() in the samefigure.
56 / 76
![Page 57: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/57.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Section 6
More Plots
57 / 76
![Page 58: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/58.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Histogram
Histogram is useful to visualize the distribution of univariate data.
Syntax
• Create a histogram of data vector xhist(x);
• To specify number of bins, usehist(x,m);
58 / 76
![Page 59: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/59.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Exercise: Random Number Generators
• Two important random number generators in Matlab:• Uniform between 0 and 1: rand().• Standard normal: randn().
• Exercise:• Generate N random samples from rand().• Use hist to visualize the distribution.• Experiment with different N and different number of bins.• How many samples are required to produce a good
approximation of it distribution?• Repeat for randn().
59 / 76
![Page 60: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/60.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Area, Bar and Pie ChartSyntax
• Stacks each data series and fill the underlying area withdifferent colors
area(X,Y);
• Create a bar chartbar(Y);
Each column of Y will have the same color and rows aregrouped together.
• Pie chartpie(Y);
60 / 76
![Page 61: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/61.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Exercise: Operating System War!
• Go to http://www.netmarketshare.com/ anddownload the operating system share trend data (Excel file).
• Import it into workspace.• Create the following plots:
• Line plot.• Area chart.• Bar chart.• Pie chart on April, 2012.
61 / 76
![Page 62: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/62.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Scatter Plot
Scatter plot is used to visualize the distribution of two dimensionaldata.Syntax
• To generate the scatter plot for the data vector X and Y
scatter(X,Y)
62 / 76
![Page 63: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/63.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Exercise: Basic Regression• Suppose we are given a data set x and y.• It is assumed that y = ax + b + noise.• Find the best linear fit y = ax + b.
0 0.5 12
3
4
5
6
x
y
y = 2.0x+ 3.0
63 / 76
![Page 64: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/64.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Section 7
Extra
64 / 76
![Page 65: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/65.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Solving Differential Equations
• Differential equation involves derivatives of a function in itsindependent variables.
• Almost everything is described by some differential equation.• Mostafa will talk about solving differential equations using
powerful tools provided by Matlab.• We can create a poor man’s differential equation solver.• The idea of Finite Difference Method (FDM):
∂u∂t ≈
u(t + h)− u(t)
h
for small h.
65 / 76
![Page 66: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/66.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
The Finite Difference Method• Consider the wave equation
utt = c2∆u − but ,
where the Laplacian operator ∆ is defined as
∆u = uxx + uyy .
• Use finite difference to approximate derivatives:
ut ≈u(t + ∆t)− u(t)
∆t
utt ≈u(t + ∆t)− 2u(t) + u(t −∆t)
∆t2 .
Similarly for uxx and uyy .
66 / 76
![Page 67: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/67.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
The Finite Difference Method
• In Matlab, we can use the discrete Laplacian function del2to approximate
∆u ≈ 4h2del2(u),
where h is the spacing in the spatial grid.• Substitute the derivatives by their finite difference
approximation, we get (verify this!)
u(t+∆t) ≈ 2u(t)−u(t−∆t)+h2∆u(t)−b∆t(u(t)−u(t−∆t)).(3)
• Provide the initial data of u and ut at t = 0 and the boundarycondition, we can approximate the solution of the waveequation by recursively solving (3).
67 / 76
![Page 68: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/68.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Demo: 2D Wave Equation
−4−2
02
4
−4
−2
0
2
40
2
4
6
8
10
Figure: The simulated solution to the wave equation.
68 / 76
![Page 69: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/69.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Exercise: Linear Congruential Generator (LCG)• LCG is a popular (and old) Pseudo-Random Numbers
Generator.• Simple and efficient to compute.• Poor choice of parameters lead to bad performance.
LCG
xn+1 ≡ (axn + b) (mod m), (4)
• m > 0: the modulus• a > 0: the multiplier• b ≥ 0: the increment• x0: the seed
69 / 76
![Page 70: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/70.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Histogram
0 10 20 300
5
10
15• Try different parameters and use
hist to evaluate its performance.• If you can’t figure out what
combination of parameters wouldwork, try
• a = 3• b = 0• m = 31• x0 = 1
70 / 76
![Page 71: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/71.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Polar Plot• Sometimes it is easier to express coordinate in the polar form.• Let (x , y) be the coordinates in the Cartesian coordinate
system, its corresponding polar coordinates is given by
r =√
x2 + y2 (5)θ = tan−1(y/x). (6)
Syntax
• Plot r versus theta in the polar coordinatepolar(theta,r);
71 / 76
![Page 72: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/72.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Exercise: Butterfly CurveThe butterfly fly curve, discovered by Temple H. Fay, is generatedby the equations
r = esin θ − 2 cos(4θ) + sin5(2θ − π
24
). (7)
2
4
6
30
210
60
240
90
270
120
300
150
330
180 0
72 / 76
![Page 73: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/73.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Demo: Lorenz Attractor
Lorenz system is a simplified model for atmospheric convection, itis modeled by the ordinary differential equations
dxdt = σ(y − x), (8)dydt = x(ρ− z)− y , (9)dzdt = xy − βz , (10)
where x , y and z are the coordinate of the state, t represents time,ρ, σ and β are parameters.
73 / 76
![Page 74: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/74.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
3D Line Plot
Example Lorenz Attractor
−20 0 20
−500
50
0
20
40
60
xy
z
74 / 76
![Page 75: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/75.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Exercise: Visualizing Gibbs’ Phenomena
75 / 76
![Page 76: Matlab Graphics Tutorial](https://reader034.fdocuments.in/reader034/viewer/2022052217/58ef96f91a28ab9b408b45a7/html5/thumbnails/76.jpg)
2D Plots The Graphical User Interface Advanced Topics Animation 3D Plots More Plots Extra
Exercise: Complex Data as 2D Representation
Plot the Hypocycloid.
76 / 76