Lesson 3

Vinnu Vinay
Vinnu VinayNishitha College of Engineering and Technology
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

Recomendados

Matlab plotting von
Matlab plottingMatlab plotting
Matlab plottingpramodkumar1804
1.3K views6 Folien
Graph Plots in Matlab von
Graph Plots in MatlabGraph Plots in Matlab
Graph Plots in MatlabDataminingTools Inc
4.7K views11 Folien
Mat lab von
Mat labMat lab
Mat labSARVJEET SINGH SOHAL
193 views22 Folien
Matlab Graphics Tutorial von
Matlab Graphics TutorialMatlab Graphics Tutorial
Matlab Graphics TutorialCheng-An Yang
1.7K views76 Folien
Matlab ploting von
Matlab plotingMatlab ploting
Matlab plotingAmeen San
3K views19 Folien
Computer Graphics in Java and Scala - Part 1 von
Computer Graphics in Java and Scala - Part 1Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1Philip Schwarz
403 views25 Folien

Más contenido relacionado

Was ist angesagt?

Programming with matlab session 6 von
Programming with matlab session 6Programming with matlab session 6
Programming with matlab session 6Infinity Tech Solutions
12K views38 Folien
Monad presentation scala as a category von
Monad presentation   scala as a categoryMonad presentation   scala as a category
Monad presentation scala as a categorysamthemonad
2K views18 Folien
Matlab dc circuit analysis von
Matlab dc circuit analysisMatlab dc circuit analysis
Matlab dc circuit analysisAmeen San
1.1K views8 Folien
2D Plot Matlab von
2D Plot Matlab2D Plot Matlab
2D Plot MatlabJorge Jasso
2.1K views11 Folien
Introduction to matlab lecture 3 of 4 von
Introduction to matlab lecture 3 of 4Introduction to matlab lecture 3 of 4
Introduction to matlab lecture 3 of 4Randa Elanwar
580 views35 Folien
Matlab quickref von
Matlab quickrefMatlab quickref
Matlab quickrefArduino Aficionado
913 views12 Folien

Was ist angesagt?(19)

Monad presentation scala as a category von samthemonad
Monad presentation   scala as a categoryMonad presentation   scala as a category
Monad presentation scala as a category
samthemonad2K views
Matlab dc circuit analysis von Ameen San
Matlab dc circuit analysisMatlab dc circuit analysis
Matlab dc circuit analysis
Ameen San1.1K views
Introduction to matlab lecture 3 of 4 von Randa Elanwar
Introduction to matlab lecture 3 of 4Introduction to matlab lecture 3 of 4
Introduction to matlab lecture 3 of 4
Randa Elanwar580 views
Monoids - Part 2 - with examples using Scalaz and Cats von Philip Schwarz
Monoids - Part 2 - with examples using Scalaz and CatsMonoids - Part 2 - with examples using Scalaz and Cats
Monoids - Part 2 - with examples using Scalaz and Cats
Philip Schwarz1K views
Introduction to matlab lecture 2 of 4 von Randa Elanwar
Introduction to matlab lecture 2 of 4Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4
Randa Elanwar524 views
Dancing Links: an educational pearl von ESUG
Dancing Links: an educational pearlDancing Links: an educational pearl
Dancing Links: an educational pearl
ESUG187 views
MATLAB for Technical Computing von Naveed Rehman
MATLAB for Technical ComputingMATLAB for Technical Computing
MATLAB for Technical Computing
Naveed Rehman1.3K views
A complete introduction on matlab and matlab's projects von Mukesh Kumar
A complete introduction on matlab and matlab's projectsA complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projects
Mukesh Kumar1.3K views
Bostock and Chandler chapter3 functions von Sarah Sue Calbio
Bostock and Chandler chapter3 functionsBostock and Chandler chapter3 functions
Bostock and Chandler chapter3 functions
Sarah Sue Calbio2.8K views
Introduction to matlab lecture 4 of 4 von Randa Elanwar
Introduction to matlab lecture 4 of 4Introduction to matlab lecture 4 of 4
Introduction to matlab lecture 4 of 4
Randa Elanwar457 views

Similar a Lesson 3

Commands list von
Commands listCommands list
Commands listPRAVEENKUMAR CHIKOTI
287 views60 Folien
1. Introduction.pptx von
1. Introduction.pptx1. Introduction.pptx
1. Introduction.pptxSungaleliYuen
7 views48 Folien
Matlab tut3 von
Matlab tut3Matlab tut3
Matlab tut3Vinnu Vinay
520 views7 Folien
Matlab: Graph Plots von
Matlab: Graph PlotsMatlab: Graph Plots
Matlab: Graph Plotsmatlab Content
4.4K views11 Folien
Statistics lab 1 von
Statistics lab 1Statistics lab 1
Statistics lab 1University of Salerno
305 views19 Folien
MATLABgraphPlotting.pptx von
MATLABgraphPlotting.pptxMATLABgraphPlotting.pptx
MATLABgraphPlotting.pptxPrabhakarSingh646829
5 views21 Folien

Similar a Lesson 3(20)

SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx von agnesdcarey33086
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docxSAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
Matlab plotting von pink1710
Matlab plottingMatlab plotting
Matlab plotting
pink1710202 views
Show working The symmetric hyperbolic Fibonacci sine and cosine func.pdf von kethanraj2
Show working The symmetric hyperbolic Fibonacci sine and cosine func.pdfShow working The symmetric hyperbolic Fibonacci sine and cosine func.pdf
Show working The symmetric hyperbolic Fibonacci sine and cosine func.pdf
kethanraj22 views
More instructions for the lab write-up1) You are not obli.docx von gilpinleeanna
More instructions for the lab write-up1) You are not obli.docxMore instructions for the lab write-up1) You are not obli.docx
More instructions for the lab write-up1) You are not obli.docx
gilpinleeanna4 views
COMPANION TO MATRICES SESSION III.pptx von imman gwu
COMPANION TO MATRICES SESSION III.pptxCOMPANION TO MATRICES SESSION III.pptx
COMPANION TO MATRICES SESSION III.pptx
imman gwu21 views
Matlab-free course by Mohd Esa von Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd Esa
Mohd Esa794 views

Más de Vinnu Vinay

Matlab tut2 von
Matlab tut2Matlab tut2
Matlab tut2Vinnu Vinay
327 views7 Folien
Matlab summary von
Matlab summaryMatlab summary
Matlab summaryVinnu Vinay
421 views8 Folien
Lesson 8 von
Lesson 8Lesson 8
Lesson 8Vinnu Vinay
401 views8 Folien
Lesson 7 von
Lesson 7Lesson 7
Lesson 7Vinnu Vinay
407 views7 Folien
Lesson 6 von
Lesson 6Lesson 6
Lesson 6Vinnu Vinay
463 views8 Folien
Lesson 5 von
Lesson 5Lesson 5
Lesson 5Vinnu Vinay
292 views3 Folien

Último

Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ... von
Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ...Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ...
Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ...ShapeBlue
55 views12 Folien
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ... von
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...Jasper Oosterveld
27 views49 Folien
Uni Systems for Power Platform.pptx von
Uni Systems for Power Platform.pptxUni Systems for Power Platform.pptx
Uni Systems for Power Platform.pptxUni Systems S.M.S.A.
58 views21 Folien
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue von
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlueVNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlueShapeBlue
62 views54 Folien
Data Integrity for Banking and Financial Services von
Data Integrity for Banking and Financial ServicesData Integrity for Banking and Financial Services
Data Integrity for Banking and Financial ServicesPrecisely
29 views26 Folien
HTTP headers that make your website go faster - devs.gent November 2023 von
HTTP headers that make your website go faster - devs.gent November 2023HTTP headers that make your website go faster - devs.gent November 2023
HTTP headers that make your website go faster - devs.gent November 2023Thijs Feryn
26 views151 Folien

Último(20)

Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ... von ShapeBlue
Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ...Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ...
Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ...
ShapeBlue55 views
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ... von Jasper Oosterveld
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue von ShapeBlue
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlueVNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue
ShapeBlue62 views
Data Integrity for Banking and Financial Services von Precisely
Data Integrity for Banking and Financial ServicesData Integrity for Banking and Financial Services
Data Integrity for Banking and Financial Services
Precisely29 views
HTTP headers that make your website go faster - devs.gent November 2023 von Thijs Feryn
HTTP headers that make your website go faster - devs.gent November 2023HTTP headers that make your website go faster - devs.gent November 2023
HTTP headers that make your website go faster - devs.gent November 2023
Thijs Feryn26 views
"Surviving highload with Node.js", Andrii Shumada von Fwdays
"Surviving highload with Node.js", Andrii Shumada "Surviving highload with Node.js", Andrii Shumada
"Surviving highload with Node.js", Andrii Shumada
Fwdays33 views
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlue von ShapeBlue
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlueCloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlue
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlue
ShapeBlue26 views
Business Analyst Series 2023 - Week 3 Session 5 von DianaGray10
Business Analyst Series 2023 -  Week 3 Session 5Business Analyst Series 2023 -  Week 3 Session 5
Business Analyst Series 2023 - Week 3 Session 5
DianaGray10345 views
Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava... von ShapeBlue
Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava...Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava...
Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava...
ShapeBlue28 views
How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ... von ShapeBlue
How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ...How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ...
How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ...
ShapeBlue46 views
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f... von TrustArc
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc72 views
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue von ShapeBlue
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlueCloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue
ShapeBlue25 views
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院 von IttrainingIttraining
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT von ShapeBlue
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBITUpdates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT
ShapeBlue66 views
DRBD Deep Dive - Philipp Reisner - LINBIT von ShapeBlue
DRBD Deep Dive - Philipp Reisner - LINBITDRBD Deep Dive - Philipp Reisner - LINBIT
DRBD Deep Dive - Philipp Reisner - LINBIT
ShapeBlue44 views
KVM Security Groups Under the Hood - Wido den Hollander - Your.Online von ShapeBlue
KVM Security Groups Under the Hood - Wido den Hollander - Your.OnlineKVM Security Groups Under the Hood - Wido den Hollander - Your.Online
KVM Security Groups Under the Hood - Wido den Hollander - Your.Online
ShapeBlue75 views
Business Analyst Series 2023 - Week 4 Session 7 von DianaGray10
Business Analyst Series 2023 -  Week 4 Session 7Business Analyst Series 2023 -  Week 4 Session 7
Business Analyst Series 2023 - Week 4 Session 7
DianaGray1042 views

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