+ All Categories
Home > Documents > MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9...

MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9...

Date post: 28-May-2020
Category:
Upload: others
View: 3 times
Download: 0 times
Share this document with a friend
56
MATLAB Laboratory 09/23/10 Lecture Chapters 5 and 9: Plotting Lisa A. Oberbroeckling Loyola University Maryland [email protected] L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 1 / 21
Transcript
Page 1: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 2: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 3: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 4: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 5: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 6: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 7: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 8: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 9: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 10: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 11: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 12: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 13: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 14: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 15: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 16: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 17: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 18: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 19: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 20: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 21: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

More formatting

Good commandsaxis

grid

xlabel, etc.xlim, etc.title

text

legend

L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 10 / 21

Page 22: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

More formatting

Good commandsaxis

grid

xlabel, etc.xlim, etc.title

text

legend

L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 10 / 21

Page 23: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

More formatting

Good commandsaxis

grid

xlabel, etc.

xlim, etc.title

text

legend

L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 10 / 21

Page 24: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

More formatting

Good commandsaxis

grid

xlabel, etc.xlim, etc.

title

text

legend

L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 10 / 21

Page 25: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

More formatting

Good commandsaxis

grid

xlabel, etc.xlim, etc.title

text

legend

L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 10 / 21

Page 26: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

More formatting

Good commandsaxis

grid

xlabel, etc.xlim, etc.title

text

legend

L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 10 / 21

Page 27: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

More formatting

Good commandsaxis

grid

xlabel, etc.xlim, etc.title

text

legend

L. Oberbroeckling (Loyola University) MATLAB 09/23/10 Lecture 10 / 21

Page 28: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 29: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 30: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 31: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 32: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 33: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 34: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 35: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 36: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 37: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 38: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 39: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 40: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 41: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 42: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 43: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 44: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 45: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 46: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 47: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 48: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 49: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 50: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 51: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 52: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 53: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 54: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 55: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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

Page 56: MATLAB Laboratory 09/23/10 Lecture [2ex] Chapters 5 and 9 ...math.loyola.edu/~loberbro/ma302/Week3plotting.pdfPlotting Plot commands basically ”connect-the-dots” and you have to

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


Recommended