Sei sulla pagina 1di 16

The Freshman Computer Skills Manual 16 - 1 Prof.

Martin Patt, UMass-Lowell

ARRAYS, MATRIX ALGEBRA & COMPLEX NUMBERS

Graphical Plots and Histograms

MATLAB will become your favorite tool for plotting data. You can supply labels for your
axes and a title for your graph. A number of curves, in a variety of colors and styles, can
be placed on a single graph.

Table of Contents

Graph title and various labels 1 3-D Wire-Frame Surfaces 10


Specifying scales for the axes 3 3-D Plots in Space 13
Graph paper (polar, semi-log, etc.) 6 Plot Editor - Modifying your Plots 14
Bar charts and Staircases 10

Before you begin

Review the curve-fitting exercise of Chapter 15. Define the same eighteen data points and
generate the same two polynomial curves, as follows.

>> x = [ 1 : 18 ] ; data
points
>> y = [ 2, 3, 4, 5, 6, 6, 6, 5, 6, 7, 8, 9, 8, 7, 4, 2, 0, -2 ] ;

>> c2 = polyfit( x, y, 2 )
Show the coefficients
>> c6 = polyfit( x, y, 6 )

>> y2 = polyval( c2 , x ) ;

>> y6 = polyval( c6 , x ) ;

>> plot( x , y , 'ob' , x , y2 , 'r' , x , y6 , 'g' )

green
circles in blue red

Note: To view your graph (beginning with MATLAB 4.0), you may have to use your mouse to select Figure
No.1 in the Windows pull-down menu, or else from the Task Bar at the bottom of your screen.

A title for your graph, and labels for your axes and curves

Complete the graph at the top of the next page, by copying the curves for y2 and y6 from
your terminal screen. Then you will start labeling.
16 - 2

10

-2
0 2 4 6 8 10 12 14 16 18

Here is how you put a title on your graph. Do it, and then record, on the
diagram above, what you see on your terminal screen.

>> title( 'Curve-fitting using 2nd and 6th-degree polynomials' )

Now label the x-axis. Record, above, what you see on your terminal screen.

>> xlabel( 'Time elapsed (years) since Perestroika' )

Label the y-axis. Record, above, what you see on your terminal screen.

>> ylabel( 'Potato production in the Ukraine' )

Label the 2nd degree polynomial curve. Record, above, what you see on
your terminal screen.

>> gtext( 'Second-degree polynomial' )


Now, move your mouse, and press your mouse button to select a cursor position
close to the point (x=2, y=8). gtext creates the string, and your mouse drops it!
16 - 3

Label the 6th degree polynomial curve. Record, above, what you see on
your terminal screen.

>> gtext( 'Sixth-degree polynomial' )


Now, move your mouse, and press your mouse button to select a cursor position
close to the point (x=6, y=4). Don't forget to copy what you see onto the
diagram!

Finally, put grid lines on your graph. Record, above, what you see on your
terminal screen.

>> grid

Specifying scales for the axes

Before you call the plot function, you can specify scaling for the axes. Try
each of the following sets of commands, and record how the axes are
graduated.

Plotting with automatic scaling

>> plot( x , y2)


>> grid

Declaring X and Y-axis scales

>> scale = [ 8 , 18 , -1 , 8 ]
>> axis(scale)

Return to automatic scaling

>> axis auto


16 - 4

An Exercise - Curve-Fitting Velocity to Determine Acceleration


Air Force Tower 3, monitoring the horizontal velocity of Test Flight X2010, records the following
flight data during the first 9 minutes after takeoff..

Time (seconds) Velocity (ft/second)

0 0

60 1,496

120 2,816

180 3,960

240 4,928

300 5,720

360 6,336

420 6,776

480 7,040

540 7,128

In this exercise, you will use Matlab to plot the aircraft's progress and to find a polynomial
expression for velocity. That polynomial will then be used to write an expression for the
aircraft's acceleration.
Begin by entering the data from the table. First, fill in the blank spaces below, and then key-in
the data. Caution: Key-in large numbers like 1,496 without using a comma (1496)

>> time = [ 0 : 60 : 540 ]

>> velocity = [ 0 1496 2816 ]

>> plot(time, velocity, 'ob')

>> xlabel( )

>> ylabel( )

>> title( )

>> grid

Complete the graph of your data in the space provided at the bottom of the next page by labeling
the axes and showing the title that you assigned, above.
16 - 5

A Polynomial expression for Velocity


>> c2 = polyfit(time, velocity, 2)

show the velocity coefficients here By knowing those coefficients, you can write v(t)

c2 = v(t) =

And now you will direct Matlab to plot both the velocity and its polynomial approximation
expression so that you may compare them, and thereafter you will have confidence when you
use the polynomial expression v(t) to represent the observations from the control tower.
>> vel2 = polyval(c2, time)
>> plot( time, velocity, 'ob', time, vel2, 'r' )

Sketch the combined plot below.

Acceleration (for students who have learned some elementary Differential Calculus)
Finally, we differentiate the expression for velocity that you wrote in the second box, above, to
obtain an expression for the acceleration of the aircraft.

a(t) = - 0.0488 t + 26.4


16 - 6

Selecting graph paper


In this section, you will plot a variety of functions on several different types
of graph paper. In each case, you will observe how the graphical
presentation of your function is affected by the graph paper which you
select.

Function
y = e -x For the four types of graph paper
shown, sketch the shape of the curve,
MATLAB commands and show how the axes are graduated.

>> x = linspace( 0 , 5 , 100 )


>> y = exp(-x)

>> plot(x , y) >> semilogx(x , y) >> semilogy(x , y) >> loglog(x , y)


>> grid >> grid >> grid >> grid

Function
y = ln(x) For the four types of graph paper
shown, sketch the shape of the curve,
MATLAB commands and show how the axes are graduated.

>> x = linspace( 0.001, 5 , 100 )


>> y = log(x)

>> plot(x , y) >> semilogx(x , y) >> semilogy(x , y) >> loglog(x , y)


>> grid >> grid >> grid >> grid
16 - 7

Function
y = log10 (x) For the four types of graph paper
shown, sketch the shape of the curve,
MATLAB commands and show how the axes are graduated.

>> x = linspace( 0.001 , 5 , 100 )


>> y = log10(x)

>> plot(x , y) >> semilogx(x , y) >> semilogy(x , y) >> loglog(x , y)


>> grid >> grid >> grid >> grid

Function MATLAB commands


r = sin -0 >> theta= linspace( 0 , 2*pi , 100 )
>> r = sin(theta)

>> plot(theta, r) >> polar(theta, r)


>> grid >> grid
16 - 8

Function MATLAB commands


r = cos -0 >> theta = linspace( 0 , 2*pi , 100 )
>> r = cos(theta)

>> plot(theta, r) >> polar(theta, r)


>> grid >> grid

Function MATLAB commands


r = cos 2 -0 >> theta = linspace( 0 , 2*pi , 100 )
>> for i = 1 : 100 , r(i) = cos(theta(i) ) * cos( theta(i) ) ; end

>> plot(theta, r) >> polar(theta, r)


>> grid >> grid
16 - 9

Function MATLAB commands


r = 1 >> theta = linspace( 0 , 2*pi , 100 )
>> for i = 1 : 100 , r(i) = 1 ; end

>> plot(theta, r) >> polar(theta, r)


>> grid >> grid

Function MATLAB commands


r = -0 >> theta = linspace( 0 , 2*pi , 100 )
>> for i = 1 : 100 , r(i) = theta(i) ; end

>> plot(theta, r) >> polar(theta, r)


>> grid >> grid
16 - 10

Bar Graphs vs. Histograms

In this exercise, you will define a matrix consisting of 18 experimental measurement


values. These values will then be displayed, one by one, as a series of bars, and
then in the form of a histogram. Do you understand the difference?

>> y = [2, 3, 4, 5, 6, 6, 6, 5, 6, 7, 8, 9, 8, 7, 4, 2, 0, -2]

>> bar( y ) >> hist( y )

Three Dimensional Wire-Frame Mesh Surface

MATLAB's mesh command makes three-dimensional plots from a two-dimensional


matrix. It treats each matrix element as the height of a surface covering the matrix.
Here is a simple example to try. Sketch the resulting three dimensional wire-frame
mesh surface, as best you can.
>> mesh(z)
Begin by defining the matrix [z]

0 0 0 0 0 0 0
0 0 1 1 1 0 0
0 1 2 2 2 1 0
0 1 2 3 2 1 0
0 1 2 2 2 1 0
0 0 1 1 1 0 0
0 0 0 0 0 0 0
16 - 11

3D Wire-Frame Surface over a Two-Dimensional Mesh Grid

In this exercise, you will define a square-mesh grid over which a surface will be
plotted. The mesh grid will defined for x and y between -3 and +3. The surface z
will be plotted above that grid acording to the formula

z = x

>> x = linspace( -3 , 3 , 50 );
>> y = x;
>> [ x , y ] = meshgrid( x, y );
>> z = x;
>> mesh( z )

3D Wire-Frame Surface over a Two-Dimensional Mesh Grid

In this exercise, you will define a square-mesh grid over which a surface will be
plotted. The mesh grid will defined for x and y between -3 and +3. The surface z
will be plotted above that grid acording to the formula

z = y

>> x = linspace( -3 , 3 , 50 );
>> y = x;
>> [ x , y ] = meshgrid( x, y );
>> z = y;
>> mesh( z )
16 - 12

3D Wire-Frame Surface over a Two-Dimensional Mesh Grid

In this exercise, you will define a square-mesh grid over which a surface will be
plotted. The mesh grid will defined for x and y between -3 and +3. The surface z
will be plotted above that grid acording to the formula

z = x+y

>> x = linspace( -3 , 3 , 50 );
>> y = x;
>> [ x , y ] = meshgrid( x, y );
>> z = x + y;
>> mesh( z )

3D Wire-Frame Surface over a Two-Dimensional Mesh Grid

In this exercise, you will define a square-mesh grid over which a surface will be
plotted. The mesh grid will defined for x and y between -3 and +3. The surface z
will be plotted above that grid acording to the formula

z = y2

>> x = linspace( -3 , 3 , 50 );
>> y = x;
>> [ x , y ] = meshgrid( x, y );
>> z = y.^2;
>> mesh( z )
16 - 13

3D Wire-Frame Surface over a Two-Dimensional Mesh Grid

In this exercise, you will define a square-mesh grid over which a surface will be
plotted. The mesh grid will defined for x and y between -3 and +3. The surface z
will be plotted above that grid acording to the formula
5
z = -
2 2
1 + x + y

>> x = linspace( -3 , 3 , 50 );
>> y = x;
>> [ x , y ] = meshgrid( x, y );
>> z = -5. / ( 1 + x. ^2 + y. ^2 );
>> mesh( z )

Three-dimensional Plots in Space

MATLAB's plot3 command makes three-dimensional plots in space. Here is a simple


example to try. Sketch the resulting three-dimensional plot, as best you can.

>> clf
>> t = linspace( 0 , 6 * pi , 100 );
>> x = cos( t );
>> y = sin( t );
>> z = t;
>> plot3( x , y , z )
16 - 14

Plot Editor - Modifying your Plots


Beginning with Matlab 6, you can modify an existing plot using Matlab's plot-editing tools. To
activate the object-editing tool, go to the Figure window and click on the left-leaning arrow in the
menu bar near the top of the window.

If you do not have a plot handy, create one now


>> x = linspace(0, 2*pi, 50) ;
>> y = sin(x) ;
>> plot(x, y)
>> grid

Click on the left-leaning arrow and then on the


plotted curve.

Click on the curve with your right-mouse button to obtain the pop-up menu
shown here. Now you should experiment with line-width, style, and color
to give yourself a feel for plot editing.
While you are at it, use the other plot-editing tools (from the menu bar at the
top of the Figure window) to add text, lines, and arrows to your graph.
16 - 15

Chapter No. 16 - Self Testing

Exercise 16.1 - True or False? Give your opinion of the truth of each of the
following statements by placing a T or an F to the left of each line.

___ Matlab can produce multiple curves in a single plot by listing more
than just one set of arguments For example: ( x, y, 'ob', x, y2, 'g' )

___ Using Matlab, you can put a title on your plots, and label the axes.

___ Matlab's gtext function allows you to place labels wherever you wish
on your plots.

___ You can specify x and y-axis scales using Matlab's axis command.

___ You can put grid lines on your plots using Matlab's grid command.

___ Matlab's grid command also removes grid lines from your plots.

___ Matlab facilitates plotting on a variety of graph paper styles, including


linear, semi-log, log-log, and polar coordinate paper styles.

___ Matlab provides two styles of semi-log graph papers. One with a
logarithmic horizontal scale, and the other with a logarithmic vertical
scale.

___ You can generate 3-D plots using Matlab's plot3 and mesh commands.

Exercise 16.2 - A Matching Problem. The left column consists of MATLAB


commands. Draw a line to connect each command with its function.

polar Draws grid lines on your plot

bar Displays your data in the form of a histogram

loglog Specifies how the axes will be scaled

gtext Places a label anywhere you want on your plot

ylabel Labels the vertical axis of your plot

axis Plots data, with both axes logarithmic

hist Displays data values, one after another

grid Plots data in a polar-coordinate graph style


16 - 16

Exercise 16.3 - On a set of rectangular coordinates, plot both the sine and cosine
of x. Use MATLAB's gtext function to label the two curves. Put a title on the
plot, label the axes, and copy your final plot here.

Exercise 16.4 - On a set of polar coordinates, plot both the sine and cosine of x.
Use MATLAB's gtext function to label the two curves. Add a title, and copy your
final plot here. [Hint: Matlab plots only one curve at a time in polar plots. To get the second
curve onto the same polar graph as the first, you must issue Matlab's hold command before
asking for the second plot. This freezes the first plot and allows you to plot on top of it.]

Potrebbero piacerti anche