371x Filetype PDF File size 0.30 MB Source: publish.uwo.ca
Chapter 1
MATLABM-FILES AND
PROGRAMMING
1.1 Introduction
The course is based around the use of the internationally recognised software tool MATLAB .
There is a comprehensive on-line help with the MATLAB software, there are numerous books and online
tutorials written on MATLAB programming, searching the UWO library database is a good starting point.
MATLAB
❼ - in its simplest form is a basic scientific calculator; in its sophisticated form it is a scientific, graphing,
programmable, matrix algebra calculator with a set of toolboxes for advanced problem solving.
❼ The array of modern utilities involving state of the art algorithms make MATLAB a very powerful
scientific computational tool.
❼ However MATLAB is an interpreted language and can execute more slowly than compiled languages.
Just bear this is mind for large computations.
1.2 Revision of MATLAB Basics - NOT Lectured
Invoke MATLAB – by clicking on the MATLAB icon.
You are in the MATLAB Command Window - which is part of the MATLAB Desktop.
Commands can be typed after the >> prompt.
❼ The MATLAB Environment
TheMATLABdesktopcomeswithanumberofwindowsandtoolswhichdescribeyourcurrentMATLAB
environment.
The main window is the Command Window. The other two windows are the Command History
Window which displays the sequence of commands and outputs that have appeared in the Command
Window, and the Launch Pad/Workspace window.
MATLABCommandsare typed after the >> prompt in the Command Window.
1
AM2415 2010 - Course Support Notes September 27, 2010. Bob Broughton & Piers Lawrence 2
❼ HELP features.
The Windows version of MATLAB contains a complete HELP browser - see Section 1.3.9 as well as
command line HELP utilities.
Try typing help; help help; help who; help ls in the Command Window.
The lookfor command is also available on the command line.
This command will return all occurrences of a word.
Try typing help lookfor and lookfor tan.
❼ ARITHMETIC OPERATIONS
MATLABasadesktop calculator. (Note anything after a % is a MATLAB comment.)
>> 7+9 % ans is a special MATLAB value
ans =
16
>> 7*9
ans =
63
>> 7^2
ans =
49
You must follow the laws of the algebra of real numbers with regard to precedence of operations, eg ∧
(power), */, then +−, with anything in parentheses (brackets () ) being evaluated first.
>> 53+4
ans =
57
>> 5*(3+4) % Calculate (3 + 4) first
ans =
35
❼ LAST-LINE EDITING
You have typed:
>> 5*(12.2+7.5-2/9)/4.6
ans =
21.1715
However you meant to type 2.9 instead of 2/9
MATLAB has a simple line-editor process - △ (uparrow) key recalls previous calculations; then use
right ⊲ and left ⊳ arrow keys to shift on the line; use the Backspace key to delete; and insert text as
required. The down arrow key scrolls through the previous commands.
❼ MATLABERROR MESSAGES
Tutors are always being asked what is wrong with my MATLAB ?.
AM2415 2010 - Course Support Notes September 27, 2010. Bob Broughton & Piers Lawrence 3
Recognizing incorrect code is a matter of practice and commonsense. At this stage we will be mainly
concerned with syntax errors - that is incorrect use of MATLAB statements.
MATLAB will give an error message where possible and you should make sure you read this carefully.
However sometimes the message is not at all clear and may be the result of a progression of errors.
As a simple example:
>> 52+*7
??? 52+*7
|
Error: Unexpected MATLAB operator.
Note that the place where MATLAB believes the error to have occurred is marked.
MATLAB is trying to say that it really expected a value of some kind - say a number instead of two
arithmetic operators. However 52∗+7 would actually be executed since it is assumed you meant 52∗(+7)
with the multiplication being performed first. In this case missing out the value would go undetected and
hence cause a computational error.
❼ BUILT-IN FUNCTIONS
Just as a scientific calculator has keys to invoke special functions such as square-root, exponential,
logarithms; trig functions etc, MATLAB also has built-in functions.
Mathematical MATLAB
√
6.5 >> sqrt(6.5)
6.5
e >> exp(6.5)
ln 6.5 >> log(6.5) natural logarithm (base e)
log 6.5 >> log10(6.5) common logarithm (base 10)
sin(6.5) >> sin(6.5)
NOTE: arguments for trigonometric functions are taken to be in RADIANS
Verify log(10000) is 4
Try these: Verify ln(3.2 · 8.5) = ln(3.2) + ln(8.5)
Verify 10log10(100) = 100
Verify eln(3.5) = 3.5
❼ BUILT-IN VALUES
MATLABhasvarious built-in standard values which are obtained using common terminology.
You have already come across the ans special value which contains the result of a a command line
operation.
The value for π is represented by the name pi in MATLAB .
Type >> pi to obtain a value.
Thus sin(60◦) is expressed as >> sin(60*pi/180)
Since MATLAB essentially works in complex arithmetic, the names i or j are associated with the
√
standard mathematical complex number i = −1
Type >> 1+2i to obtain a value. Note 1+2j will give the same result.
Note that these names may be assigned other values - it is a good habit to get into with MATLAB to
avoid using these MATLAB names for other values.
❼ MODES or FORMATS for DISPLAYING OUTPUT DATA
Type >> help format
Then type
AM2415 2010 - Course Support Notes September 27, 2010. Bob Broughton & Piers Lawrence 4
>> format long
>> pi
>> %then type in
>> format short
>> pi
Compare the output for each case.
The default is format short
❼ MATLABVARIABLES
MATLABallows you to assign names to numeric values. That is we can use variables
So far we have the built-in MATLAB variable ans
>> 3*9
ans =
27
>> 4*6
ans =
24
However it is clear that the value of ans will change for each calculation.
So let us assign names:
>> a=3*9
a =
27
>> b=4*6
b =
24
>> a*b
ans =
648
>> c=a*b
c =
648
However if you type A instead of a you will told A is undefined.
NOTE: This is because in MATLAB , names are CASE SENSITIVE
Also do NOT use the same names as MATLAB does for its functions etc.
❼ MATLABWORKSPACE
Thevalues you are working with are associated with variable names that tag memory locations. By typing
the command who you will obtain a list of such variables; whilst the command whos gives a detailed
list. This information is referred to as your workspace.
In order to clear your workspace the command clear can be used.
no reviews yet
Please Login to review.