MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9...
Transcript of MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9...
MATLAB Laboratory 09/23/10 Lecture
Chapters 5 and 9: Plotting
Lisa A. Oberbroeckling
Loyola University [email protected]
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 1 / 21
Examples of Plots used in Lectures
−1 −0.5 0 0.5 1 1.5 2 2.5 3−5
−4
−3
−2
−1
0
1
2
3
4
5
y=1/x
y=−2x+2
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 2 / 21
Examples of Plots used in Lectures
−10 −5 0 5 10−10
−8
−6
−4
−2
0
2
4
6
8
10
x=2y−y
x=y2−6y
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 3 / 21
Examples of Plots used in Lectures
−1
−0.5
0
0.5
1
−1
−0.5
0
0.5
10
5
10
15
xy
z
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 4 / 21
Examples of Plots used in Lectures
−5
0
5
−5
0
50
10
20
30
40
50
x
Example 2
y
z
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 5 / 21
Plots
Where linspace(0,2*pi)
or 0:pi/60:2*pi (for example) comes in handy!
Also where ”dot operations” are needed
Look at plotEx# m-files on the H-drive
Basic commands:plot(x,y), plot3(x,y,z), surf(x,y,z), mesh(x,y,z).
Useful command: clf: clears the current figure
There are other ways to plot, like using fplot or ezplot
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 6 / 21
Plots
Where linspace(0,2*pi)
or 0:pi/60:2*pi (for example) comes in handy!
Also where ”dot operations” are needed
Look at plotEx# m-files on the H-drive
Basic commands:plot(x,y), plot3(x,y,z), surf(x,y,z), mesh(x,y,z).
Useful command: clf: clears the current figure
There are other ways to plot, like using fplot or ezplot
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 6 / 21
Plots
Where linspace(0,2*pi)
or 0:pi/60:2*pi (for example) comes in handy!
Also where ”dot operations” are needed
Look at plotEx# m-files on the H-drive
Basic commands:plot(x,y),
plot3(x,y,z), surf(x,y,z), mesh(x,y,z).
Useful command: clf: clears the current figure
There are other ways to plot, like using fplot or ezplot
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 6 / 21
Plots
Where linspace(0,2*pi)
or 0:pi/60:2*pi (for example) comes in handy!
Also where ”dot operations” are needed
Look at plotEx# m-files on the H-drive
Basic commands:plot(x,y), plot3(x,y,z),
surf(x,y,z), mesh(x,y,z).
Useful command: clf: clears the current figure
There are other ways to plot, like using fplot or ezplot
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 6 / 21
Plots
Where linspace(0,2*pi)
or 0:pi/60:2*pi (for example) comes in handy!
Also where ”dot operations” are needed
Look at plotEx# m-files on the H-drive
Basic commands:plot(x,y), plot3(x,y,z), surf(x,y,z),
mesh(x,y,z).
Useful command: clf: clears the current figure
There are other ways to plot, like using fplot or ezplot
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 6 / 21
Plots
Where linspace(0,2*pi)
or 0:pi/60:2*pi (for example) comes in handy!
Also where ”dot operations” are needed
Look at plotEx# m-files on the H-drive
Basic commands:plot(x,y), plot3(x,y,z), surf(x,y,z), mesh(x,y,z).
Useful command: clf: clears the current figure
There are other ways to plot, like using fplot or ezplot
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 6 / 21
Plots
Where linspace(0,2*pi)
or 0:pi/60:2*pi (for example) comes in handy!
Also where ”dot operations” are needed
Look at plotEx# m-files on the H-drive
Basic commands:plot(x,y), plot3(x,y,z), surf(x,y,z), mesh(x,y,z).
Useful command: clf: clears the current figure
There are other ways to plot, like using fplot or ezplot
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 6 / 21
Plots
Where linspace(0,2*pi)
or 0:pi/60:2*pi (for example) comes in handy!
Also where ”dot operations” are needed
Look at plotEx# m-files on the H-drive
Basic commands:plot(x,y), plot3(x,y,z), surf(x,y,z), mesh(x,y,z).
Useful command: clf: clears the current figure
There are other ways to plot, like using fplot or ezplot
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 6 / 21
Plotting
Plot commands basically ”connect-the-dots” and you have to tellMATLAB which dots to connect, and how to connect them.
Thus you have more control, so can make parametric plots, or plotswith x as a function of y, much easier than on graphing calculators orother packages.
−3 −2 −1 0 1 2 3−2.5
−2
−1.5
−1
−0.5
0
0.5
1
1.5
2
2.5
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 7 / 21
Plotting
Plot commands basically ”connect-the-dots” and you have to tellMATLAB which dots to connect, and how to connect them.
Thus you have more control, so can make parametric plots, or plotswith x as a function of y, much easier than on graphing calculators orother packages.
−3 −2 −1 0 1 2 3−2.5
−2
−1.5
−1
−0.5
0
0.5
1
1.5
2
2.5
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 7 / 21
Plotting
Plot commands basically ”connect-the-dots” and you have to tellMATLAB which dots to connect, and how to connect them.
Thus you have more control, so can make parametric plots, or plotswith x as a function of y, much easier than on graphing calculators orother packages.
−3 −2 −1 0 1 2 3−2.5
−2
−1.5
−1
−0.5
0
0.5
1
1.5
2
2.5
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 7 / 21
Basic Plot
Basic example: plotEx1a.mx=-2:2;
plot(x,x.^2)
What’s wrong with this plot?
Look at plotEx1aV2.m for better ways to define x
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 8 / 21
Basic Plot
Basic example: plotEx1a.mx=-2:2;
plot(x,x.^2)
What’s wrong with this plot?
Look at plotEx1aV2.m for better ways to define x
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 8 / 21
Basic Plot
Basic example: plotEx1a.mx=-2:2;
plot(x,x.^2)
What’s wrong with this plot?
Look at plotEx1aV2.m for better ways to define x
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 8 / 21
Formatting Plots
Line Specifiers (pages 121-123):
LineStyleLineColorMarkerType
More formatting in Chapter 5: LineWidth, MarkerSize, etc.
Look up and experiment with the different types!
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 9 / 21
More formatting
Good commandsaxis
grid
xlabel, etc.xlim, etc.title
text
legend
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 10 / 21
More formatting
Good commandsaxis
grid
xlabel, etc.xlim, etc.title
text
legend
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 10 / 21
More formatting
Good commandsaxis
grid
xlabel, etc.
xlim, etc.title
text
legend
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 10 / 21
More formatting
Good commandsaxis
grid
xlabel, etc.xlim, etc.
title
text
legend
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 10 / 21
More formatting
Good commandsaxis
grid
xlabel, etc.xlim, etc.title
text
legend
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 10 / 21
More formatting
Good commandsaxis
grid
xlabel, etc.xlim, etc.title
text
legend
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 10 / 21
More formatting
Good commandsaxis
grid
xlabel, etc.xlim, etc.title
text
legend
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 10 / 21
3D Plots
To plot a space curve
Example~r(t) = (3 cos t, 3 sin t, t)plot3(x,y,z) with x, y, z already defined as in 2D plotsORplot3(x,y,z, ’line specifiers’, ’Property Name’, prop
values)
Look at plotEx3a.m
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 11 / 21
3D Plots
To plot a space curve
Example~r(t) = (3 cos t, 3 sin t, t)plot3(x,y,z) with x, y, z already defined as in 2D plotsORplot3(x,y,z, ’line specifiers’, ’Property Name’, prop
values)
Look at plotEx3a.m
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 11 / 21
3D Plots (mesh/surface/contour, etc.)
To plot z = f(x, y), 4 main steps
1 establish the domain for x and y
2 create grid in xy-plane3 calculate z (z is calculated for each point in “grid”)4 make the mesh, surface plots, etc.
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 12 / 21
3D Plots (mesh/surface/contour, etc.)
To plot z = f(x, y), 4 main steps
1 establish the domain for x and y
2 create grid in xy-plane
3 calculate z (z is calculated for each point in “grid”)4 make the mesh, surface plots, etc.
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 12 / 21
3D Plots (mesh/surface/contour, etc.)
To plot z = f(x, y), 4 main steps
1 establish the domain for x and y
2 create grid in xy-plane3 calculate z (z is calculated for each point in “grid”)
4 make the mesh, surface plots, etc.
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 12 / 21
3D Plots (mesh/surface/contour, etc.)
To plot z = f(x, y), 4 main steps
1 establish the domain for x and y
2 create grid in xy-plane3 calculate z (z is calculated for each point in “grid”)4 make the mesh, surface plots, etc.
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 12 / 21
Creating the grid using meshgrid
ExampleDomain is −1 ≤ x ≤ 3, 1 ≤ y ≤ 4
u = -1:3;
v = 1:4;
[x, y] = meshgrid(u,v)
(can use whatever letters, names you want)
[x, y] are matrices of the x and y coords, respectively
(u,v) are the vectors for the domain of x and y (created throughlinspace, etc)
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 13 / 21
Creating the grid using meshgrid
ExampleDomain is −1 ≤ x ≤ 3, 1 ≤ y ≤ 4
u = -1:3;
v = 1:4;
[x, y] = meshgrid(u,v)
(can use whatever letters, names you want)
[x, y] are matrices of the x and y coords, respectively
(u,v) are the vectors for the domain of x and y (created throughlinspace, etc)
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 13 / 21
Creating the grid using meshgrid
ExampleDomain is −1 ≤ x ≤ 3, 1 ≤ y ≤ 4
u = -1:3;
v = 1:4;
[x, y] = meshgrid(u,v)
(can use whatever letters, names you want)
[x, y] are matrices of the x and y coords, respectively
(u,v) are the vectors for the domain of x and y (created throughlinspace, etc)
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 13 / 21
Creating the grid using meshgrid
ExampleDomain is −1 ≤ x ≤ 3, 1 ≤ y ≤ 4
u = -1:3;
v = 1:4;
[x, y] = meshgrid(u,v)
(can use whatever letters, names you want)
[x, y] are matrices of the x and y coords, respectively
(u,v) are the vectors for the domain of x and y (created throughlinspace, etc)
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 13 / 21
Creating the grid using meshgrid
ExampleDomain is −1 ≤ x ≤ 3, 1 ≤ y ≤ 4
u = -1:3;
v = 1:4;
[x, y] = meshgrid(u,v)
(can use whatever letters, names you want)
[x, y] are matrices of the x and y coords, respectively
(u,v) are the vectors for the domain of x and y (created throughlinspace, etc)
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 13 / 21
Calculating z
If you have already created a meshgrid using above example andletters and want to graph
z =3xy2 + 2
x2 + y2
z = (3*x.*y.^2+2)./(x.^2 + y.^2);
Then can usemesh(x,y,z) OR
surf(x,y,z)
(Those are the most common... there are others listed in the book)
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 14 / 21
Calculating z
If you have already created a meshgrid using above example andletters and want to graph
z =3xy2 + 2
x2 + y2
z = (3*x.*y.^2+2)./(x.^2 + y.^2);
Then can usemesh(x,y,z) OR
surf(x,y,z)
(Those are the most common... there are others listed in the book)
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 14 / 21
Calculating z
If you have already created a meshgrid using above example andletters and want to graph
z =3xy2 + 2
x2 + y2
z = (3*x.*y.^2+2)./(x.^2 + y.^2);
Then can usemesh(x,y,z)
OR
surf(x,y,z)
(Those are the most common... there are others listed in the book)
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 14 / 21
Calculating z
If you have already created a meshgrid using above example andletters and want to graph
z =3xy2 + 2
x2 + y2
z = (3*x.*y.^2+2)./(x.^2 + y.^2);
Then can usemesh(x,y,z) OR
surf(x,y,z)
(Those are the most common... there are others listed in the book)
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 14 / 21
Making the plots
Good: xlabel(’x’); ylabel(’y’); zlabel(’z’);
title(’title text’)
Notice the look: maybe should have had
u = linspace(-1,3); v = linspace(1,4);
Different plotsmesh, surfmeshz, meshcsurfc, surflwaterfallcontour, contour3
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 15 / 21
Making the plots
Good: xlabel(’x’); ylabel(’y’); zlabel(’z’);
title(’title text’)
Notice the look: maybe should have had
u = linspace(-1,3); v = linspace(1,4);
Different plotsmesh, surfmeshz, meshcsurfc, surflwaterfallcontour, contour3
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 15 / 21
Making the plots
Good: xlabel(’x’); ylabel(’y’); zlabel(’z’);
title(’title text’)
Notice the look: maybe should have had
u = linspace(-1,3); v = linspace(1,4);
Different plotsmesh, surfmeshz, meshcsurfc, surflwaterfallcontour, contour3
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 15 / 21
View command
view(az,el) OR view([az, el])az = azimuth: angle (in degrees) in xy-plane measured relative tonegative y-axis in COUNTERCLOCKWISE direction
el = elevation: elevation angle (in degrees) from xy-plane
DEFAULT: az = −37.5◦, el = 30◦
projection plane az elxy (top) 0 90xz (side) 0 0yz (side) 90 0
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 16 / 21
View command
view(az,el) OR view([az, el])az = azimuth: angle (in degrees) in xy-plane measured relative tonegative y-axis in COUNTERCLOCKWISE direction
el = elevation: elevation angle (in degrees) from xy-plane
DEFAULT: az = −37.5◦, el = 30◦
projection plane az elxy (top) 0 90xz (side) 0 0yz (side) 90 0
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 16 / 21
View command
view(az,el) OR view([az, el])az = azimuth: angle (in degrees) in xy-plane measured relative tonegative y-axis in COUNTERCLOCKWISE direction
el = elevation: elevation angle (in degrees) from xy-plane
DEFAULT: az = −37.5◦, el = 30◦
projection plane az elxy (top) 0 90xz (side) 0 0yz (side) 90 0
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 16 / 21
Contour Plots
If want to create contour lines:
contour(z,n)
n is the number of lines/levels you want
or
contour3(z,n) or contourf(z,n)
If you want the levels to be labeled:
[c, h] = contour(z, n); clabel(c, h);
MATLAB help: Contour plots... labeling
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 17 / 21
Contour Plots
If want to create contour lines:
contour(z,n)
n is the number of lines/levels you want
or
contour3(z,n) or contourf(z,n)
If you want the levels to be labeled:
[c, h] = contour(z, n); clabel(c, h);
MATLAB help: Contour plots... labeling
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 17 / 21
Contour Plots
If want to create contour lines:
contour(z,n)
n is the number of lines/levels you want
or
contour3(z,n) or contourf(z,n)
If you want the levels to be labeled:
[c, h] = contour(z, n); clabel(c, h);
MATLAB help: Contour plots... labeling
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 17 / 21
Contour Plots
If want to create contour lines:
contour(z,n)
n is the number of lines/levels you want
or
contour3(z,n) or contourf(z,n)
If you want the levels to be labeled:
[c, h] = contour(z, n); clabel(c, h);
MATLAB help: Contour plots... labeling
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 17 / 21
Basic Plot Examples
plotEx1a.m: Basic exampleplotEx1b.m: Basic fplot exampleplotEx1c.m: Lots of command examples to format plot
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 18 / 21
Multiple Plot Examples
plotEx2a.m: Basic multiple plot exampleplotEx2b.m: Using Line SpecifiersplotEx2c.m: Example of hold on, legends, etc.plotEx2d.m: Using fplot and adding pointsplotEx2e.m: More formatting, and adding manual axes
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 19 / 21
Using subplot
plotEx2f.m: Example of subplot using fplot
plotEx2g.m: Example where plot goes across cellsplotEx2h.m: Adding titles to subplotsplotEx2i.m: Example of subplot using plot and formatting the filefor better viewing and editing
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 20 / 21
3D Plot Examples
plotEx3a.m: Space curve examplesplotEx3b.m: 3D surface (and mesh) examples
L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 21 / 21