Introduction to MATLAB Part 1

advertisement
MATLAB
An Introduction to MATLAB
(Matrix Laboratory)
1
MATLAB Windows
Current Directory
Current Directory
Command History
Command History
Command Window
Command Window
Workspace
WorkspaceWindow
Window
2
2
MATLAB Windows
• Command Window
– Heart of MATLAB
– Access most commands and functions
• Workspace window
– Shows created variables during present session
– Variables remain only for present session
• Current Directory Window
–
Contains options for locating, opening, editing, and saving files
• Command History Window
– Keeps a history of commands used and executed in the Command
Window
– Does not show results of your commands
3
Document Window
(Double-click on any Variable in the Workspace window
automatically launches a document window)
Document Window
4
Figure Window
When Figures are created a new window opens
5
Edit Window
Save and Run
6
Order of Operation
1.
2.
3.
4.
Exponentiation
Multiplication / division
Parentheses first
Addition / subtraction
5*(3+6)
= 45
5*3+6
= 21
White space does not matter!!!
5 * 3+6
= 21
7
Parentheses
• Use only ( )
• { } and [ ] mean something different
• MATLAB does not assume operators
5 * (3+4) not 5(3+4)
5*6/6*5 = 25
5*6/(6*5)
= 1
8
Basic Math Functions
• Built into MATLAB
– Addition (+)
– Subtraction (-)
– Multiplication (*)
– Division (/)
– Exponentiation (^)
9
Saving a MATLAB Session
Only values of the variables are saved in the workspace Window
(Caution: Do not program in the Command Window.
Program in the Editor Window)
Variables are saved,
not the work space
10
Saving a MATLAB Session
(Caution: Use Editor Window to program.)
Save either by using the file menu or...
Save with a command in the
command window
11
Saving a Program as a M-file
1. Save your work by creating an m-file
2. File->New->m-file
3. Type your commands in the edit window that
opens
4. Save as XXX.m
5. The file is saved into the current directory
6. It runs in the command window
12
Comments (%)
• Be sure to comment your code
– Add your name
– Date
– Section #
– Assignment #
– Descriptions of what you are doing and why
13
Comments (%)
The % sign identifies comments
You need one on each line
14
Elementary Math Functions
•
•
•
•
•
abs(x)
sign(x)
exp(x)
log(x)
log10(x)
absolute value
plus or minus
ex
natural log
log base 10
15
Rounding Functions
•
•
•
•
round(x)
fix(x)
floor(x)
ceil(x)
16
Rounding Functions
17
Discrete Mathematics
• factor(x)
• gcd(x,y)
greatest common
denominator
•
•
•
•
•
lcm(x)
rats(x)
factorial(x)
primes(x)
isprime(x)
lowest common multiple
represent x as a fraction
18
Trigonometric Functions
•
•
•
•
•
•
•
•
sin(x)
cos(x)
tan(x)
asin(x)
sinh(x)
asinh(x)
sind(x)
asind(x)
sine
cosine
tangent
inverse sine
hyperbolic sine
inverse hyperbolic sine
sine with degree input
inverse sin with degree
output
19
Data Analysis
•
•
•
•
•
•
•
max(x)
min(x)
mean(x)
median(x)
sum(x)
prod(x)
sort(x)
20
Data Analysis
When x is a matrix,
the max is found for
each column
21
Data Analysis
max value
element number
where the max
value occurs
22
Data Analysis
Vector of maximums
Vector of row numbers
23
Data Analysis
24
Determining Matrix Size
• size(x)
• length(x)
number of rows and columns
biggest dimension
25
Determining Matrix Size
26
Variance and Standard Deviation

2
• std(x)
• var(x)
N
 
2
 x
k 1
 
2
k
N 1
27
Random Numbers
• rand(x)
– Returns an x by x matrix of random numbers
between 0 and 1
• rand(n,m)
– Returns an n by m matrix of random numbers
• These random numbers are evenly distributed
28
Random Numbers
29
Matrices
• Group of numbers arranged into rows and
columns
• Single Value (Scalar)
– Matrix with one row and one column
• Vector (One dimensional matrix)
– One row or one column
• Matrix (Two dimensional)
30
Scalar Calculations
• You can use MATLAB like you’d use a
calculator
Command
Prompt
>> 9 + 10
Ans=19
Result
31
Scalar Calculations
32
Variables
•
•
•
•
MATLAB allows you to assign a value to a variable
A=3
Should be read as A is assigned a value of 3
Use the variables in subsequent calculations
33
Predefined MATLAB Functions
• Functions consist of
– Name
– Input argument(s)
– Output
Sqrt (x) = results
Sqrt (4) = 2
34
Functions accept either scalar or matrix input
X=1:10 is one row matrix 1 to 10
35
d - Matrix
36
Array Operations
To create a row vector, enclose a
list of values in brackets
37
Array Operations
You may use either a space or a
comma as a “delimiter” in a row
vector
38
Array Operations
Use a semicolon as a delimiter to create a
new row
39
Array Operations
Use a semicolon as a delimiter to create a
new row
40
Array Operations
Hint: It’s easier to keep track of how many
values you’ve entered into a matrix, if you
enter each row on a separate line. The
semicolons are optional
41
Array Operations
• While a complicated matrix might have to be
entered by hand, evenly spaced matrices can
be entered much more readily. The command
b= 1:5
or the command
b = [1:5]
both return a row matrix
42
Array Operations
The default increment is 1, but if you
want to use a different increment put
it between the first and final values
43
Array Operations
• Array multiplication
• Array division
• Array exponentiation
.*
./
.^
In each case the size of the arrays must match
44
Array Operations
Repetitive Calculations
• assume you have a list of angles in degrees that you would
like to convert to radians.
45
Array Operations
Repetitive Calculations
Either the * or the .* operator can be
used for this problem, because it is
composed of scalars and a single
matrix
The value of pi is built into MATLAB as
a floating point number, called pi
46
Array Operations
Transpose Operator
The transpose operator
makes it easy to create
tables
47
Array Operations
Transpose Operator
table =[degrees;radians]’ would have given
the same result
48
Array Operations
Transpose Operator
The transpose
operator works on
both one dimensional
and two dimensional
arrays
49
Number Display
• Scientific Notation
– Although you can enter any number in
decimal notation, it isn’t always the best
way to represent very large or very small
numbers
– In MATLAB, values in scientific notation are
designated with an e between the decimal
number and exponent. (Your calculator
probably uses similar notation.)
50
Number Display
It is important to omit blanks between
the decimal number and the exponent.
For example, MATLAB will interpret
6.022 e23
as two values (6.022 and 1023 )
51
To calculate spacing between elements use
linspace and logspace
number of elements in
the array
Initial value in the
array
Final value in the
array
52
logspace
number of elements in
the array
Initial value in the
array expressed
as a power of 10
Final value in the
array expressed
as a power of 10
e=
10
100
1000
53
Manipulating MATLAB Matrices
54
55
Download