cmps 1371 introduction to computing for engineers plotting

70
CMPS 1371 Introduction to Computing for Engineers PLOTTING

Upload: denis-hamilton

Post on 28-Dec-2015

251 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: CMPS 1371 Introduction to Computing for Engineers PLOTTING

CMPS 1371Introduction to

Computing for Engineers

PLOTTING

Page 2: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Plotting

1 2 3 4 50

2

4

6

8A bargraph of vector x

1 20

2

4

6

8A bargraph of matrix y

12

34

51

2

0

5

10

A three dimensional bargraph5%

10%

25%

20%

40%

A pie chart of x

Page 3: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Two Dimensional Plots

The xy plot is the most commonly used plot by engineers

The independent variable is usually called x

The dependent variable is usually called y

Page 4: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Consider xy data

time, sec Distance, Ft

0 0

2 0.33

4 4.13

6 6.29

8 6.85

10 11.19

12 13.19

14 13.96

16 16.33

18 18.17

Time is the independent variable and distance is the dependent variable

Page 5: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Plot function

Define x and y and call the plot function

You can use any variable name that is convenient for the dependent and independent variables

Page 6: CMPS 1371 Introduction to Computing for Engineers PLOTTING
Page 7: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Engineers always add …

Title X axis label, complete with units Y axis label, complete with units

Often it is useful to add a grid

Page 8: CMPS 1371 Introduction to Computing for Engineers PLOTTING
Page 9: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Multiple plots

MATLAB overwrites the figure window every time you request a new plot

To open a new figure window use the figure function – for example figure(2)

Create plot on same graph hold on

Freezes the current plot, so that an additional plot can be overlaid

When you use this approach the additional line is drawn in blue – the default drawing color

Page 10: CMPS 1371 Introduction to Computing for Engineers PLOTTING

The first plot is drawn in blue

Page 11: CMPS 1371 Introduction to Computing for Engineers PLOTTING

The hold on command freezes the plot

The second line is also drawn in blue, on top of the original plot

To unfreeze the plot use the hold off command

Page 12: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Same graph

You can also create multiple lines on a single graph with one command

Using this approach each line defaults to a different color

Page 13: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Each set of ordered pairs will produce a new line

Page 14: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Variations

If you use the plot command with a single matrix, MATLAB plots the values versus the index number

Page 15: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Variations

If you want to create multiple plots, all with the same x value you can…

Use alternating sets of ordered pairs plot(x,y1,x,y2,x,y3,x,y4)

Or group the y values into a matrix z=[y1,y2,y3,y4] plot(x,z)

Page 16: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Alternating sets of ordered pairs

Matrix of Y values

Page 17: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Peaks

The peaks(100) function creates a 100x100 array of values. Since this is a plot of a single variable, we get 100 different line plots

Page 18: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Line, Color and Mark Style

You can change the appearance of your plots by selecting user defined line styles color mark styles

Try using: help plot

for a list of available styles

Page 19: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Available choices

Line Type Indicator Point Type Indicator Color Indicator

solid - point . blue b

dotted : circle o green g

dash-dot -. x-mark x red r

dashed -- plus + cyan c

star * magenta m

square s yellow y

diamond d black k

triangle down v

triangle up ^

triangle left <

triangle right >

pentagram p

hexagram h

Page 20: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Specify your choices in a string

plot(x,y,‘linestyle markstyle color') strings are identified with a tick (apostrophe) mark if you don’t specify style, a default is used

line style – none mark style – none color – blue

Consider: plot(x,y,’:ok’) the : means use a dotted line the o means use a circle to mark each point the letter k indicates that the graph should be drawn

in black (b indicates blue)

Page 21: CMPS 1371 Introduction to Computing for Engineers PLOTTING

dotted line

circles

black

Page 22: CMPS 1371 Introduction to Computing for Engineers PLOTTING

specify the drawing parameters for each line after the ordered pairs that define the line

Page 23: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Axis scaling

MATLAB automatically scales each plot to completely fill the graph

If you want to specify a different axis – use the axis command

axis([xmin,xmax,ymin,ymax])

Lets change the axes on the graph we just looked at

Page 24: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Use the axis function to override the automatic scaling

Page 25: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Annotating Your Plots

You can also add legends textbox

Of course you should always add title axis labels

Page 26: CMPS 1371 Introduction to Computing for Engineers PLOTTING
Page 27: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Improving your labels

You can use Greek letters in your labels by putting a backslash (\) before the name of the letter. For example:

title(‘\alpha \beta \gamma’)creates the plot title α β

γ

To create a superscript use curly brackets title(‘x^{2}’)

gives x2

Page 28: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Subplots

The subplot command allows you to subdivide the graphing window into a grid of m rows and n columns

subplot(m,n,p)

rows columns location

Page 29: CMPS 1371 Introduction to Computing for Engineers PLOTTING

subplot(2,2,1)

2 rows

2 columns

1 2

3 4

-20

2-2

02

-5

0

5

x

Peaks

y

Page 30: CMPS 1371 Introduction to Computing for Engineers PLOTTING

2 rows and 1 column

Page 31: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Other Types of 2-D Plots

Polar Plots Logarithmic Plots Bar Graphs Pie Charts Histograms X-Y graphs with 2 y axes Function Plots

Page 32: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Polar Plots

Some functions are easier to specify using polar coordinates than by using rectangular coordinates

Use polar(x,y)

For example the equation of a circle is y=sin(x)in polar coordinates

Page 33: CMPS 1371 Introduction to Computing for Engineers PLOTTING
Page 34: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Logarithmic Plots

A logarithmic scale (base 10) is convenient when a variable ranges over many orders of

magnitude, because the wide range of values can be graphed, without compressing the smaller values.

data varies exponentially

Page 35: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Plots

plot(x,y) – uses a linear scale on both axes

semilogy(x,y) uses a log10 scale on the y axis

semilogx(x,y) uses a log10 scale on the x axis

loglog(x,y) use a log10 scale on both axes

Page 36: CMPS 1371 Introduction to Computing for Engineers PLOTTING
Page 37: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Bar Graphs and Pie Charts

MATLAB includes a whole family of bar graphs and pie charts bar(x) – vertical bar graph barh(x) – horizontal bar graph bar3(x) – 3-D vertical bar graph bar3h(x) – 3-D horizontal bar graph pie(x) – pie chart pie3(x) – 3-D pie chart

Page 38: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Bar Graphs

Page 39: CMPS 1371 Introduction to Computing for Engineers PLOTTING
Page 40: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Histograms

A histogram is a plot showing the distribution of a set of values

Defaults to class size of 10

Page 41: CMPS 1371 Introduction to Computing for Engineers PLOTTING

X-Y Graphs with Two Y Axes

Sometimes it is useful to overlay two x-y plots onto the same figure.

However, if the order of magnitude of the y values are quite different, it may be difficult to see how the data behave.

Page 42: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Scaling Depends on the largest value plotted

Example

Its difficult to see how the blue line behaves, because the scale isn’t appropriate

Page 43: CMPS 1371 Introduction to Computing for Engineers PLOTTING

plotyy function for 2 y-axis

The plotyy function allows you to use two scales on a single graph

Page 44: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Function Plots

Function plots allow you to use a function as input to a plot command, instead of a set of ordered pairs of x-y values

fplot('sin(x)',[-2*pi,2*pi])

function input as a string

range of the independent variable – in this case x

Page 45: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Three Dimensional Plotting

Line plots

Surface plots

Contour plots

Page 46: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Three Dimensional Line Plots

These plots require a set of order triples ( x-y-z values) as input

Use plot3(x,y,z)

The z-axis is labeled the same way the x and y axes are labeled

Page 47: CMPS 1371 Introduction to Computing for Engineers PLOTTING

MATLAB uses a coordinate system consistent with the right hand rule

Page 48: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Just for fun

try the comet3 function, which draws the graph in an animation sequence

comet3(x,y,z)

If your animation draws too slowly, add more data points

For 2-D line graphs use the comet function

Page 49: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Surface Plots

Represent x-y-z data as a surface

mesh - meshplot

surf – surface plot

Can use both mesh and surface plots to good effect with a single two dimensional matrix

Page 50: CMPS 1371 Introduction to Computing for Engineers PLOTTING

The x and y coordinates are the matrix index numbers

Page 51: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Surf plots

surf plots are similar to mesh plots

they create a 3-D colored surface instead of an open mesh

syntax is the same

Page 52: CMPS 1371 Introduction to Computing for Engineers PLOTTING
Page 53: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Shading

There are several shading options shading interp shading flat faceted flat is the default

You can also adjust the color scheme with the color map function

Page 54: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Colormaps

autumn bone hot

spring colorcube hsv

summer cool pink

winter copper prism

jet (default) flag white

Page 55: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Contour Plots

Contour plots use the same input syntax as mesh and surf plots

They create graphs that look like the familiar contour maps used by hikers

Page 56: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Variations

A more complicated surface can be created by calculating the values of z, instead of just defining them

We’ll need to use the meshgrid function to create 2-D input arrays – which will then be used to create a 2-D result

Creates an x by y matrix

Page 57: CMPS 1371 Introduction to Computing for Engineers PLOTTING
Page 58: CMPS 1371 Introduction to Computing for Engineers PLOTTING
Page 59: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Another 3-D graph

Try this example and see what you get

>> g = 0:0.2:10;>> [x,y] = meshgrid(g);>> z = 2*sin(sqrt(x.^2 + y.^2));>> mesh(z);

Page 60: CMPS 1371 Introduction to Computing for Engineers PLOTTING
Page 61: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Cowboy Hat

Try this code

>> [X,Y]=meshgrid(-1.5:.1:1.5,-1.5:.1:1.5);>> Z=sin(3*X.^2+2*Y.^2)./(X.^2+Y.^2);>> mesh(Z)

Page 62: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Cowboy Hat

Page 63: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Combine 3-D Plots

Try this code

>> [s,t]=meshgrid(0:.02*pi:2*pi,0:.02*pi:pi);>> [u,v]=meshgrid(0:.05*pi:2*pi,-2:.2:2);>> surf(3*cos(s).*sin(t),2*sin(s).*sin(t),cos(t))>> axis equal;>> hold on;>> surf(cos(u),sin(u),v)

Page 64: CMPS 1371 Introduction to Computing for Engineers PLOTTING

ellipsoid and cylinder

Page 65: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Editing Plots from the Menu Bar

In addition to controlling the way your plots look by using MATLAB commands, you can also edit a plot once you’ve created it using the menu bar

Another demonstration function built into MATLAB is

>> sphere

Page 66: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Menu Bar

• Once you’ve created a plot you can adjust it using the menu bar

• In this picture the insert menu has been selected

• Notice you can use it to add labels, legends, a title and other annotations

Page 67: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Select

Edit-> Axis Properties from the menu tool bar

Change the Aspect Ratio

Explore the property editor to see some of the other ways you can adjust your plot interactively Select More

Properties to pull up the Inspector

Page 68: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Note

If you adjust a figure interactively, you’ll lose your improvements when you rerun your program

Page 69: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Creating plots from the workspace

Page 70: CMPS 1371 Introduction to Computing for Engineers PLOTTING

Plotting options

MATLAB will suggest plotting options and create the plot for you