0% found this document useful (0 votes)
18 views

Cse1121 Lecture05

The document discusses various plotting and visualization techniques in MATLAB including two-dimensional and three-dimensional plots, histograms, functions with multiple inputs and outputs, and user-defined functions. Key functions and concepts covered include plot, mesh, surf, histogram, subplot, and anonymous functions.

Uploaded by

mustafakamak04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Cse1121 Lecture05

The document discusses various plotting and visualization techniques in MATLAB including two-dimensional and three-dimensional plots, histograms, functions with multiple inputs and outputs, and user-defined functions. Key functions and concepts covered include plot, mesh, surf, histogram, subplot, and anonymous functions.

Uploaded by

mustafakamak04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 81

CSE1121

Scientific
Programming
Lecture 5: Plotting
• Two-Dimensional Plots
• Subplots
• Three Dimensional Plotng
•User Defined Functons
Histograms
• Useful for statstcal analysis

• Plot that shows the distributon of your data

• Computes the number of values falling into 10 bins


(categories)
Example of Exam
Distribution
Count your Data points

E > 0 to 60
D > 60 to 70
C > 70 to 80
B > 80 to 90
A > 90 to 100
>> bin_edge=[0,60,70,80,90,100.1]; >>
bar(bin_edge,n)
X-Y Graphs with Two Y Axes
• Sometmes 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.
For example
Scaling Depends on the
largest value plotted
• Its difficult to see how the blue line behaves, because the
scale isn’t appropriate
The plotyy
function allows
you to use two
scales on a
single graph
Different Types of Plots with two Y-axes
Adding Labels

But how do you add the


right axis label?
Give the plot a name - also
called a ‘handle’
Function Plots
• Functon plots allow you to use a functon 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
Three Dimensional Plotting
• Line plots
• Surface plots
• Contour plots
Three Dimensional Line Plots
• These plots require a set of order triples ( x-y-z
values) as input

The z-axis is labeled


the same way the x
and y axes are labeled
MATLAB uses
a coordinate
system
consistent with
the right hand
rule
Just for fun
• try the comet3 functon, which draws the graph in
an animaton sequence
• comet3(x,y,z)
• If your animaton draws too quickly, add more data
points
• For 2-D line graphs use the comet functon
Surface Plots
• Represent x-y-z data as a surface
• mesh - meshplot
• surf - surface plot
Both Mesh and Surf
• Can be used to good effect with a single two
dimensional matrix
The x and y
coordinates are the
matrix index
numbers
Using mesh with 3 variables
• If we know the values of x and y that correspond to
our z values, we can plot against those values
instead of the index numbers
Same number of columns as in the z
Same number of rows as in the z
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
Shading
• There are several shading optons
• shading interp
• shading flat
• faceted flat is the default
• You can also adjust the color scheme with the color
map functon
Default shading
shading interp
shading flat
Colormaps

autumn bone hot


spring colorcube hsv
summer cool pink
winter copper pris
jet (default) flag whi
colormap (hot)
colormap (cool)
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
To demonstrate these functions lets
use a more interesting example
• A more complicated surface can be created by
calculatng the values of z, instead of just
defining them
•We’ll need to use the meshgrid functon to
create 2-D input arrays - which will then be
used to create a 2-D result
Editing Plots from the Menu Bar

• In additon 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 demonstraton functon built into MATLAB
is
sphere
Once you’ve created a plot you
can adjust it using the menu
bar
• In this picture the
insert menu has
been selected
•Notce you can use
it to add labels,
legends, a ttle and
other annotatons
Select
Edit-> Axis
Properties from the
menu tool bar

Explore the
property editor to
see some of the
other ways you Change the
can adjust your Aspect Ratio
plot interactively Select Inspector
from the
Property Editor
• If you adjust a figure interactvely, you’ll lose your
improvements when you rerun your program
Interactive Plotting in MATLAB

This interface can be advantageous in situations


where:

You need to create a large number of different


types of plots,
You must construct plots involving many data
sets,
You want to add annotations such as rectangles
and ellipses, or
You want to change plot characteristics such as
tick spacing, fonts, bolding, italics, and colors.

5-42
The interactive plotting environment in MATLAB is
a set of tools for:

Creating different types of graphs,


Selecting variables to plot directly from the
Workspace Browser,
Creating and editing subplots,
Adding annotations such as lines, arrows, text,
rectangles, and ellipses, and
Editing properties of graphics objects, such as
their color, line weight, and font.

5-43
The Figure and Plot Edit toolbars displayed.

5-45
The Plot Tools interface includes the following
three panels associated with a given figure.

The Figure Palette: Use this to create and


arrange subplots, to view and plot workspace
variables, and to add annotations.
The Plot Browser: Use this to select and control
the visibility of the axes or graphics objects plotted
in the figure, and to add data for plotting.
The Property Editor: Use this to set basic
properties of the selected object and to obtain
access to all properties through the Property
Inspector.

5-46
The Figure window with the Plot Tools
activated..

5-47
Saving your plots
• Rerun your M-file to recreate a plot
• Save the figure from the file menu using the save
as… opton
• You’ll be presented with several choices of file format
such as
• jpeg
• emg (enhanced metafile) etc
• Right-click on the figure and select copy - then
paste it into another document
• >> saveas(gcf,'myFigure.png')

• >>saveas(figure(2),'mfilename3.png')
Summary
• The x-y plot is the most common used in
engineering
•Graphs should always include ttles and axis labels.
Labels should include units.
•MATLAB includes extensive optons for controlling
the appearance of your plot
•Multple plots can be displayed in the same figure
window
•Most common plot types are supported by MATLAB
including
• polar plots
• bar graphs
• pie charts
• histograms
•MATLAB supports 3-D plotng
• line plots
• surface plots
• You can modify plots interactvely from the menu
bar
• You can create plots interactvely from the
workspace window
• Figures created in MATLAB can be stored using a
number of different file formats
User-Defined Functions
Objectives

• Create and use MATLAB functons with both single


and multple inputs and outputs
• Learn how to store and access functons in a user
defined toolbox
• Create and use anonymous functons
• Create and use functon handles
• Create and use subfunctons and nested
subfunctons
Creating Function M-files
•User defined functons are stored as separate M-
files
• To use them, they must be in the current directory
Syntax
• All functons have a similar syntax, whether they
are built-in functons or user-defined functons
• Name
• Input
• Output
A=cos(x)
User-defined functions must start
with a function definition line

• The line contains…


• The word ‘functon’
• A variable that defines the functon output
• A functon name
• A variable used for the input argument

function output = poly(x)


A simple function

The function name must be the same as the


file name
The function is available from
the command window or from
other M-file programs
Function using the Toolbar
Open MATLAB. Open a new Function File.
Press the Save button. This will automatically name the
Function File appropriately.
Type in the equation for the volume of a sphere as shown below.
You must use the input variable name Sphere_Radiusand the
output variable name Sphere_Volumeas specified in the Function
Statement. Make sure to Save the Function File whenever you
make changes to it.
Functions accept a variable as
an input
Hint
While you are creatng a functon it may be useful to
allow intermediate calculatons to print to the
command window. However, once you complete
your “debugging” make sure that all your output is
suppressed. If you don’t, you’ll see extraneous
informaton in the command window.
Hint
Comments
• You should comment functons liberally, just as you
would any computer code
• The comment lines immediately afer the first line
are returned when you query the help functon
Functions with Multiple Inputs
and Outputs
• Recall the remainder functon

This function
has two
inputs
A user defined function with
multiple inputs
Functions with Multiple
Outputs
• Recall the max
functon
• It returns two
results
T

If you don’t ask for all


three results, the
program just
returns the first
value
Recall the size function

At first this
function looks
like it returns
two values -
but it really
only returns a
single array
with two
elements
Functions with no input or no
output
• It isn’t always necessary to define an output
N

When you try to set


the star function
equal to a variable,
an error statement
is returned
Determining the number of
input and output arguments
• nargin
• determines the number of input arguments
• nargout
• determines the number of output arguments
The input to these functions is
represented using a string
You can use these functions in your
programming to make your functions
more versatile

• For example the surf functon accepts a variable


number of arguments
• surf(z) plots the 2-D matrix z against the index
numbers
• surf(x,y,z) plots the 2-D matrix z against the x and y
coordinates
When a variable number of
arguments is allowed…
• nargin returns -1
C
… and the output is
stored in the
workspace

If star1 is set
equal to a
variable, the
rhyme is
returned

You might also like