Chapter 3: MATLAB Fundamentals

advertisement
Introduction to MATLAB
Chapter 3
MATLAB Fundamentals
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
MATLAB INTERFACE
Engineering Computation: An Introduction Using MATLAB and Excel
Command Window
This is the command prompt –
MATLAB commands are entered
here
Engineering Computation: An Introduction Using MATLAB and Excel
Command Window
• MATLAB commands enter at the prompts can be
divided into two categories:
– Interactive commands, in which we directly enter the
instructions into the command window, and
– File commands, in which we call up a series of
commands stored in a MATLAB file
• The interactive mode is useful for quick
computations, testing commands, or called up
stored values
• More complex command sequences are stored in
files for easy retrieval
Engineering Computation: An Introduction Using MATLAB and Excel
Interactive Commands
• MATLAB can be used like a calculator, returning
the answer for mathematical expressions:
• Note that MATLAB rounded the answer of the
second calculation to 5 digits
Engineering Computation: An Introduction Using MATLAB and Excel
Number Format
• By default, MATLAB uses a “short” format to display
results. Entering the command format long causes all
stored digits to be displayed:
• The command format short switches the output back to the
default format
• More specific formats can be specified, but the default
(short) format is sufficient for most calculations
Engineering Computation: An Introduction Using MATLAB and Excel
Variables
• In the previous examples, the answer was stored
in MATLAB with the variable name ans
• Often, we want to store our input and output
values with our own variable names
• Let’s calculate the volume of a sphere. We will call
the diameter of the sphere dia:
Engineering Computation: An Introduction Using MATLAB and Excel
Variables
• When the expression is entered, the value of the
variable is echoed to the screen:
• Note that here the “=“ sign is called an assignment
operator
• The variable (dia) is assigned a value of 3
Engineering Computation: An Introduction Using MATLAB and Excel
Variables
• Now let’s assign a new variable Vol to the value
calculated from the volume formula:
• Again, the variable’s value is printed to the screen
Engineering Computation: An Introduction Using MATLAB and Excel
Workspace Window
• The Workspace Window shows all currently
assigned variables, and their current values
• Note the columns for Min and Max: MATLAB is
especially good for working with arrays, and treats
a scalar as a 1 X 1 array
Engineering Computation: An Introduction Using MATLAB and Excel
Command History Window
• Your most recent entries to the Command Window
are stored in the Command History Window,
making it easy to retrace your steps
Engineering Computation: An Introduction Using MATLAB and Excel
Changing a Variable
• Let’s change the value of the diameter of the sphere to
6 inches:
• Note that the value of Vol has not changed. Unlike a
spreadsheet, where all cells containing formulas
update automatically, in a programming language the
variables are not reassigned unless we issue
instructions to reassign them
Engineering Computation: An Introduction Using MATLAB and Excel
Updating the Volume
• We could re-enter the formula for Vol, but here is a
handy shortcut:
• From the command prompt, clicking the up arrow
key allows you to scroll through the most recent
entries. Two clicks of the key displays the
assignment of Vol, and pressing Enter reassigns its
value.
Engineering Computation: An Introduction Using MATLAB and Excel
Suppressing Screen Display
• At the end of a command, adding a semicolon
causes the display to the screen to be skipped:
• Semi-colons are entered at the ends of most lines
in MATLAB files, since we usually don’t want
intermediate results displayed to the screen
Engineering Computation: An Introduction Using MATLAB and Excel
Displaying Variable Values
• Entering a variable name at the command prompt
displays its value to the screen:
Engineering Computation: An Introduction Using MATLAB and Excel
Variable Names
• MATLAB variable name are case sensitive! This is
very important to remember. Suppose we enter
the volume formula as:
• We get an error, since MATLAB does not recognize
Dia. If fact, we could assign different variables the
names dia, DIA, Dia, etc., and MATLAB would treat
them as unrelated variables
Engineering Computation: An Introduction Using MATLAB and Excel
Clearing Variables
• To clear the value of a variable from memory, use
the “clear” command:
• To clear all values, use “clear all”
• Clearing variables is a good idea at the beginning
of programs to ensure that results from previous
calculations are not used accidentally (especially
with arrays - more on this later)
Engineering Computation: An Introduction Using MATLAB and Excel
Hierarchy of Operations
• Note that our equation for volume required
parentheses only around “dia/2”:
• The exponent operation is done first. Therefore,
without the parentheses, the number 2 would be
cubed first.
• MATLAB, like Excel and other computation software,
performs calculations in this order: exponentials;
multiplication/division; addition/subtraction
Engineering Computation: An Introduction Using MATLAB and Excel
Formulas in MATLAB
• Adding parentheses, even when not required, can
often help you to organize formulas
• Spaces are ignored, and can also be used to make
formulas more readable
Engineering Computation: An Introduction Using MATLAB and Excel
Functions in MATLAB
• There are many built-in functions in MATLAB. As
an example, consider sin:
• The value in parentheses is the argument of the
function; many functions required multiple
arguments, separated by commas
Engineering Computation: An Introduction Using MATLAB and Excel
Help With Functions
• To learn more about a function, type “help” followed by the
function name:
• From the “see also” section, we discover that there is a
function called sind, which finds the sine of an angle that is
input in degrees rather than radians
Engineering Computation: An Introduction Using MATLAB and Excel
More Help
• You can type “help” at the command prompt to call
up a menu of help topics
• Also, clicking the Help button opens a window
where you can browse topics or search for
particular keywords
Engineering Computation: An Introduction Using MATLAB and Excel
MATLAB Files
• The interactive mode is good for quick
calculations, but frequently used and/or complex
algorithms are stored in files
• MATLAB uses a file extension of .m, and MATLAB
files are often called “m-files”
• Before getting started with m-files, it is important
to consider the locations where your files will be
stored
Engineering Computation: An Introduction Using MATLAB and Excel
Current Directory
• At the top of the screen is the
name of the current directory,
where by default your new
m-files will be stored.
• A list of MATLAB files in the
current directory can be
displayed by clicking the
“Current Directory” tab (this
window can be toggled
between the Workspace and
Current Directory)
Engineering Computation: An Introduction Using MATLAB and Excel
Current Directory
• You may want to store your MATLAB files for each
class or project in a specific folder
• If so, create the directory in Windows and then
browse to it from the MATLAB interface to set it as
the current directory
Engineering Computation: An Introduction Using MATLAB and Excel
Path
• Files that you create in the new folder will run as
long as that folder is set as the current directory in
MATLAB
• However, if another folder is set as the current
directory, files from the folder that you just created
will not run unless its address is added to the
MATLAB path
• The path is a list of locations that MATLAB
searches to find files
Engineering Computation: An Introduction Using MATLAB and Excel
Path
• When you enter a command at the prompt,
MATLAB looks for a file matching the command
name, beginning with the first location in the path.
• To add you new folder to the Path, select File: Set
Path…
Engineering Computation: An Introduction Using MATLAB and Excel
Path
• Select Add Folder…
• Browse to your desired folder
and click OK…
Engineering Computation: An Introduction Using MATLAB and Excel
Path
• Click Save and Close. Note that your new folder is
now the first location searched by MATLAB
Engineering Computation: An Introduction Using MATLAB and Excel
M-File Example
• Consider our sphere example. Let’s find the
weight of the sphere by calculating the volume,
and then multiplying by the specific weight of the
sphere’s material
• Since we plan to repeat this calculation repeatedly,
we will write the formulas in an M-File
Engineering Computation: An Introduction Using MATLAB and Excel
m-file Example
• We open the m-file Editor
by clicking on this icon:
• In the Editor, the lines are
automatically numbered.
This is very helpful in
debugging
• Note that we have left a
semi-colon off the second
line, so that the weight will
be printed to the screen
Engineering Computation: An Introduction Using MATLAB and Excel
m-file Example
• The m-file is saved to the Current Directory, with
the name “WtSphere.m” (the .m is added
automatically)
Engineering Computation: An Introduction Using MATLAB and Excel
m-file Example
• After assigning values to the diameter dia and the
specific weight SpWt, typing the name of the file
executes it:
Engineering Computation: An Introduction Using MATLAB and Excel
Adding Comments
• Text following a % sign in a m-file is added to
document the program.
• Adding comments is important for being able to
comprehend and debug a program
Engineering Computation: An Introduction Using MATLAB and Excel
Context-Specific Help
• The MATLAB Editor often displays help when it
detects possible errors. In this case, we meant to
leave off the semicolon, so we can ignore the
message
Engineering Computation: An Introduction Using MATLAB and Excel
MATLAB Functions
• In addition to the built-in functions of MATLAB,
users can write their own functions
• Functions are m-files, with a special first line:
• For our sphere, the output variable will be WT, the
weight. The function name will be WtSphere, and
the input variables will be the dia and SpWt
• Therefore, the first line will be:
function Wt = WtSphere(dia,SpWt)
Engineering Computation: An Introduction Using MATLAB and Excel
Function Example
• Here is the rest of the function file:
• Note that we added a semicolon to the last line;
the output variable (WT) will be printed to the
screen when we execute the function
Engineering Computation: An Introduction Using MATLAB and Excel
Function Example
• From the command prompt, when we call for the
function, we must enter the arguments of dia and
SpWt:
• The comments immediately following the first line
are displayed when we ask for help with this
function:
Engineering Computation: An Introduction Using MATLAB and Excel
Why Use Functions?
• At first glance, it might appear that the function is exactly
the same as the M-File, with only an extra line added. So
why use the function?
• When we called the first M-file, we defined the diameter
and specific weight before we called the M-file. In other
words, variables stored in memory were passed into the Mfile
• For example, suppose we clear all the variables before
running the function:
Engineering Computation: An Introduction Using MATLAB and Excel
Why Use Functions?
• We note that dia and SpWt, variables used within the
function, are not stored in memory. Only the temporary
variable ans is stored:
• With a function, only the arguments are passed into the
file, and only the outputs are passed out. This means that
in a long program, we don’t have to worry about
overwriting previously assigned variables. This makes
functions more flexible for including in more lengthy
algorithms.
Engineering Computation: An Introduction Using MATLAB and Excel
Download