CS1371 Introduction to Computing for Engineers - PowerPoint PPT Presentation

1 / 52
About This Presentation
Title:

CS1371 Introduction to Computing for Engineers

Description:

Check Web Work about the Lab 1 issues. HW6 due Thursday midnight as always. Reading: ... legend('Legend for graph') grid on. Basic 2D Plotting ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 53
Provided by: david3049
Category:

less

Transcript and Presenter's Notes

Title: CS1371 Introduction to Computing for Engineers


1
CS1371Introduction to Computing for Engineers
  • Plotting (who is?)

2
Administrivia
  • First exam will be handed back next week since
    couldn't finish by yesterday (gt1000 grades).
  • On the first 250, mean was 75
  • Standard deviation was 22!!!!
  • Make up exam will be next Tuesday. Should have
    contacted me and dvaid Smith by now.
  • Check Web Work about the Lab 1 issues.
  • HW6 due Thursday midnight as always.
  • Reading
  • Plotting, Smith Ch. 15

3
Background
  • While numerical methods are the heart (and
    origin) of Matlab, graphics has become the major
    component since the release of Version 4.
  • Version 6 adds to this legacy with refinements
    and new functions.
  • Professional Matlab comes with two thick manuals
    (a) basic commands programming and (b) graphics
  • The graphics capabilities are so broadly defined
    that we will be able to cover only a small part
  • we'll focus on graphics you will find immediately
    useful
  • we will point to some of the areas where you will
    find powerful new capabilities when you need them
    later
  • Like all of us, you will find yourselves
    frequently looking up "help" or checking the
    manuals for graphics!

4
Basic 2D Plotting
  • The simplest kind of plot is a cartesian plot of
    (x,y) pairs defined by symbols or connected with
    lines

gtgt x00.0510pi gtgt yexp(-.1.x).sin(x) gtgt
plot(x,y) gtgt xlabel('X axis description') gtgt
ylabel('Y axis description') gtgt title('Title for
plot goes here') gtgt legend('Legend for graph') gtgt
grid on
NOTE 1Reversing the x,y order (y,x) simply
rotates the plot 90 degrees!
NOTE 2line(x,y) is similar to plot(x,y) but
does not have additional options
5
Do basic 2D
  • gtgt x00.0510pi
  • gtgt yexp(-.1.x).sin(x)
  • gtgt plot(x,y)
  • gtgt xlabel('X axis description')
  • gtgt ylabel('Y axis description')
  • gtgt title('Title for plot goes here')
  • gtgt legend('Legend for graph')
  • gtgt grid on

6
Supporting Commands
  • Several functions let you control the plot
    appearance
  • axis() determines the axis scaling (see help for
    options)
  • hold on/off controls whether or not the plot is
    erased before another plot is drawn (toggles if
    no argument given)

gtgt x00.12pi gtgt plot(x,sin(x)) gtgt axis ans
0 7 -1 1 gtgt axis(0 7 -.5 .5)
gtgt x00.12pi gtgt plot(x,sin(x)) gtgt hold on gtgt
plot(x,cos(x))
7
Using axis for scale hold
  • x00.12pi
  • gtgt plot(x,sin(x))
  • gtgt axis
  • ans
  • 0 7 -1 1
  • help axis
  • gtgt axis(0 7 -.5 .5)
  • x00.12pi
  • gtgt plot(x,sin(x))
  • gtgt hold on
  • gtgt plot(x,cos(x))

8
Using Lines or Markers or Both
  • Plots must follow the following logic
  • Lines whenever plotting analytical functions
    like sin(x) where you can compute y for any value
    of x
  • Markers whenever plotting discrete experimental
    data or whenever the data are known only
    discretely
  • Both connecting markers with straight lines is
    appropriate when you want to show a sequence

gtgt x0.021 gtgt yx.1.5 gtgt yrrandn(size(x)) gt
gt yyy0.1.yr gtgt plot(x,yy,'ro',x,yy)
gtgt x00.021 gtgt yx.1.5 gtgt yrrandn(size(x))
gtgt yyy0.1.yr gtgt plot(x,yy,'rx')
gtgt plot(x,cos(x),'r') gtgt hold on gtgt
plot(x,sin(x),'b--')
9
Using Lines or Markers or Both
  • Plots must follow the following logic
  • Lines whenever plotting analytical functions
    like sin(x) where you can compute y for any value
    of x
  • Markers whenever plotting discrete experimental
    data or whenever the data are known only
    discretely
  • Both connecting markers with straight lines is
    appropriate when you want to show a sequence
  • Plot depends upon "Line Style Spec" (do doc plot
    and then line specs)

gtgt x0.021 gtgt yx.1.5 gtgt yrrandn(size(x)) gt
gt yyy0.1.yr gtgt plot(x,yy,'ro',x,yy)
gtgt x00.021 gtgt yx.1.5 gtgt yrrandn(size(x))
gtgt yyy0.1.yr gtgt plot(x,yy,'rx')
gtgt plot(x,cos(x),'r') gtgt hold on gtgt
plot(x,sin(x),'b--')
10
Using Both Markers Lines
  • Use lines to show analytical fit through discrete
    data

gtgt x0.021 gtgt yx.1.5 gtgt yrrandn(size(x)) gt
gt yyy0.1.yr gtgt plot(x,yy,'x') gtgt
ppolyfit(x,yy,1) p 1.0159 -0.0927 gtgt
hold on gtgt plot(x,polyval(p,x),'r')
gtgt x00.22.pi gtgt ysin(x) gtgt
yrrandn(size(x)) gtgt plot(x,10.yyr,'ro') gtgt
hold on gtgt plot(x,10.y)
11
Plotting Multiple Curves
  • Problem How can you compare several curves?
  • Lets start with the following
  • We could plot these using

gtgt X 0.0pi/1002pi gtgt Y1 cos(X) gtgt Y2
3cos(X) gtgt Y3 cos(2X) gtgt Y4 sin(X)
gtgt plot(X,Y1) gtgt hold on gtgt plot(X,Y2) gtgt
plot(X,Y3) gtgt plot(X,Y4)
12
Plotting Multiple Curves (contd)
  • Or we could do
  • Or we could do this
  • What if we did this?
  • Do a help plot for more markers.
  • How could we see the data points more distinctly?

gtgt plot(X,Y1,X,Y2,X,Y3,X,Y4)
gtgt Z Y1Y2Y3Y4 gtgt plot(X,Z)
gtgt plot(X, Z, 'o')
13
Using 2 Y-axis Scales
  • Sometimes it is useful to plot two curves with
    widely different y-axis scales

gtgt x00.13.pi gtgt y1sin(x0.5) gtgt
y290.sin(x-0.5) gtgt plotyy(x,y1,x,y2)
gtgt x00.13.pi gtgt y1sin(x0.5) gtgt
y290.sin(x-0.5) gtgt plot(x,y1,x,y2)
NOTE it is complicated to label the 2nd axis
14
Basic Plot Commands
  • axis - freezes current axis scaling
  • axis(xmin, xmax, ymin, ymax) sets axis limit
    values (note use of )
  • axis off turns off display of axes (plot
    unchanged)
  • axis on turns on display of axes
  • grid on/off turns on/off display of a grid
  • text(x,y,string) - places horizontal text
    starting at (x,y)
  • gtext(string) places horizontal text starting
    wherever user clicks with mouse
  • line(x,y) adds line specified by x y vectors

15
Example of Log Plots
  • Using a log scale can reveal large dynamic ranges

gtgt xlinspace(.1,10,1000) gtgt damp0.05 gtgt
y1./sqrt((1-x.2).2 (2.damp.x).2) gtgt
plot(x,y) gtgt semilogx(x,y) gtgt loglog(x,y)
Describes the behavior of vibrating systems
16
Subplot Command
  • There are times when it is better to create
    several smaller plots arranged in some kind of
    grid subplot(m,n,k) does this
  • mrows, ncolumns in the grid
  • kcurrent focus (numbered row-wise)
  • Lets define a 2x3 subplot grid for
    subplot(2,3,1) with the focus on the first plot.

3
1
2
3
2
6
4
5
17
On Your Own
  • Putting it all together
  • What does grid do?
  • Whats the quickest way to execute this code?

X00.550 Y5x.2 subplot(2,2,1), plot(X,Y),
title(Polynomial Linear/Linear), ...
ylabel(y), grid subplot(2,2,2), semilogx(X,Y),
title(Polynomial Log/Linear), ...
ylabel(y), grid subplot(2,2,3), semilogy(X,Y),
title(Polynomial Linear/Log), ...
ylabel(y), grid subplot(2,2,4), loglog(X,Y),
title(Polynomial Log/Log), ... ylabel(y),
grid
18
Specialized 2D Plots
  • There are a number of other specialized 2D plots
  • area(x,y) builds a stacked area plot
  • pie() creates a pie chart (with options)
  • bar(x,y) creates a vertical bar chart (with many
    options)
  • stairs(x,y) similar to bar() but shows only
    outline
  • errorbar(x,y,e) plots x vs y with error bars
    defined by e
  • scatter(x,y) creates a scatter plot with options
    for markers
  • semilogx(x,y) plots x vs y with x using a log
    scaling
  • semilogy(x,y) plots x vs y with y using a log
    scaling
  • loglog(x,y) plots x vs y using log scale for
    both axes
  • And many others (explore these yourself you may
    find a good use in a later course)

19
Plotting a Function
  • Suppose have some cool function as function of
    ONE variable.
  • Function sindub sin2(x)
  • Takes sin of 2x
  • Sindub sin(2x)
  • Fplot(sin2(t),150, r)

20
Doing fplot
  • Function sindub sin2(x)
  • Takes sin of 2x
  • Sindub sin(2x)
  • fplot(sin2(t),150, r)

21
3D Plotting
  • Plotting in 3D is where Matlabs power really
    becomes apparent!
  • Matlab defines a number of different kinds of 3D
    plots but you will probably find 3 or 4 to be the
    most useful
  • x,y,z 3D line plot (plot3( ))
  • mesh plot
  • surface plot
  • contour plot
  • combo surface/mesh with contour
  • The surface plotting can be applied to create
    realistic 3D objects by defining and plotting
    their exterior surfaces!
  • We can only touch lightly on this vast area of
    Matlab

22
Anatomy of a 3D Plot
  • There are MANY options for plotting in 3D but we
    will consider only the basics
  • Plotting a curve in 3D plot3(x,y,z)
  • Plotting a surface, zf(x,y), in 3D
  • There are also several other plotting topics that
    we will not cover or discuss in class (but you
    might find interesting to explore on your own)
  • ribbon plots
  • quiver plots (showing vectors)
  • volume plots
  • advanced colormap use

23
2D Plots vs 3D Line Plots
  • Actually, every 2D plot is simply a 3D plot
    without the 3rd dimension being specified.

gtgt clf gtgt x00.12.pi gtgt plot(x,sin(x))
Rotate 3D button experiment with how it works
24
Do plot and rotate
  • clf
  • gtgt x00.12.pi
  • gtgt plot(x,sin(x))

25
3D Line Plot
  • This is the simplest (and least useful) 3D plot
    and is really just a generalization of the 2D
    function

gtgt x00.13.pi gtgt z1sin(x) gtgt
z2sin(2.x) gtgt z3sin(3.x) gtgt
y1zeros(size(x)) gtgt y3ones(size(x)) gtgt
y2y3./2 gtgt plot3(x,y1,z1,'r',x,y2,z2,'b',x,y3,z3
,'g') gtgt grid on gtgt xlabel('x-axis'),
ylabel('y-axis'), zlabel('z-axis')
26
3D Line Plot (2)
  • These examples may be a little more useful

gtgt clf gtgt turns40.pi gtgt thetalinspace(0,turns,
4000) gtgt xcos(theta).(turns-theta)./turns gtgt
ysin(theta).(turns-theta)./turns gtgt
ztheta./turns gtgt plot3(x,y,z) gtgt grid on gtgt
text(0.5,0.5,0.75,'Here is a piece of text!')
gtgt theta00.110.pi gtgt plot3(sin(theta),cos(the
ta),theta) gtgt grid on
27
3D Surface Plots
  • It is often desirable to plot functions of the
    form zf(x,y)
  • for each (x,y), we can compute a value for z
  • this defines a surface in 3D space
  • If we can define (x,y) at regular intervals,
    Matlab provides powerful ways to plot the
    resulting function as a grid or surface in 3D.
  • There are tools in Matlab to handle the situation
    with (x,y) defined at irregular intervals, but we
    won't consider them in this course.
  • We will look into how colors can be employed to
    add the equivalent of a 4th dimension

28
Defining the (x,y) Values
  • We need a way to create the range of (x,y) values
    needed to compute f(x,y)

This won't work because we need all values of y
for each value of x and vice versa to evaluate
function over entire region shown
x -3 -2 -1 0 1 2 3 y -3 -2 -1 0
1 2 3 zf(x,y)
This works corresponding elements of xx yy
contain the x and y coordinates to be used for
f(x,y) at that point
xx -3 -2 -1 0 1 2 3 -3
-2 -1 0 1 2 3 -3 -2
-1 0 1 2 3 -3 -2 -1
0 1 2 3 -3 -2 -1 0
1 2 3 -3 -2 -1 0 1
2 3 -3 -2 -1 0 1 2
3 yy -3 -3 -3 -3 -3 -3 -3
-2 -2 -2 -2 -2 -2 -2 -1
-1 -1 -1 -1 -1 -1 0 0
0 0 0 0 0 1 1 1
1 1 1 1 2 2 2 2
2 2 2 3 3 3 3 3
3 3 zf(xx,yy)
NOTExx varies along ROWS while yy varies along
COLUMNS
29
The meshgrid() Function
  • Matlab provides a function to compute these
    arrays

gtgt x-3.53.5 gtgt y-2.52.5 gtgt
xx,yymeshgrid(x,y) xx -3.5 -2.5 -1.5
-0.5 0.5 1.5 2.5 3.5 -3.5 -2.5 -1.5
-0.5 0.5 1.5 2.5 3.5 -3.5 -2.5 -1.5
-0.5 0.5 1.5 2.5 3.5 -3.5 -2.5 -1.5
-0.5 0.5 1.5 2.5 3.5 -3.5 -2.5 -1.5
-0.5 0.5 1.5 2.5 3.5 -3.5 -2.5 -1.5
-0.5 0.5 1.5 2.5 3.5 yy -2.5 -2.5
-2.5 -2.5 -2.5 -2.5 -2.5 -2.5 -1.5 -1.5
-1.5 -1.5 -1.5 -1.5 -1.5 -1.5 -0.5 -0.5
-0.5 -0.5 -0.5 -0.5 -0.5 -0.5 0.5 0.5
0.5 0.5 0.5 0.5 0.5 0.5 1.5 1.5
1.5 1.5 1.5 1.5 1.5 1.5 2.5 2.5
2.5 2.5 2.5 2.5 2.5 2.5
NOTE 1xx varies along ROWS while yy
varies along COLUMNS
NOTE 2For any (i, j), the value in xx is the x
coordinate, while the value in yy is the y
coordinate xx(3,1) -3.5 yy(3,1) -0.5 So
(x,y) (-3.5, -0.5)
NOTE 3Matlab calls these arrays "plaids"
NOTE 4We can use array math to efficiently
compute the z values when zf(x,y)
30
Calculation of f(x,y) the Hard Way
  • In this case we will plot z (x2 y2) over the
    range -3 ? x ? 3 and -3 ? y ? 3

Element-by-element Calculation of z f(x,y)
gtgt nclength(x) number of columns in z gtgt
nrlength(y) number of rows in z gtgt
zzeros(nr,nc) initialize z (for speed) gtgt for
kc1nc for kr1nr z(kr,kc)
x(kc).2 y(kr).2 end end gtgt
mesh(xx,yy,z)
NOTEIt can be very tricky to keep track of the
row and column indices in this kind of
calculation
31
Calculation of f(x,y) the Matlab Way
  • In this case we use Matlabs scalar operations

gtgt x-33 gtgt y-33 gtgt xx,yymeshgrid(x,y) xx
3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3 -3
-2 -1 0 1 2 3 -3 -2
-1 0 1 2 3 -3 -2 -1
0 1 2 3 -3 -2 -1 0
1 2 3 -3 -2 -1 0 1
2 3 yy -3 -3 -3 -3 -3 -3
-3 -2 -2 -2 -2 -2 -2 -2
-1 -1 -1 -1 -1 -1 -1 0
0 0 0 0 0 0 1 1
1 1 1 1 1 2 2 2
2 2 2 2 3 3 3 3
3 3 3 gtgt zzxx.2 yy.2 gtgt
mesh(xx,yy,zz)
NOTEMatlab's array instructions make this a
deceptively simple calculation for all values at
once
32
Go to Demo script
  • Demo

33
Key Concepts for zf(x,y) Plots
  • 3D plotting introduces several key concepts
  • Meshes versus surfaces
  • Hidden line removal
  • Pedestals and contours
  • Color maps and pseudo-coloring
  • Viewpoints and camera control (advanced!)
  • Shading and lighting (advanced)
  • The following figures demonstrate these concepts,
    but you are encouraged to check this out for
    yourself (using Matlabs graphic brings out the
    real fun in using this powerful software!).

34
Exploring Hidden Line Removal
  • This uses an interesting built-in function
    sphere( )
  • Hidden lines
  • ON shows white inside mesh
  • OFF shows transparent mesh

X,Y,Z sphere(12) subplot(1,2,1) mesh(X,Y,Z),
title('Figure 26.5a Opaque') hidden on axis
square off subplot(1,2,2) mesh(X,Y,Z),title('Fig
ure 26.5b Transparent') hidden off axis square
off
35
Let's Explore the mesh( ) Function
  • We'll use peaks( ) to create a zf(x,y) function
    that is interesting and shows off the 3D plotting
  • Note you should check help peaks and help mesh
    and also the textbook for further details on
    these functions

gtgt x,y,zpeaks(30) gtgt mesh(x,y,z) gtgt axis
tight gtgt xlabel('x-axis') gtgt ylabel('y-axis') gtgt
zlabel('z-axis')
SuggestionTry using hidden off and hidden on to
see what happens.
36
Demo
37
Exploring meshc Plots
  • meshc( ) adds a contour plot directly below the
    mesh
  • helps visualize the contours
  • can locate the peaks and dips

gtgt x,y,zpeaks(30) gtgt meshc(x,y,z) gtgt axis
tight gtgt xlabel('x-axis') gtgt ylabel('y-axis') gtgt
zlabel('z-axis')
HintIf you just finished the previous example,
you need only type in the new meshc( ) command.
38
Exploring meshz Plots
  • This special variation allows you to emphasize
    the zero plane in the mesh plot

gtgt x,y,zpeaks(30) gtgt meshz(x,y,z) gtgt axis
tight gtgt xlabel('x-axis') gtgt ylabel('y-axis') gtgt
zlabel('z-axis')
39
Exploring waterfall Plots
  • This is another variation on the mesh plot and
    can also be useful in some special cases

gtgt x,y,zpeaks(30) gtgt waterfall(x,y,z) gtgt axis
tight gtgt xlabel('x-axis') gtgt ylabel('y-axis') gtgt
zlabel('z-axis')
40
Let's Explore the surf( ) Function
  • So far we have only been able to plot meshes to
    represent the surface
  • can hide hidden lines to clarify the surface
    shape
  • still appears as a wireframe-like shape
  • Matlab provides a function that will fill in the
    mesh with facets (surfaces with 3 or 4 corners
    but not necessarily plane surfaces)
  • we'll see that these can produce very realistic
    appearing surfaces in 3D
  • can control appearance of mesh
  • can change color mapping to reveal other
    information
  • can add lighting

41
Exploring surf Plots (shading faceted)
  • The basic function uses the default shading
    faceted and this shows the mesh

gtgt x,y,zpeaks(30) gtgt surf(x,y,z) gtgt axis
tight gtgt xlabel('x-axis') gtgt ylabel('y-axis') gtgt
zlabel('z-axis')
42
Exploring surf Plots (shading flat)
  • shading flat will eliminate the mesh and leave
    the facets colored with a constant color value

gtgt x,y,zpeaks(30) gtgt surf(x,y,z) gtgt shading
flat gtgt axis tight gtgt xlabel('x-axis') gtgt
ylabel('y-axis') gtgt zlabel('z-axis')
43
Exploring surfc Plots (shading interp)
  • surfc acts much like meshc with a contour plot
    drawn below the surface
  • shading interp interpolates color over each facet

gtgt x,y,zpeaks(30) gtgt surfc(x,y,z) gtgt shading
interp gtgt axis tight gtgt xlabel('x-axis') gtgt
ylabel('y-axis') gtgt zlabel('z-axis')
NOTEshading interp can take time to execute and
the figure may cause plotting problems
44
Changing the Viewing Direction
  • You can change the orientation of the object
  • Viewing direction view(az,el) or you can use
    the rotate3d button on the view toolbar on the
    figure window menu
  • Camera direction this is best controlled from
    the camera toolbar on the figure window menu

z
viewpoint
view(-45,60)
y
elev
x
azimuth
45
Adding a Colorbar
  • You can use the colorbar command to add a color
    bar that defines the color used in the plot. Use
    help colorbar to find out what the other options
    are

NOTE You should check out the instructions on
how to use the Property Editor and the tools in
the Figure window to interactively add text and
labels to various parts of this 3D graph.
46
Using Color as a 4th Dimension
  • Matlab associates a colormap with each figure
    window
  • this is a 3 column array in which columns 1-3
    control the Red, Blue Green colors (0-1 range)
  • each row defines a specific color
  • rows are limited by the color display
    capabilities of the computer
  • these, along with a few fixed colors, are the
    colors Matlab will use in the figure window (each
    figure window has a separate colormap)
  • Matlab predefines a number of useful colormaps
  • JET, HSV, GRAY, HOT, COOL, BONE, COPPER, PINK,
    FLAG, PRISM
  • see help graph3d for more information and other
    colormaps
  • use colormap hsv or colormap(hsv) to change
  • colormap default restores the colormap to default
    values
  • use the colorbar command to display the color bar
    by itself or alongside a plot (see help)

47
Using Color as a 4th Dimension (2)
  • Matlab uses pseudo-color to change the color in
    a mesh or surf plot
  • colors can be based on the z values (default)
  • you can specify the color variable in mesh() and
    surf()
  • Use caxis(cmin cmax) to define the max and min
    values that are mapped to the colormap

gtgt caxis(-5 5) gtgt colorbar
gtgt caxis(-50 50) gtgt colorbar
48
Using Color as a 4th Dimension (3)
  • mesh() and surf() can accept a "color" argument
    that defines the color used over the plaid.

gtgt Cdel2(z) compute Laplacian gtgt
surf(x,y,z,C) gtgt axis tight gtgt colorbar
gtgt caxis('auto') gtgt surf(x,y,z,y) gtgt axis
tight gtgt ylabel('y-axis')
This shows curvature of the surface as the color
variable.
Here we have used the y values as the color
variable.
49
Contour Plots
  • Matlab provides several functions to draw
    contours
  • contour() draws simple contour map with N
    intervals
  • contourf() draws a contour with filled contours
  • contour3() draws a contour map in 3D

x,y,zpeaks(30) contourf(x,y,z,10)
colorbar xlabel('x-axis') ylabel('y-axis')
x,y,zpeaks(30) contour(x,y,z,10)
colorbar xlabel('x-axis') ylabel('y-axis')
NOTE See textbook for other options.
50
Example
  • Here is a simple example to illustrate 3D
    plotting
  • it is the sinc function (sin(r)/r) where
    rradius
  • we need to add eps to avoid inf when dividing by
    zero

xx,yymeshgrid(-4.pipi./54.pi)
Rsqrt(xx.2 yy.2)eps radius
zzsin(R)./R surf(xx,yy,zz) axis tight
Question How could you handle a situation when
the data are not defined on a regular grid (when
the points are unevenly spaced)?
51
And finally.
52
Do Globe
  • Globe
Write a Comment
User Comments (0)
About PowerShow.com