Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
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 ) ;
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.
Now label the x-axis. Record, above, what you see on your terminal screen.
Label the y-axis. Record, above, what you see on your terminal screen.
Label the 2nd degree polynomial curve. Record, above, what you see on
your terminal screen.
Label the 6th degree polynomial curve. Record, above, what you see on
your terminal screen.
Finally, put grid lines on your graph. Record, above, what you see on your
terminal screen.
>> grid
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.
>> scale = [ 8 , 18 , -1 , 8 ]
>> axis(scale)
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)
>> 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
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' )
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.
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.
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.
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.
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
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 )
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
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 )
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
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 )
>> clf
>> t = linspace( 0 , 6 * pi , 100 );
>> x = cos( t );
>> y = sin( t );
>> z = t;
>> plot3( x , y , z )
16 - 14
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
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 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.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.]