SlideShare a Scribd company logo
Last modified:January 1, 1970 GMT
An Introduction to Matlab(tm): Lesson 3
New commands in this lesson:
plot(x,y) creates a Cartesian plot of the vectors x & y
plot(y) creates a plot of y vs. the numerical values of the
elements in the y-vector.
semilogx(x,y) plots log(x) vs y
semilogy(x,y) plots x vs log(y)
loglog(x,y)
grid

plots log(x) vs log(y)

creates a grid on the graphics plot

title('text') places a title at top of graphics plot
xlabel('text') writes 'text' beneath the x-axis of a plot
ylabel('text') writes 'text' beside the y-axis of a plot
text(x,y,'text')

writes 'text' at the location (x,y)

text(x,y,'text','sc') writes 'text' at point x,y assuming
lower left corner is (0,0) and upper
right corner is (1,1).
polar(theta,r) creates a polar plot of the vectors r & theta
where theta is in radians.
bar(x)

creates a bar graph of the vector x. (Note also
the command stairs(x).)

bar(x,y) creates a bar-graph of the elements of the vector y,
locating the bars according to the vector elements
of 'x'. (Note also the command stairs(x,y).)

CARTESIAN OR X-Y PLOTS
One of 'matlab' most powerful features is the ability to
create graphic plots. Here we introduce the elementary ideas for
simply presenting a graphic plot of two vectors. More complicated
and powerful ideas with graphics can be found in the 'matlab'
documentation.
A Cartesian or orthogonal x,y plot is based on plotting the
x,y data pairs from the specified vectors. Clearly, the vectors x
and y must have the same number of elements. Imagine that you wish
to plot the function ex for values of x from 0 to 2.
x = 0:.1:2;
y = exp(x);
plot(x,y)
NOTE: The use of selected functions such as exp() and sin() will be
used in these tutorials without explanation if they take on an
unambiguous meaning consistent with past experience. Here it is
observed that operating on a matrix with these functions simply
creates a matrix in which the elements are based on applying the
function to each corresponding element of the argument.
Of course the symbols x,y are arbitrary. If we wanted to plot
temperature on the ordinate and time on the abscissa, and vectors
for temperature and time were loaded in 'matlab', the command would
be
plot(time,temperature)
Notice that the command plot(x,y) opens a graphics window. If you
now execute the command 'grid', the graphics window is redrawn.
(Note you move the cursor to the command window before typing new
commands.) To avoid redrawing the window, you may use the line
continuation ellipsis. Consider
plot(x,y),...
grid,...
title('Exponential Function'),...
xlabel('x'),...
ylabel('exp(x)'),
text(.6,.4,' y = exp(x)','sc')
Note that if you make a mistake in typing a series of lines of
code, such as the above, the use of line continuation can be
frustrating. In a future lesson, we will learn how to create batch
files (called '.m' files) for executing a series of 'matlab'
commands. This approach gives you better opportunity to return to
the command string and edit errors that have been created.
Having defined the vectors x and y, construct semilog and loglog plots using these or other vectors you may wish to create. Note
in particular what occurs when a logarithmic scale is selected for
a vector that has negative or zero elements. Here, the vector x has
a zero element (x(1)=0). The logarithm of zero or any negative
number is undefined and the x,y data pair for which a zero or
negative number occurs is discarded and a warning message provided.
Try the commands plot(x) and plot(y) to ensure you understand
how they differ from plot(x,y).
Notice that 'matlab' draws a straight line between the datapairs. If you desire to see a relatively smooth curve drawn for a
rapidly changing function, you must include a number of data
points. For example, consider the trigonometric function, sin(x1)
(x1 is selected as the argument here to distinguish it from the
vector 'x' that was defined earlier. )
Plot sin(x1) over the limits 0 <= x1 <= pi. First define x1
with 5 elements,
x1 = 0 : pi/4 : pi
y1 = sin(x1)
plot(x1,y1)
Now, repeat this after defining a new vector for x1 and y1 with 21
elements. (Note the use of the semicolon here to prevent the
printing and scrolling on the monitor screen of a vector or matrix
with a large number of elements!)
x1 = 0 : .05*pi : pi ;
y1 =sin(x1);
plot(x1,y1)
POLAR PLOTS
Polar plots are constructed much the same way as are Cartesian
x-y plots; however, the arguments are the angle 'theta' in radians
measured CCW from the horizontal (positive-x) axis, and the length
of the radius vector extended from the origin along this angle.
Polar plots do not allow the labeling of axes; however, notice that
the scale for the radius vector is presented along the vertical and
when the 'grid' command is used the angles-grid is presented in 15-
degree segments. The 'title' command and the 'text' commands are
functional with polar plots.
angle = 0:.1*pi:3*pi;
radius = exp(angle/20);
polar(angle,radius),...
title('An Example Polar Plot'),...
grid
Note that the angles may exceed one revolution of 2*pi.
BAR GRAPHS
To observe how 'matlab' creates a bargraph, return to the
vectors x,y that were defined earlier. Create bar and stair graphs
using these or other vectors you may define. Of course the 'title'
and 'text' commands can be used with bar and stair graphs.
bar(x,y) and bar(y)
stair (x,y) and stair(y)

MULTIPLE PLOTS
More than a single graph can be presented on one graphic plot.
One common way to accomplish this is hold the graphics window open
with the 'hold' command and execute a subsequent plotting command.
x1=0:.05*pi:pi;
y1=sin(x1);
plot(x1,y1)
hold
y2=cos(x1);
plot(x1,y2)
The "hold" command will remain active until you turn it off with
the command 'hold off'.
You can create multiple graphs by using multiple arguments.
In addition to the vectors x,y created earlier, create the vectors
a,b and plot both vector sets simultaneously as follows.
a = 1 : .1 : 3;
b = 10*exp(-a);
plot(x,y,a,b)
Multiple plots can be accomplished also by using matrices
rather than simple vectors in the argument. If the arguments of
the 'plot' command are matrices, the COLUMNS of y are plotted on
the ordinate against the COLUMNS of x on the abscissa. Note that x
and y must be of the same order! If y is a matrix and x is a
vector, the rows or columns of y are plotted against the elements
of x. In this instance, the number of rows OR columns in the matrix
must correspond to the number of elements in 'x'. The matrix 'x'
can be a row or a column vector!
Recall the row vectors 'x' and 'y' defined earlier. Augment
the row vector 'y' to create the 2-row matrix, yy.
yy=[y;exp(1.2*x)];
plot(x,yy)

PLOTTING DATA POINTS & OTHER FANCY STUFF
Matlab connects a straight line between the data pairs
described by the vectors used in the 'print' command. You may wish
to present data points and omit any connecting lines between these
points. Data points can be described by a variety of characters
( . , + , * , o and x .) The following command plots the x,y data
as a "curve" of connected straight lines and in addition places an
'o' character at each of the x1,y1 data pairs.
plot(x,y,x1,y1,'o')
Lines can be colored and they can be broken to make
distinctions among more than one line. Colored lines are effective
on the color monitor and color printers or plotters. Colors are
useless on the common printers used on this network. Colors are
denoted in 'matlab' by the symbols r(red), g(green), b(blue),
w(white) and i(invisible). The following command plots the x,y
data in red solid line and the r,s data in broken green line.
plot(x,y,'r',r,s,'--g')
PRINTING GRAPHIC PLOTS
Executing the 'print' command will send the contents of the
current graphics widow to the local printer.
You may wish to save in a graphics file, a plot you have
created. To do so, simply append to the 'print' command the name
of the file. The command
print filename
will store the contents of the graphics window in the file titled
'filename.ps' in a format called postscript. You need not include
the .ps suffix, as matlab will do this. When you list the files
in your matlab directory, it is convenient to identify any graphics
files by simply looking for the files with the .ps suffix. If
you desire to print one of these postscript files, you can
conveniently "drag and drop" the file from the file-manager window
into the printer icon.
Back to Matlab In-House Tutorials
current graphics widow to the local printer.
You may wish to save in a graphics file, a plot you have
created. To do so, simply append to the 'print' command the name
of the file. The command
print filename
will store the contents of the graphics window in the file titled
'filename.ps' in a format called postscript. You need not include
the .ps suffix, as matlab will do this. When you list the files
in your matlab directory, it is convenient to identify any graphics
files by simply looking for the files with the .ps suffix. If
you desire to print one of these postscript files, you can
conveniently "drag and drop" the file from the file-manager window
into the printer icon.
Back to Matlab In-House Tutorials

More Related Content

PDF
Matlab plotting
PPTX
Graph Plots in Matlab
PDF
Matlab Graphics Tutorial
PPTX
Matlab ploting
PDF
Computer Graphics in Java and Scala - Part 1
PDF
Abstracting over the Monad yielded by a for comprehension and its generators
PDF
Monad Transformers - Part 1
Matlab plotting
Graph Plots in Matlab
Matlab Graphics Tutorial
Matlab ploting
Computer Graphics in Java and Scala - Part 1
Abstracting over the Monad yielded by a for comprehension and its generators
Monad Transformers - Part 1

What's hot (19)

PDF
Programming with matlab session 6
PDF
Monad presentation scala as a category
PPTX
Matlab dc circuit analysis
PPTX
2D Plot Matlab
PPTX
Introduction to matlab lecture 3 of 4
PDF
Matlab quickref
PDF
Monoids - Part 2 - with examples using Scalaz and Cats
PPTX
Introduction to matlab lecture 2 of 4
PPT
Matlab1
PDF
Goldie chapter 4 function
PDF
Dancing Links: an educational pearl
PDF
MATLAB for Technical Computing
PDF
Twopi.1
PDF
A complete introduction on matlab and matlab's projects
PDF
Bostock and Chandler chapter3 functions
PPTX
Mechanical Engineering Homework Help
PPTX
Introduction to matlab lecture 4 of 4
Programming with matlab session 6
Monad presentation scala as a category
Matlab dc circuit analysis
2D Plot Matlab
Introduction to matlab lecture 3 of 4
Matlab quickref
Monoids - Part 2 - with examples using Scalaz and Cats
Introduction to matlab lecture 2 of 4
Matlab1
Goldie chapter 4 function
Dancing Links: an educational pearl
MATLAB for Technical Computing
Twopi.1
A complete introduction on matlab and matlab's projects
Bostock and Chandler chapter3 functions
Mechanical Engineering Homework Help
Introduction to matlab lecture 4 of 4
Ad

Similar to Lesson 3 (20)

PDF
Commands list
PPTX
1. Introduction.pptx
DOC
Matlab tut3
PPTX
Introduction to MATLAB Programming for Engineers
PPTX
Matlab: Graph Plots
PPTX
matlab presentation fro engninering students
PDF
Statistics lab 1
PPTX
MATLABgraphPlotting.pptx
PDF
Introduction to R
DOCX
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
PPTX
Matlab plotting
PDF
Matlab plotting
PPT
WIDI ediot autis dongok part 1.ediot lu lemot lu setan lu
PPT
MatlabIntro1234.ppt.....................
PPTX
lect.no.3.pptx
DOCX
More instructions for the lab write-up1) You are not obli.docx
PPT
MatlabIntro.ppt
PPT
MatlabIntro.ppt
PPT
MatlabIntro.ppt
PPT
MatlabIntro.ppt
Commands list
1. Introduction.pptx
Matlab tut3
Introduction to MATLAB Programming for Engineers
Matlab: Graph Plots
matlab presentation fro engninering students
Statistics lab 1
MATLABgraphPlotting.pptx
Introduction to R
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
Matlab plotting
Matlab plotting
WIDI ediot autis dongok part 1.ediot lu lemot lu setan lu
MatlabIntro1234.ppt.....................
lect.no.3.pptx
More instructions for the lab write-up1) You are not obli.docx
MatlabIntro.ppt
MatlabIntro.ppt
MatlabIntro.ppt
MatlabIntro.ppt
Ad

More from Vinnu Vinay (10)

DOC
Matlab tut2
DOC
Matlab summary
DOC
Lesson 8
DOC
Lesson 7
DOC
Lesson 6
DOC
Lesson 5
DOC
Lesson 4
DOC
Lesson 2
DOC
matlab Lesson 1
DOC
Matlabtut1
Matlab tut2
Matlab summary
Lesson 8
Lesson 7
Lesson 6
Lesson 5
Lesson 4
Lesson 2
matlab Lesson 1
Matlabtut1

Recently uploaded (20)

PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Getting Started with Data Integration: FME Form 101
PDF
A comparative analysis of optical character recognition models for extracting...
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
Web App vs Mobile App What Should You Build First.pdf
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
A comparative study of natural language inference in Swahili using monolingua...
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
project resource management chapter-09.pdf
PPTX
Tartificialntelligence_presentation.pptx
PPTX
A Presentation on Artificial Intelligence
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
A novel scalable deep ensemble learning framework for big data classification...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Getting Started with Data Integration: FME Form 101
A comparative analysis of optical character recognition models for extracting...
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Web App vs Mobile App What Should You Build First.pdf
Group 1 Presentation -Planning and Decision Making .pptx
A comparative study of natural language inference in Swahili using monolingua...
OMC Textile Division Presentation 2021.pptx
Encapsulation_ Review paper, used for researhc scholars
project resource management chapter-09.pdf
Tartificialntelligence_presentation.pptx
A Presentation on Artificial Intelligence
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
Encapsulation theory and applications.pdf
Univ-Connecticut-ChatGPT-Presentaion.pdf
Unlocking AI with Model Context Protocol (MCP)
A novel scalable deep ensemble learning framework for big data classification...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Zenith AI: Advanced Artificial Intelligence
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf

Lesson 3

  • 1. Last modified:January 1, 1970 GMT An Introduction to Matlab(tm): Lesson 3 New commands in this lesson: plot(x,y) creates a Cartesian plot of the vectors x & y plot(y) creates a plot of y vs. the numerical values of the elements in the y-vector. semilogx(x,y) plots log(x) vs y semilogy(x,y) plots x vs log(y) loglog(x,y) grid plots log(x) vs log(y) creates a grid on the graphics plot title('text') places a title at top of graphics plot xlabel('text') writes 'text' beneath the x-axis of a plot ylabel('text') writes 'text' beside the y-axis of a plot text(x,y,'text') writes 'text' at the location (x,y) text(x,y,'text','sc') writes 'text' at point x,y assuming lower left corner is (0,0) and upper right corner is (1,1). polar(theta,r) creates a polar plot of the vectors r & theta where theta is in radians. bar(x) creates a bar graph of the vector x. (Note also the command stairs(x).) bar(x,y) creates a bar-graph of the elements of the vector y, locating the bars according to the vector elements of 'x'. (Note also the command stairs(x,y).) CARTESIAN OR X-Y PLOTS One of 'matlab' most powerful features is the ability to
  • 2. create graphic plots. Here we introduce the elementary ideas for simply presenting a graphic plot of two vectors. More complicated and powerful ideas with graphics can be found in the 'matlab' documentation. A Cartesian or orthogonal x,y plot is based on plotting the x,y data pairs from the specified vectors. Clearly, the vectors x and y must have the same number of elements. Imagine that you wish to plot the function ex for values of x from 0 to 2. x = 0:.1:2; y = exp(x); plot(x,y) NOTE: The use of selected functions such as exp() and sin() will be used in these tutorials without explanation if they take on an unambiguous meaning consistent with past experience. Here it is observed that operating on a matrix with these functions simply creates a matrix in which the elements are based on applying the function to each corresponding element of the argument. Of course the symbols x,y are arbitrary. If we wanted to plot temperature on the ordinate and time on the abscissa, and vectors for temperature and time were loaded in 'matlab', the command would be plot(time,temperature) Notice that the command plot(x,y) opens a graphics window. If you now execute the command 'grid', the graphics window is redrawn. (Note you move the cursor to the command window before typing new commands.) To avoid redrawing the window, you may use the line continuation ellipsis. Consider plot(x,y),... grid,... title('Exponential Function'),... xlabel('x'),... ylabel('exp(x)'), text(.6,.4,' y = exp(x)','sc') Note that if you make a mistake in typing a series of lines of code, such as the above, the use of line continuation can be frustrating. In a future lesson, we will learn how to create batch files (called '.m' files) for executing a series of 'matlab' commands. This approach gives you better opportunity to return to
  • 3. the command string and edit errors that have been created. Having defined the vectors x and y, construct semilog and loglog plots using these or other vectors you may wish to create. Note in particular what occurs when a logarithmic scale is selected for a vector that has negative or zero elements. Here, the vector x has a zero element (x(1)=0). The logarithm of zero or any negative number is undefined and the x,y data pair for which a zero or negative number occurs is discarded and a warning message provided. Try the commands plot(x) and plot(y) to ensure you understand how they differ from plot(x,y). Notice that 'matlab' draws a straight line between the datapairs. If you desire to see a relatively smooth curve drawn for a rapidly changing function, you must include a number of data points. For example, consider the trigonometric function, sin(x1) (x1 is selected as the argument here to distinguish it from the vector 'x' that was defined earlier. ) Plot sin(x1) over the limits 0 <= x1 <= pi. First define x1 with 5 elements, x1 = 0 : pi/4 : pi y1 = sin(x1) plot(x1,y1) Now, repeat this after defining a new vector for x1 and y1 with 21 elements. (Note the use of the semicolon here to prevent the printing and scrolling on the monitor screen of a vector or matrix with a large number of elements!) x1 = 0 : .05*pi : pi ; y1 =sin(x1); plot(x1,y1) POLAR PLOTS Polar plots are constructed much the same way as are Cartesian x-y plots; however, the arguments are the angle 'theta' in radians measured CCW from the horizontal (positive-x) axis, and the length of the radius vector extended from the origin along this angle. Polar plots do not allow the labeling of axes; however, notice that the scale for the radius vector is presented along the vertical and when the 'grid' command is used the angles-grid is presented in 15-
  • 4. degree segments. The 'title' command and the 'text' commands are functional with polar plots. angle = 0:.1*pi:3*pi; radius = exp(angle/20); polar(angle,radius),... title('An Example Polar Plot'),... grid Note that the angles may exceed one revolution of 2*pi. BAR GRAPHS To observe how 'matlab' creates a bargraph, return to the vectors x,y that were defined earlier. Create bar and stair graphs using these or other vectors you may define. Of course the 'title' and 'text' commands can be used with bar and stair graphs. bar(x,y) and bar(y) stair (x,y) and stair(y) MULTIPLE PLOTS More than a single graph can be presented on one graphic plot. One common way to accomplish this is hold the graphics window open with the 'hold' command and execute a subsequent plotting command. x1=0:.05*pi:pi; y1=sin(x1); plot(x1,y1) hold y2=cos(x1); plot(x1,y2) The "hold" command will remain active until you turn it off with the command 'hold off'. You can create multiple graphs by using multiple arguments. In addition to the vectors x,y created earlier, create the vectors a,b and plot both vector sets simultaneously as follows. a = 1 : .1 : 3; b = 10*exp(-a);
  • 5. plot(x,y,a,b) Multiple plots can be accomplished also by using matrices rather than simple vectors in the argument. If the arguments of the 'plot' command are matrices, the COLUMNS of y are plotted on the ordinate against the COLUMNS of x on the abscissa. Note that x and y must be of the same order! If y is a matrix and x is a vector, the rows or columns of y are plotted against the elements of x. In this instance, the number of rows OR columns in the matrix must correspond to the number of elements in 'x'. The matrix 'x' can be a row or a column vector! Recall the row vectors 'x' and 'y' defined earlier. Augment the row vector 'y' to create the 2-row matrix, yy. yy=[y;exp(1.2*x)]; plot(x,yy) PLOTTING DATA POINTS & OTHER FANCY STUFF Matlab connects a straight line between the data pairs described by the vectors used in the 'print' command. You may wish to present data points and omit any connecting lines between these points. Data points can be described by a variety of characters ( . , + , * , o and x .) The following command plots the x,y data as a "curve" of connected straight lines and in addition places an 'o' character at each of the x1,y1 data pairs. plot(x,y,x1,y1,'o') Lines can be colored and they can be broken to make distinctions among more than one line. Colored lines are effective on the color monitor and color printers or plotters. Colors are useless on the common printers used on this network. Colors are denoted in 'matlab' by the symbols r(red), g(green), b(blue), w(white) and i(invisible). The following command plots the x,y data in red solid line and the r,s data in broken green line. plot(x,y,'r',r,s,'--g') PRINTING GRAPHIC PLOTS Executing the 'print' command will send the contents of the
  • 6. current graphics widow to the local printer. You may wish to save in a graphics file, a plot you have created. To do so, simply append to the 'print' command the name of the file. The command print filename will store the contents of the graphics window in the file titled 'filename.ps' in a format called postscript. You need not include the .ps suffix, as matlab will do this. When you list the files in your matlab directory, it is convenient to identify any graphics files by simply looking for the files with the .ps suffix. If you desire to print one of these postscript files, you can conveniently "drag and drop" the file from the file-manager window into the printer icon. Back to Matlab In-House Tutorials
  • 7. current graphics widow to the local printer. You may wish to save in a graphics file, a plot you have created. To do so, simply append to the 'print' command the name of the file. The command print filename will store the contents of the graphics window in the file titled 'filename.ps' in a format called postscript. You need not include the .ps suffix, as matlab will do this. When you list the files in your matlab directory, it is convenient to identify any graphics files by simply looking for the files with the .ps suffix. If you desire to print one of these postscript files, you can conveniently "drag and drop" the file from the file-manager window into the printer icon. Back to Matlab In-House Tutorials