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

What's hot

Programming with matlab session 6
Programming with matlab session 6Programming with matlab session 6
Programming with matlab session 6
Infinity Tech Solutions
 
Monad presentation scala as a category
Monad presentation   scala as a categoryMonad presentation   scala as a category
Monad presentation scala as a categorysamthemonad
 
Matlab dc circuit analysis
Matlab dc circuit analysisMatlab dc circuit analysis
Matlab dc circuit analysis
Ameen San
 
2D Plot Matlab
2D Plot Matlab2D Plot Matlab
2D Plot Matlab
Jorge Jasso
 
Introduction to matlab lecture 3 of 4
Introduction to matlab lecture 3 of 4Introduction to matlab lecture 3 of 4
Introduction to matlab lecture 3 of 4
Randa Elanwar
 
Matlab quickref
Matlab quickrefMatlab quickref
Matlab quickref
Arduino Aficionado
 
Monoids - Part 2 - with examples using Scalaz and Cats
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 Schwarz
 
Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4
Randa Elanwar
 
Goldie chapter 4 function
Goldie chapter 4 functionGoldie chapter 4 function
Goldie chapter 4 function
Sarah Sue Calbio
 
Dancing Links: an educational pearl
Dancing Links: an educational pearlDancing Links: an educational pearl
Dancing Links: an educational pearl
ESUG
 
MATLAB for Technical Computing
MATLAB for Technical ComputingMATLAB for Technical Computing
MATLAB for Technical Computing
Naveed Rehman
 
Lec4
Lec4Lec4
A complete introduction on matlab and matlab's projects
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 Kumar
 
Bostock and Chandler chapter3 functions
Bostock and Chandler chapter3 functionsBostock and Chandler chapter3 functions
Bostock and Chandler chapter3 functions
Sarah Sue Calbio
 
Mechanical Engineering Homework Help
Mechanical Engineering Homework HelpMechanical Engineering Homework Help
Mechanical Engineering Homework Help
Matlab Assignment Experts
 
Lec5
Lec5Lec5
Introduction to matlab lecture 4 of 4
Introduction to matlab lecture 4 of 4Introduction to matlab lecture 4 of 4
Introduction to matlab lecture 4 of 4
Randa Elanwar
 

What's hot (19)

Programming with matlab session 6
Programming with matlab session 6Programming with matlab session 6
Programming with matlab session 6
 
Monad presentation scala as a category
Monad presentation   scala as a categoryMonad presentation   scala as a category
Monad presentation scala as a category
 
Matlab dc circuit analysis
Matlab dc circuit analysisMatlab dc circuit analysis
Matlab dc circuit analysis
 
2D Plot Matlab
2D Plot Matlab2D Plot Matlab
2D Plot Matlab
 
Introduction to matlab lecture 3 of 4
Introduction to matlab lecture 3 of 4Introduction to matlab lecture 3 of 4
Introduction to matlab lecture 3 of 4
 
Matlab quickref
Matlab quickrefMatlab quickref
Matlab quickref
 
Monoids - Part 2 - with examples using Scalaz and Cats
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
 
Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4
 
Matlab1
Matlab1Matlab1
Matlab1
 
Goldie chapter 4 function
Goldie chapter 4 functionGoldie chapter 4 function
Goldie chapter 4 function
 
Dancing Links: an educational pearl
Dancing Links: an educational pearlDancing Links: an educational pearl
Dancing Links: an educational pearl
 
MATLAB for Technical Computing
MATLAB for Technical ComputingMATLAB for Technical Computing
MATLAB for Technical Computing
 
Twopi.1
Twopi.1Twopi.1
Twopi.1
 
Lec4
Lec4Lec4
Lec4
 
A complete introduction on matlab and matlab's projects
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
 
Bostock and Chandler chapter3 functions
Bostock and Chandler chapter3 functionsBostock and Chandler chapter3 functions
Bostock and Chandler chapter3 functions
 
Mechanical Engineering Homework Help
Mechanical Engineering Homework HelpMechanical Engineering Homework Help
Mechanical Engineering Homework Help
 
Lec5
Lec5Lec5
Lec5
 
Introduction to matlab lecture 4 of 4
Introduction to matlab lecture 4 of 4Introduction to matlab lecture 4 of 4
Introduction to matlab lecture 4 of 4
 

Similar to Lesson 3

Commands list
Commands listCommands list
Commands list
PRAVEENKUMAR CHIKOTI
 
1. Introduction.pptx
1. Introduction.pptx1. Introduction.pptx
1. Introduction.pptx
SungaleliYuen
 
Matlab: Graph Plots
Matlab: Graph PlotsMatlab: Graph Plots
Matlab: Graph Plots
matlab Content
 
Statistics lab 1
Statistics lab 1Statistics lab 1
Statistics lab 1
University of Salerno
 
MATLABgraphPlotting.pptx
MATLABgraphPlotting.pptxMATLABgraphPlotting.pptx
MATLABgraphPlotting.pptx
PrabhakarSingh646829
 
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
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
agnesdcarey33086
 
Matlab plotting
Matlab plottingMatlab plotting
Matlab plotting
shahid sultan
 
Matlab plotting
Matlab plottingMatlab plotting
Matlab plotting
pink1710
 
lect.no.3.pptx
lect.no.3.pptxlect.no.3.pptx
lect.no.3.pptx
ahmed343312
 
More instructions for the lab write-up1) You are not obli.docx
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
gilpinleeanna
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
ssuser772830
 
COMPANION TO MATRICES SESSION III.pptx
COMPANION TO MATRICES SESSION III.pptxCOMPANION TO MATRICES SESSION III.pptx
COMPANION TO MATRICES SESSION III.pptx
imman gwu
 
Matlab graphics
Matlab graphicsMatlab graphics
Matlab graphics
pramodkumar1804
 
Matlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd Esa
Mohd Esa
 

Similar to Lesson 3 (20)

Commands list
Commands listCommands list
Commands list
 
1. Introduction.pptx
1. Introduction.pptx1. Introduction.pptx
1. Introduction.pptx
 
Matlab tut3
Matlab tut3Matlab tut3
Matlab tut3
 
Matlab: Graph Plots
Matlab: Graph PlotsMatlab: Graph Plots
Matlab: Graph Plots
 
Statistics lab 1
Statistics lab 1Statistics lab 1
Statistics lab 1
 
MATLABgraphPlotting.pptx
MATLABgraphPlotting.pptxMATLABgraphPlotting.pptx
MATLABgraphPlotting.pptx
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
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
Matlab plottingMatlab plotting
Matlab plotting
 
Matlab plotting
Matlab plottingMatlab plotting
Matlab plotting
 
lect.no.3.pptx
lect.no.3.pptxlect.no.3.pptx
lect.no.3.pptx
 
More instructions for the lab write-up1) You are not obli.docx
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
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 
Matlab intro
Matlab introMatlab intro
Matlab intro
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 
COMPANION TO MATRICES SESSION III.pptx
COMPANION TO MATRICES SESSION III.pptxCOMPANION TO MATRICES SESSION III.pptx
COMPANION TO MATRICES SESSION III.pptx
 
Matlab graphics
Matlab graphicsMatlab graphics
Matlab graphics
 
Matlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd Esa
 

More from Vinnu Vinay

matlab Lesson 1
matlab Lesson 1matlab Lesson 1
matlab Lesson 1
Vinnu Vinay
 

More from Vinnu Vinay (10)

Matlab tut2
Matlab tut2Matlab tut2
Matlab tut2
 
Matlab summary
Matlab summaryMatlab summary
Matlab summary
 
Lesson 8
Lesson 8Lesson 8
Lesson 8
 
Lesson 7
Lesson 7Lesson 7
Lesson 7
 
Lesson 6
Lesson 6Lesson 6
Lesson 6
 
Lesson 5
Lesson 5Lesson 5
Lesson 5
 
Lesson 4
Lesson 4Lesson 4
Lesson 4
 
Lesson 2
Lesson 2Lesson 2
Lesson 2
 
matlab Lesson 1
matlab Lesson 1matlab Lesson 1
matlab Lesson 1
 
Matlabtut1
Matlabtut1Matlabtut1
Matlabtut1
 

Recently uploaded

Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 

Recently uploaded (20)

Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 

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