0% found this document useful (0 votes)
21 views94 pages

CSE1121 Chapter4

Uploaded by

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

CSE1121 Chapter4

Uploaded by

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

CSE 1121

Scientific Programming

ENGR 101
Introduction to Programming
Plotting
Two Dimensional Plots
• The x-y plot is the most commonly used plot by
engineers
• The independent variable is usually called x
• The dependent variable is usually called y
Consider this x-y data
time, sec Distance, Ft
0 0 Time is the
2 0.33 independent
variable and
4 4.13
distance is the
6 6.29 dependent
8 6.85 variable
10 11.19
12 13.19
14 13.96
16 16.33
18 18.17
Define x and y and call the plot
function

You can use any variable name that is convenient


for the dependent and independent variables
A graphics window automatically opens
Docking
Arrow

In the default mode the figure


window is free floating, and appears
on top of the MATLAB desktop.

It is often convenient to “dock” the


figure, using the docking arrow
Titles, Labels, and Grids
• Title
• X axis label, complete with units
• Y axis label, complete with units
• You may utilize grids to track your plots
Visual Aid Example

A single quote (‘) starts a string, and it alerts you


with red color

With final single quote (‘), the color changes to


purple
To add an apostrophe to a title (or other
annotation) you must enter the single quote
twice – otherwise MATLAB interprets the
single apostrophe as the end of the string.
Creating 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)
Plots with multiple lines
• 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

• line(x,y, ’Property Name’ , Property Value)


%Another option to add a line to an existing plot
The first plot is drawn in blue
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
You can also create multiple lines on a
single graph with one command

• Using this approach each line defaults to a different


color
Each set of
ordered pairs will
produce a new
line
Variations
• If you use the plot command with a single matrix,
MATLAB plots the values versus the index number
• Usually this type of data is plotted on a bar graph
• When plotted on an x-y grid, it is often called a line
graph
Elements are plottted with
respect to index number
Elements of each column
are plottted with respect to
index number
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)
Alternating sets
of ordered pairs

Matrix of Y
values
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
Line Specifier: Available
choices
Specify your choices in a string
• For example
• plot(x,y,':ok')
• strings are identified with a tick mark
• if you don’t specify style, a default is used
• line style – none
• mark style – none
• color - blue
plot(x,y,':ok')
• In this command
• 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)
dotted line
circles
black
specify the
drawing
parameters for
each line after
the ordered pairs
that define the
line
For more details
>>help plot
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
Use the axis
function to
override the
automatic
scaling
Additional Annotations
• You can also add
• legends
• textbox
• Of course you should always add
• title
• axis labels
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
αβγ
Superscripts and Subscripts
To create a superscript use caret
title(‘x^2’)
gives
x2

To create a subscript use an underscore

title(‘x_2’)
gives
x2

Use curly brackets if you have more than one element in your
sub/superscript

title(‘x^{-255}’)
gives
x-255
Tex Markup Language
• These label improvements use the Tex Markup
Language
• Use the Help feature to find out more!
>>help text properties
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
subplot(2,2,1)
2 columns
Peaks

-5
1 2
2
0 2
0
2 rows y
-2 -2
x

3 4
2 rows and 1
column
Other Types of 2-D Plots
• Polar Plots
• Logarithmic Plots
• Bar Graphs
• Pie Charts
• Histograms
• X-Y graphs with 2 y axes
• Function Plots
Polar Plots
• Some functions are easier to specify using polar
coordinates than by using rectangular coordinates
• For example the equation of a circle is
• y=sin(x)
in polar coordinates
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.
x-y plot – linear on
both axes

semilogx – log scale


on the x axis

semilogy – log scale


on the y axis

loglog – log scale on


both axes
Bar Graphs and Pie Charts
• MATLAB includes a whole family of bar graphs and
pie charts
Histograms
• Useful for statistical analysis

• Plot that shows the distribution 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
• 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.
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
• 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
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
Animations
• try the comet3 function, which draws the graph in
an animation sequence
• comet3(x,y,z)
• If your animation draws too quickly, add more data
points
• For 2-D line graphs use the comet function
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 options
• shading interp
• shading flat
• faceted flat is the default
• You can also adjust the color scheme with the color
map function
Default shading
shading interp
shading flat
Colormaps

autumn bone hot


spring colorcube hsv
summer cool pink
winter copper prism
jet (default) flag white
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
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
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
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
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
Saving your plots
• Rerun your M-file to recreate a plot
• Save the figure from the file menu using the save
as… option
• 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')

You might also like