What is MATLAB?

Download Report

Transcript What is MATLAB?

MATLAB Short
Course
1
History of Calculator
2
Introduction to Matlab
• Matlab is short for Matrix Laboratory
• Matlab is also a programming language
3
Matlab Basics
• Basic data type: Matrix
• A matrix is a twodimensional array of
numbers rows and
A
columns.
• A vector is a onedimensional matrix. It may
have either one row or one
column.
4  2 8 
5 9  2


5 2
9 
4
Toolboxes
Collections of functions to solve problems of
several applications.






Control Toolbox
Neural Network Toolbox
Fuzzy Logic Toolbox
Genetic Algorithms Toolbox
Communication Toolbox
…………..
5
Main Working Windows
After the “>>” symbol, you
can type the commands
6
Display Windows
Graphic (Figure) Window
Displays plots and graphs
Creates response to graphics
commands
M-file editor/debugger window
Create and edit scripts of commands
called M-files
7
Getting Help
To get help:
MATLAB main menu
-> Help
-> MATLAB Help
8
Getting Help
Type one of the following commands in the command
window:





help – lists all the help topics
help topic – provides help for the specified topic
help command – provides help for the specified command
helpwin – opens a separate help window for navigation
Lookfor keyword – search all M-files for keyword
Online resource
9
Variables
Variable names:

Must start with a letter.

May contain only letters, digits, and the underscore “_”.

MATLAB is case sensitive, for example one & ONE are different
variables.

MATLAB only recognizes the first 31 characters in a variable name.
Assignment statement:

Variable = number; >> t=1234

Variable = expression; >>t=a+b
10
Variables
Special variables:





ans: default variable name for the result.
pi: π = 3.1415926 ……
eps: ε= 2.2204e-016, smallest value by which two numbers can differ
inf: ∞, infinity
NAN or nan: not-a-number
Commands involving variables:






who: lists the names of the defined variables
whos: lists the names and sizes of defined variables
clear: clears all variables
clear name: clears the variable name
clc: clears the command window
clf: clears the current figure and the graph window
11
Calculations at the Command Line
MATLAB as a calculator
»
-5/(4.8+5.32)^2
ans =
-0.0488
» (3+4i)*(3-4i)
ans =
25
» cos(pi/2)
ans =
6.1230e-017
» exp(acos(0.3))
ans =
3.5470
Assigning Variables
» a = 2;
» b = 5;
» a^b
ans =
32
» x = 5/2*pi;
» y = sin(x)
Semicolon
suppresses
screen output
Results
assigned to
“ans” if name
not specified
y =
1
» z = asin(y)
z =
() parentheses for
function inputs
1.5708
9
A Note about Workspace:
Numbers stored in double-precision floating point format
12
Creating Matrices
A = [ 100
0
99 ;
7 -1 -25 ]
The semi colon separates the rows of a matrix.
To print the matrix, type the name.
A=
100
7
0
-1
99
-25
13
Accessing Matrix Elements
To print the element in the 2nd row, 3rd column,
1
2 3
type:
A(2, 3 )
1 100 0
9 
Matlab prints ans = -25
A 
2 7
 1  25
By entering
x = A(1,1) + A( 2,3)
Matlab returns x = 75
14
Changing Matrix Elements
To change an entry, enter the new value:
A(2,3) = -50
Matlab prints the new matrix A
A = 100
7
0
-1
99
-50
15
Numerical Array
Concatenation
Use [ ] to combine
existing arrays as
matrix “elements”
» a=[1 2;3 4]
1
2
3
4
» cat_a=[a, 2*a; 3*a,
cat_a =
1
2
2
3
4
6
Column separator:
3
6
4
9
12
12
space / comma (,)
5
10
6
15
20
18
Row separator:
semicolon (;)
Use square
brackets [ ]
a =
4*a; 5*a, 6*a]
4
8
8
16
12
24
4*a
16
The : operator
x = 1 : 7 or x = [ 1 : 7 ] creates the same vector
as the command x = [ 1 2 3 4 5 6 7]
y = 0 : 3 : 12
is the same as
y = [ 0 3 6 9 12 ]
y = 0 : 3 : 11
is the same as
y = [ 0 3 6 9 ]
z = 15 : -4 : 3
is the same as
z = [ 15 11 7 3 ]
w = 0 : 0.01 : 2 is the same as
w = [ 0 0.01 0.02 ... 1.99 2.00 ]
17
Deleting Rows and Columns
» A=[1 5 9;4 3 2.5; 0.1 10 3i+1]
A =
1.0000
5.0000
9.0000
4.0000
3.0000
2.5000
0.1000
10.0000
1.0000+3.0000i
» A(:,2)=[]
A =
1.0000
9.0000
4.0000
2.5000
0.1000
1.0000 + 3.0000i
» A(2,2)=[]
???
Indexed empty matrix assignment is not allowed.
18
Array Subscripting / Indexing
1
A=
A(3,1)
A(3)
2
4
1
2
2
8
3
7.2 3
5
8
7 13
1
18
11 23
4
0
4
0.5 9
4 14
5
19
56 24
5
23
5
83 10 1315
0
20
10 25
1
1.2 7
11
6
16
9 12
4
17
5
4
10
6
3
1
2
21
25 22
A(1:5,5) A(1:end,end)
A(:,5)
A(:,end)
A(21:25) A(21:end)’
A(4:5,2:3)
A([9 14;10 15])
19
Special Matrices and Commands
Z = zeros( 4, 5 )
K = ones( 4, 5 )
is a matrix of 1’s
Id = eye( 4, 5 )
Z =
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Id =
1
0
0
0
0
0
1
0
0
0
0
0
1
0
0
0
0
0
1
20
0
Special Matrices and Commands
The transpose operator is ’
transZ = Z’
transZ =
Z =
1
2
3
1
4
4
5
6
2
5
3
6
21
Special Matrices and Commands
>> A = [ 100
0
99 ;
7 -1 -25 ]
A =
100
0
99
7
-1
-25
>> size(A)
a vector of rows and columns in A
ans =
2
3
>> length(A)
ans =
3
22
Special Matrices and Commands
>> A = [ 100
0
99 ;
7 -1 -25 ]
A =
100
0
99
7
-1
-25
>> v=diag(A)
a vector with the diagonal elements of A
v =
100
-1
>> B=diag(v)
B =
100
0
0
-1
a matrix with v on the diagonal and 0’s off the
diagonal
The diag command shows that
Matlab commands can depend on the
nature of the arguments.
23
Matrix Manipulation Functions
•
•
•
•
•
•
•
•
•
zeros: Create an array of all zeros
ones: Create an array of all ones
eye: Identity Matrix
rand: Uniformly distributed random numbers
diag: Diagonal matrices and diagonal of a matrix
size: Return array dimensions
fliplr: Flip matrices left-right
flipud: Flip matrices up and down
repmat: Replicate and tile a matrix
24
Matrix Manipulation Functions
•
•
•
•
•
•
•
•
•
•
transpose (’): Transpose matrix
rot90: rotate matrix 90
tril: Lower triangular part of a matrix
triu: Upper triangular part of a matrix
cross: Vector cross product
dot: Vector dot product
det: Matrix determinant
inv: Matrix inverse
eig: Evaluate eigenvalues and eigenvectors
rank: Rank of matrix
25
Arithmetic Operations
A, B are matrices, x is a scalar
x*A
multiplies each entry of A by x
x + A
adds x to each entry of A
same as x*ones(size(A)) + A
x – A
same as x*ones(size(A)) – A
A + B
standard matrix addition
A – B
standard matrix subtraction
26
Arithmetic Operations (cont.)
A*B
Standard matrix multiplication
cols A = rows B
A.*B
element-wise multiplication
size( A) = size(B)
A.\B
divide elements of B by those in A
A./B
divide elements of A by those in B
27
Matrix Multiplication
» a = [1 2 3 4; 5 6 7 8];
[2x4]
» b = ones(4,3);
[4x3]
» c = a*b
[2x4]*[4x3]
[2x3]
c =
10
26
10
26
10
26
a(2nd row).b(3rd column)
Array Multiplication
» a = [1 2 3 4; 5 6 7 8];
» b = [1:4; 1:4];
» c = a.*b
c =
1
5
4
12
9
21
16
32
c(2,4) = a(2,4)*b(2,4)
28
Linear Equations
Example: a system of 3 linear equations with 3 unknowns
(x1, x2, x3)
 3 x1 + 2x2 - x3 = 10
 - x1 + 3x2 + 2x3 = 5
 x1 - 2x2 - x3 = -1
 Let:
3 2 1
A   1 3 2 
 1 1 1
 x1 
x   x2 
 x3 
10 
b   5 
 1
Then, the system can be described as:
Ax = b
29
Complex Numbers
Matlab works with complex numbers, so
sqrt( -3 ) is a valid command.
Lowercase i is reserved for the special value
sqrt(-1)
30
Special Values
pi = 3.14159 ....
Inf = infinity (divide by 0)
M = [ pi sqrt(-3); Inf 0]
M =
3.1416
Inf
0 + 1.7321i
0
31
Linear Equations
Solution by Matrix Inverse:
Ax = b
A-1 Ax = A-1 b
Ax = b
Solution by Matrix Division:
 Ax = b
 Can be solved by left division
b\A
MATLAB:
MATLAB:
>> A = [3 2 -1; -1 3 2; 1 -1 -1];
>> b = [10;5;-1];
>> x = inv(A)*b
x=
-2.0000
5.0000
-6.0000
>> A = [3 2 -1; -1 3 2; 1 -1 1];
>> b = [10;5;-1];
>> x =A \ b
x=
-2.0000
5.0000
-6.0000
32
Example
33
Example
34
Example
P=0, Vz=0, My=0, Vy=-1000 lb, Mz=100,000 lb.in
35
Elementary Math Function
• abs, sign: Absolute value and Signum
Function
• sin, cos, asin, acos…: Triangular functions
• exp, log, log10: Exponential, Natural and
Common (base 10) logarithm
• ceil, floor: Round toward infinities
• fix: Round toward zero
36
Elementary Math Function
round: Round to the nearest integer
gcd: Greatest common devisor
lcm: Least common multiple
sqrt: Square root function
real, imag: Real and Image part of
complex
rem: Remainder after division
37
Elementary Math Function
•
•
max, min: Maximum and Minimum of arrays
mean, median: Average and Median of arrays
• std, var: Standard deviation and variance
• sort: Sort elements in ascending order
• sum, prod: Summation & Product of Elements
• trapz: Trapezoidal numerical integration
• cumsum, cumprod: Cumulative sum, product
• diff, gradient: Differences and Numerical
Gradient
38
Polynomials and Interpolation
Polynomials






Representing
Roots
(>> roots)
Evaluation (>> polyval)
Derivatives (>> polyder)
Curve Fitting (>> polyfit)
Partial Fraction Expansion (residue)
Interpolation


One-Dimensional (interp1)
Two-Dimensional (interp2)
39
Example
polysam=[1 0 0 8];
roots(polysam)
ans =
-2.0000
1.0000 + 1.7321i
1.0000 - 1.7321i
Polyval(polysam,[0 1 2.5 4 6.5])
ans =
8.0000
9.0000
23.6250
72.0000
polyder(polysam)
ans =
3
0
0
[r p k]=residue(polysam,[1 2 1])
r = 3
7
p = -1
-1
k = 1
-2
282.6250
40
Example
x
y
p
p
= [0: 0.1: 2.5];
= erf(x);
= polyfit(x,y,6)
=
0.0084 -0.0983
0.4217
-0.7435
interp1(x,y,[0.45 0.95 2.2 3.0])
ans =
0.4744
0.8198
0.9981
0.1471
1.1064
0.0004
NaN
41
Logical Operations
»
»
Mass = [-2 10 NaN 30 -11 Inf 31];
each_pos = Mass>=0
> greater than
each_pos =
0
1
0
1
0
1
1
< less than
» all_pos = all(Mass>=0)
>= Greater or equal all_pos =
0
<= less or equal
» all_pos = any(Mass>=0)
~ not
all_pos =
1
& and
» pos_fin = (Mass>=0)&(isfinite(Mass))
pos_fin =
|
or
0
1
0
1
0
0
1
= = equal to
isfinite(), etc. . . .
all(), any()
find
Note:
• 1 = TRUE
• 0 = FALSE
42