0% found this document useful (0 votes)
26 views52 pages

Two-Dimensional Plots

Uploaded by

puskass101
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)
26 views52 pages

Two-Dimensional Plots

Uploaded by

puskass101
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/ 52

Basic

Computer
Programming
Dr. Deniz Kutluay
Two-Dimensional Plots

• THE plot COMMAND


• THE fplot COMMAND
• PLOTTING MULTIPLE GRAPHS IN THE SAME PLOT
• FORMATTING A PLOT
• PLOTS WITH LOGARITHMIC AXES
• PLOTS WITH ERROR BARS
• PLOTS WITH SPECIAL GRAPHICS
• HISTOGRAMS
• POLAR PLOTS
• PUTTING MULTIPLE PLOTS ON THE SAME PAGE
Mission & Goals
Mission
Listen to me carefully, and practice some examples using Matlab by following me

Goal #1 Goal #2

To learn plotting To learn how to plot


figures in detail. special graphics,
histograms, polar
plots.
Basic Computer Programming Dr. Deniz Kutluay

Two-Dimensional
Plots
Plots are a very useful tool for presenting
information

MATLAB has many commands that can be


used for creating different types of plots.

These include standard plots with linear


axes, plots with logarithmic and semi-
logarithmic axes, bar and stairs plots,
polar plots, three-dimensional contour
surface and mesh plots, and many more.
001
Basic Computer Programming Dr. Deniz Kutluay

The plot command


The plot command is used to create two-
dimensional plots.

The arguments x and y are each a vector


(one-dimensional array).

When the plot command is executed,


a figure is created in the Figure Window.
The two vectors must have the same number of elements.

002
Basic Computer Programming Dr. Deniz Kutluay

The plot command


The vector that is typed first in the plot
command is used for the horizontal axis,
and the vector that is typed second is
used for the vertical axis.

For example, if a vector x has the


elements
1, 2, 3, 5, 7, 7.5, 8, 10,
and a vector y has the elements
2, 6.5, 7, 7, 5.5, 4, 6, 8,
a simple plot of y versus x can be created
by typing the following in the Command
Window:
003
Basic Computer Programming Dr. Deniz Kutluay

The plot command

The plot command has additional,


optional arguments that can be used to
specify the color and style of the line and
the color and type of markers.

Line Specifiers:
The line style specifiers are:
Line specifiers are optional and can be
used to define the style and color of the
line and the type of markers.

004
Basic Computer Programming Dr. Deniz Kutluay

The plot command


The line color specifiers are:

Line Specifiers:
The marker type specifiers are:
Line specifiers are optional and can be
used to define the style and color of the
line and the type of markers.

005
Basic Computer Programming Dr. Deniz Kutluay

The plot command


Line Specifiers:

006
Basic Computer Programming Dr. Deniz Kutluay

The plot command


Property Name and Property Value: Four properties and their possible values are:

Properties are optional and can be used to


specify the thickness of the line, the size
of the marker, and the colors of the
marker's edge line and fill.

The Property Name is typed as a string,


followed by a comma and a value for the
property, all inside the plot command.

007
Basic Computer Programming Dr. Deniz Kutluay

The plot command


Property Name and Property Value: Four properties and their possible values are:

For example, the command

creates a plot that connects the points with


a magenta solid line and circles as markers
at the points.

The line width is 2 points and the size of


the circle markers is 12 points. The markers
have a green edge line and yellow filling. 008
Basic Computer Programming Dr. Deniz Kutluay

The plot command

Property Name and Property Value: The Property Names for the line specifiers are:

The three line specifiers, which indicate


the style and color of the line, and the
type of the marker can also be assigned
with a PropertyName argument followed
by a PropertyValue argument.

009
Basic Computer Programming Dr. Deniz Kutluay

The plot command

Plot of Given Data

In this case given data is used to create


vectors that are then used in the plot
command.

To plot this data, the list of years is


assigned to one vector (named yr), and
the corresponding sales data is assigned
to a second vector (named sle).

010
Basic Computer Programming Dr. Deniz Kutluay

The plot command


Plot of a Function:

This can be done in MATLAB by using the


plot or the fplot command.

As an example, the plot command is used


to plot the function

011
Basic Computer Programming Dr. Deniz Kutluay

The plot command


Plot of a Function:

Since the plot is made up of segments of


straight lines that connect the points, to
obtain an accurate plot of a function, the
spacing between the elements of the
vector x must be appropriate.

012
Basic Computer Programming Dr. Deniz Kutluay

The fplot command

The fplot command plots a function with The command has the form:
the form y = f(x) between specified
limits.

'function': The function can be typed


directly as a string inside the command.

013
Basic Computer Programming Dr. Deniz Kutluay

The fplot command


'limits': The limits argument is a vector
with two elements that specify the
domain of x [ xmin, xmax ], or a vector
with four elements that specifies the
domain of x and the limits of the y-axis
[xmin, xmax, ymin, ymax].

'line specifiers': The line specifiers are the


same as in the plot command.

014
Basic Computer Programming Dr. Deniz Kutluay

Plotting multiple
graphs in the same
plot Using the plot Command:

Two or more graphs can be created in the same plot


by typing pairs of vectors inside the plot command.
There are three methods to plot multiple
graphs in one figure. One is by using
the plot command, the second is by using
the hold on and hold off commands,
and the third is by using the line creates three graphs y vs. x, v vs. u, and h vs. t all in
command. the same plot.

 The vectors of each pair must be of the same


length.
015
Sample Problem 5.1:
Plotting a function and its
derivatives

Plot the function

and its first and second

derivatives, for ,

all in the same plot.


Solution

Sample Problem 5.1: The first derivative of the function is:

Plotting a function and its


derivatives
The second derivative of the function is:
Plot the function

and its first and second

derivatives, for ,

all in the same plot.


Solution

Sample Problem 5.1: A plot of the function y = 3x3- 26x + 10 and


Plotting a function and its its first and second derivatives.

derivatives

Plot the function

and its first and second

derivatives, for ,

all in the same plot.


Basic Computer Programming Dr. Deniz Kutluay

Plotting multiple
graphs in the same
plot
Using the hold on and hold off Commands

To plot several graphs using the hold on


and hold off commands, one graph
is plotted first with the plot command.

Then the hold on command is typed.


Additional graphs can be added with plot
commands that are typed next.

The hold off command stops this process.


016
Basic Computer Programming Dr. Deniz Kutluay

Plotting multiple
graphs in the same
plot
Using the line Command
The form of the line command is:
With the line command additional graphs
(lines) can be added to a plot that
already exists.

The format of the line command is almost


the same as the plot command. The line
command does not have the line
specifiers, but the line style, color, and
marker can be specified with the Property
Name and Property Value features. 017
Basic Computer Programming Dr. Deniz Kutluay

Plotting multiple
graphs in the same
plot
Using the line Command

The major difference between the plot


and line commands is that the plot
command starts a new plot every time it is
executed, while the line command adds
lines to a plot that already exists.

To make a plot that has several graphs, a


plot command is typed first and then line
commands are typed for additional
graphs. 018
Basic Computer Programming Dr. Deniz Kutluay

Formatting a plot
The formatting commands are entered
after the plot or the fplot command.

The xlabel and ylabel commands: The title command:

Labels can be placed next to the axes with A title can be added to the plot with the
the xlabel and ylabel command which command:
have the form:

019
Basic Computer Programming Dr. Deniz Kutluay

Formatting a plot
The formatting commands are entered
after the plot or the fplot command.
The pos is an optional number that specifies where
in the figure the legend is to be placed.
The legend command:

The legend command places a legend on


the plot. The form of the command is:

The strings are the labels that are placed


next to the line sample.

020
Basic Computer Programming Dr. Deniz Kutluay

Formatting a plot
The text command:

The text command places the text in the


figure such that the first character is A text label can be placed in the plot with
positioned at the point with the the text or gtext commands:
coordinates x, y (according to the axes of
the figure).

The gtext command places the text at a


position specified by the user.

When the command is executed, the


Figure Window opens and the user
specifies the position with the mouse. 021
Basic Computer Programming Dr. Deniz Kutluay

Formatting a plot

Formatting the text within the xlabel,


Some of the modifiers that can be added are:
ylabel, title, text and legend commands

The formatting can be used to defme the


font, size, position (superscript, subscript),
style (italic, bold, etc.), and color of the
characters, the color of the background,
and many other details of the display.

The modifiers are characters that are inserted


within the string.

022
Basic Computer Programming Dr. Deniz Kutluay

Formatting a plot
Greek characters:
Subscript and superscript:
Greek characters can be included in the text by
A single character can be displayed as a typing \name of the letter within the string.
subscript or a superscript by typing _ (the
underscore character) or ^ in front of the
character, respectively.

Several consecutive characters can be


displayed as a subscript or a superscript by
typing the characters inside braces { }
following the _ or the ^.

023
Basic Computer Programming Dr. Deniz Kutluay

Formatting a plot

The text command, for example, has the form:


Formatting of the text that is displayed by
the xlabel, ylabel, title, and text
commands can also be done by adding
optional PropertyName and PropertyValue
arguments following the string inside the
command. In the other three commands the PropertyName
and PropertyValue arguments are added in the
same way.

024
Basic Computer Programming Dr. Deniz Kutluay

Formatting a plot

Some of the Property Names and


corresponding possible Property Values
are:

025
Basic Computer Programming Dr. Deniz Kutluay

Formatting a plot
The axis command:

The axis command can be used to


change the range and the appearance
of the axes.

In many situations, a graph looks


better if the range of the axes extend
beyond the range of the data.

026
Basic Computer Programming Dr. Deniz Kutluay

Formatting a plot
The grid command:

027
Basic Computer Programming Dr. Deniz Kutluay

Formatting a plot

Formatting a Plot Using the Plot Editor

A plot can be formatted interactively in


the Figure Window by clicking on the
plot and/or using the menus.

The Plot Editor can be used to


introduce new formatting items or to
modify formatting that was initially
introduced with the formatting
commands.

028
Basic Computer Programming Dr. Deniz Kutluay

Plots with
logarithmic axes
Log scales provide means for presenting
data over a wide range of values.

It also provides a tool for identifying


characteristics of data and possible
forms of mathematical relationships.

Line specifiers and Property Name and


Property Value arguments can be added
to the commands (optional) just as in
the plot command.

029
Basic Computer Programming Dr. Deniz Kutluay

Plots with
logarithmic axes
The figure shows four plots of the same
function:

 one with linear axes,

 one with a log scale for the y axis,

 one with a log scale for the x axis,

 one with a log scale on both axes. 030


Basic Computer Programming Dr. Deniz Kutluay

Plots with error


bars When the error is symmetric, the error bar extends
An error bar is typically a short vertical the same length above and below the data point,
line that is attached to a data point in a and the command has the form:
plot.

It shows the magnitude of the error


that is associated with the value that is
displayed by the data point.
• The lengths of the three vectors x, y, and e must
Plots with error bars can be done in be the same.
MATLAB with the errorbar command. • The length of the error bar is twice the value of e.
At each point the error bar extends from y(i) - e(i)
to y(i) + e(i).

031
Basic Computer Programming Dr. Deniz Kutluay

Plots with error


bars

032
Basic Computer Programming Dr. Deniz Kutluay

Plots with error


bars
The command for making a plot with
error bars that are not symmetric is:

• The lengths of the four vectors x, y,


d, and u must be the same.

• At each point the error bar extends


from y(i) - d(i) to y(i) + u(i).

033
Basic Computer Programming Dr. Deniz Kutluay

Plots with special


graphics
All the plots that have been presented
so far in this chapter are line plots in
which the data points are connected by
lines.

In many situations plots with different


graphics or geometry can present data
more effectively.

These include bar, stairs, stem, and pie


plots and many more.

034
Basic Computer Programming Dr. Deniz Kutluay

Plots with special


graphics
All the plots that have been presented
so far in this chapter are line plots in
which the data points are connected by
lines.

In many situations plots with different


graphics or geometry can present data
more effectively.

These include bar, stairs, stem, and pie


plots and many more.

035
Basic Computer Programming Dr. Deniz Kutluay

Plots with special


graphics

Pie charts are useful for visualizing the


relative sizes of different but related
quantities.

For example, the table below shows the


grades that were assigned to a class.

036
Basic Computer Programming Dr. Deniz Kutluay

Histograms

Histograms are plots that show the


distribution of data.

The histogram is a vertical bar plot in


which the width of each bar is equal to
the range of the corresponding bin and
the height of the bar corresponds to the
number of data points in the bin.

Histograms are created in MATLAB with


the hist command.

037
Basic Computer Programming Dr. Deniz Kutluay

Histograms

For example, the following data points


are the daily maximum temperature
(in °F) in Washington, DC.

038
Basic Computer Programming Dr. Deniz Kutluay

Polar plots

Polar coordinates are frequently used in


the solution of science and engineering
problems.

The polar command is used to plot


functions in polar coordinates.

The polar command plots the points


and draws the polar grid.

The line specifiers are the same as in where theta and radius are vectors whose elements
define the coordinates of the points to be plotted.
the plot command.
039
Basic Computer Programming Dr. Deniz Kutluay

Polar plots

For example, a plot of the function

is shown here

040
Basic Computer Programming Dr. Deniz Kutluay

Putting multiple plots


on the same page
Multiple plots can be created on the same
page with the subplot command

The command divides the Figure Window


into m x n rectangular subplots.

For example, the command subplot (3,2,1)


creates six areas arranged in three rows
and two columns as shown, and makes the
upper left subplot current.

041
Sample Problem 5.2:

Piston-crank mechanism

The piston-rod-crank mechanism is


used in many engineering
applications. In the mechanism
shown in the following figure, the Calculate and plot the position, velocity, and
crank is rotating at a constant acceleration of the piston for one revolution of
speed of 500 rpm. the crank. Make the three plots on the same
page. Set θ = 0o when t = 0.
Solution

Sample Problem 5.2:

Piston-crank mechanism

The piston-rod-crank mechanism is


used in many engineering
applications. In the mechanism
shown in the following figure, the
crank is rotating at a constant
speed of 500 rpm.
Solution

Sample Problem 5.2:

Piston-crank mechanism

The piston-rod-crank mechanism is


used in many engineering
applications. In the mechanism
shown in the following figure, the
crank is rotating at a constant
speed of 500 rpm.
Solution

Sample Problem 5.2:

Piston-crank mechanism

The piston-rod-crank mechanism is


used in many engineering
applications. In the mechanism
shown in the following figure, the
crank is rotating at a constant
speed of 500 rpm.

Position, velocity, and acceleration of the piston vs. time.


Thank
You

Dr. Deniz Kutluay


Electrical-Electronics Engineering

Design Architect at Vestel


[email protected]
[email protected]

You might also like