Upload
tegan
View
50
Download
2
Tags:
Embed Size (px)
DESCRIPTION
Review of MATLAB. The basics of MATLAB Manipulating matrices Built-in functions Plotting Functions in Matlab Mathematical operations and system of eqs . NOTE: Covers Chapts . 1-6, but you should be pretty good if you mainly focus on the first 4 chapters - PowerPoint PPT Presentation
Citation preview
Review of MATLAB • The basics of MATLAB• Manipulating matrices• Built-in functions• Plotting• Functions in Matlab• Mathematical operations and system
of eqs.
NOTE: Covers Chapts. 1-6, but you should be pretty good if you mainly focus on the first 4 chapters
Slides most important: 1-48, 53-57, and 59
The Basic Data Type in MATLAB: Matrices
Group of numbers arranged into rows and columnsSingle Value (Scalar) Matrix with one row and one column
Vector (One dimensional matrix) One row or one column
Matrix (Two dimensional)
To create a row vector, enclose a list of values in brackets
You may use either a space or a comma as a “delimiter” in a row vector
Use a semicolon as a delimiter to create a new row
Shortcuts• 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
The default increment is 1, but if you want to use a different increment put it between the first and final values
To calculate spacing between elements use linspace
initial value in the array
final value in the array
number of elements in the array
“What’s the output”
>>A = [1 2 3; 3 5 6]
This statement stores the matrix:
1. A=1 2 3
3 5 6
2. A= 1 2 3 3 5 6
3. A=
1 3
2 5
3 6
4. A= 1 2 3;3 5 6
What’s the output?
>>A = [0:0.1:0.5]
This statement stores the matrix:
1. A= 0 0.1 0.2 0.3 0.4 0.5 2. A=
3. A= 4. A =
0
0 0.1 0.5 0.1 0.5
What’s the output?
>>A = 5*[0:0.1:0.5]
This statement stores the matrix:
1. A= 0 0.5 1.0 1.5 2.0 2.5 2. A=
3. A= 4. A =
0
0 0.1 0.5 0.1 0.5
What’s the output?
>>A = linspace(0,10,3)
This statement stores the matrix:
1. A= 0 5 10 2. A=
3. A= 4. A =
0 10 3
0 3 6 9 0 10 10
Manipulating Matrices
• Defining matrices• A matrix can be defined by typing in a list of numbers
enclosed in square brackets.• The numbers can be separated by spaces or
commas.• New rows are indicated with a semicolon.
A = [ 3.5 ];B = [1.5, 3.1]; or B =[1.5 3.1];C = [-1, 0, 0; 1, 1, 0; 0, 0, 2];
Manipulating Matrices
• Defining matrices• Define a matrix by using another matrix that has
already been defined.
• Reference an element in a matrix• Both row and column indices start from 1.
B = [1.5, 3.1]; S = [3.0, B]
S = [3.0, 1.5, 3.1]; T = [1 2 3; S]
S = 3.0 1.5 3.1 T = 1 2 3 3.0 1.5 3.1
S(2)
T(2, 2)
T(4)
The element value on row 2 and column 2 of matrix T
Count down column 1, then down column 2, and finally down column 2 to the correct element of matrix T.
Manipulating Matrices
• Change values in a matrix• S(2) = -1.0;
changes the 2nd value in the matrix S
from 1.5 to -1.0
• Extend a matrix by defining new elements.
S(4) = 5.5;Extend the matrix S to four elements instead of three.
S(8) = 9.5;Matrix S will have eight values, and the values of S(5), S(6), S(7) will be set to 0.
T(3, 3) = 10;T(4, 5) = 20;
Manipulating Matrices
• Using the colon operator• Define an evenly spaced matrix
H = 1:8 --- The default spacing is 1
time = 0.0:0.5:3.0 --- The middle value becomes the spacing.
• Extract data from matrices x = M( :, 1) --- extract column 1 from matrix M
y = M( :, 4) --- extract column 4 from matrix M
z = M(2, : ) --- extract row 2 from matrix M
a = M(2:3, : ) --- extract rows 2 and 3 from matrix M
b = M( :, 2:4) --- extract column 2 to column 4 from matrix M
c = M(2:3, 3:5) --- extract not whole rows or whole columns
from matrix M
• Converts a two dimensional matrix to a single column M( : )
M = 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7
What’s does B store?
>>A = [5 6 ; 7 8]>>B = A(:,2)
This statement stores the matrix:
1. B= 6 8
2. B= 7 8
3. B= 5 7 4. B= 7 8
NEXT TOPIC: MATLAB BUILT-IN FUNCTIONS
MATLAB uses function names consistent with most major programming languages
For example sqrt sin cos log
Function Input can be either scalars or matrices
Trigonometric Functions
sin(x) sinecos(x) cosinetan(x) tangentasin(x) inverse sinesinh(x) hyperbolic sineasinh(x) inverse hyperbolic sinesind(x) sine with degree inputasind(x) inverse sin with degree output
Data Analysis
max(x)min(x)mean(x)median(x)sum(x)prod(x)sort(x)
When x is a matrix, the max is found for each column
max value
element number where the max value occurs
Returns both the smallest value in a vector x and its location in vector x.
Vector of maximums
Vector of row numbers
Returns a row vector containing the minimum element from each column of a matrix x, and returns a row vector of the location of the minimum in each column of matrix x.
What does B store?
>>A = [1 2 3; 3 5 6;1 2 1]>>B = max(A)
c) 6 5 6
b) 3 6 2a) 6
d) 3 5 6
What does C store?
>>A = [1 2 3; 3 5 6;1 2 1]>>B = max(A)>>C=max(B)
c) 6 5 6
b) 3 6 2a) 6
d) 3 5 6
What does B and C store?
>>A = [1 2 3; 3 5 6;1 2 1]>>[B,C] = max(A)
c) B = 6 5 6 C = 1 1 1
b) B = 3 6 2 C = 3 3 2
a) B= 6 C = 4
d) B = 3 5 6 C = 2 2 2
What does D and E store?
>>A = [1 2 3; 3 5 6;1 2 1]>>[B,C] = max(A)>>[D,E] = max(B)
c) D = 6 E = 2
b) D = 6 E = 3
a) D= 6 E = 4
d) D = 3 5 6 E = 3
What is B?
>>A = [4 4 4 1 3 1 1]>>B = median(A)
c) B = 7
b) B = 3a) B = 4
d) B = 1
Will this piece of code work? If not, why?
>>A = [1 2 3; 3 5 6]>>B = [2 4 6; 6 10 18]>>C = B*A
This code will not work because ‘*’ is used for matrixmultiplication and in matrix multiplication you need the same number of columns in B as rows in A.
Will this piece of code work? If not, why?
>>A = [1 2 3; 3 5 6]>>B = [2 4 6; 6 10 18]>>C = B.*A
This code will work because ‘.*’ means multiply eachelement of B with the same element of A. In order touse ‘.*’, A and B need to be the same dimensions.
What does C store?
>>A = [1 2 3; 3 5 6;1 2 1]>>B = [2 4 ; 6 1]>>C = B.*A(2:3,1:2)
c) 4 12 30 6
b) 2 8 18 5
a) 6 20 6 2
d) 10 24 12 1
PLOTTING IN MATLAB
Define x and y and call the plot function
Engineers always add …
Title title(‘y = cos(x)’)
X axis label, complete with units xlabel(‘x-axis’)
Y axis label, complete with units ylabel(‘y-axix’)
Often it is useful to add a grid grid on
Single quotes are used.
Line, Color and Mark Style
You can change the appearance of your plots by selecting user defined line styles mark styles colorTry using
help plotfor a list of available styles
Specify your choices in a string
For exampleplot(x, y, ‘:ok')
strings are identified with single quotes the : means use a dotted line the o means use a circle to mark each
point the letter k indicates that the graph
should be drawn in black (b indicates blue)
Available choicesTable 5. 2 Line, Mark and Color Options
Line Type Indicator Point Type Indicator Color Indicator
solid - point . blue b
dotted : circle o green g
dash-dot -. x-mark x red r
dashed -- plus + cyan c
star * magenta m
square s yellow y
diamond d black k
triangle down v
triangle up ^
triangle left <
triangle right >
pentagram p
hexagram h
specify the drawing parameters for each line after the ordered pairs that define the line
Subplots
The subplot command allows you to subdivide the graphing window into a grid of m rows and n columnssubplot(m,n,p)
rows columns location
subplot(2,2,1)
2 rows
2 columns
1 2
3 4
-20
2-2
02
-5
0
5
x
Peaks
y
2 rows and 1 column
Other Types of 2-D Plots
Polar PlotsLog/semi-log (semilogx,semilogy,etc.)
Bar GraphsPie ChartsHistogramsX-Y graphs with 2 y axesFunction Plots
CREATING FUNCTIONS IN MATLAB
A simple function (poly2)
Save the file as poly2.m (same as the name of the function)
The function is available from the command window or from other M-file programs
Comments
You should comment functions, just as you would any computer codeThe comment lines immediately after the first line are returned when you query the help function
Using find and logical operators in Matlab
Logical Operators
& and~ not| or
find
The find command searches a matrix and identifies which elements in that matrix meet a given criteria.
index numbers
element values
Mathematical manipulations etc…
MATLAB interprets * to mean matrix multiplication. The arrays a and b are not the correct size for matrix multiplication in this example
Multiplication between arrays is performed on corresponding elements if the .* operator is used
Array Operations (element-by-element)
Array multiplication .*Array division ./Array exponentiation .^
In each case the size of the arrays must match
What’s the output?
>>A = [1 2 3; 3 5 6]>>B = [2 4 6; 6 10 18]>>C = B./A
This statement stores the matrix:
1. C=2 2 2
2 2 3
2. C= 2 2 2 2 2 3
3. C=
2 2
2 2
2 3
4. Can’t divide these matrices
What’s the output?
>>A = [1 2 3; 3 5 6]>>B = [2 4 6; 6 10 18]>>C = B*A
This statement stores the matrix:
1. C=2 2 2
2 2 3
2. C= 2 2 2 2 2 3
3. C=
2 2
2 2
2 3
4. Can’t multiply these matrices: not the correct dimension – A is 2x3 B is 2x3
What is A’?
>> A = [1, 2, 3; 4, 3, 6];>> A’
A. 1 4 2 3 3 6
B. 4 1 3 2 6 3
C. 1 2 3 4 3 6
D. 4 3 6 1 2 3
This is the transpose= the rows and columns are flipped
What’s the output?
>>A = [1 2 3; 3 5 6]>>B = [1 1 1; 1 1 1]>>C = B*A’
This statement stores the matrix:
1. C=6 14
6 14
2. C=
3. C=14 14
6 6
4. Can’t multiply these matrices: not the correct dimension – A is 2x3 B is 2x3
2 2 2
2 2 3
What is C?
>> A = [1, 2, 3];>> B = [4, 2, 1];>> C=dot(A,B)
A. 1 4 2 2 3 1
B. 11
C. 1 2 3 4 2 1
D. 4 8 3
Dot product = sum of the elements in vector A multiplied by same elements in vector B
What is C?
>> A = [1, 2, 3; 3, 2, 1];>> B = [1, 1; 1, 1; 1, 1];>> C=A*B
A. 1 4 2 2 3 1
B. 11
C. 6 6 6 6
D. 4 8 3
Matrix multiplication= dot product of each row of A with each column of B
Matrix Inverse
MATLAB offers two approaches The matrix inverse function
inv(A) Raising a matrix to the -1 power
A-1
A matrix times its inverse is the identity matrix
Equivalent approaches to finding the inverse of a matrix
Not all matrices have an inverse
Called Singular Ill-conditioned matrices
Attempting to take the inverse of a singular matrix results in an error statement
Determinants
Related to the matrix inverseIf the determinant is equal to 0, the matrix does not have an inverseThe MATLAB function to find a determinant is det(A)
What is B?
>> A = [1, 2; 3, 2];>> B=det(A)
A. 0 B. 4
C. -4 D. 8
Don’t have to know the mathematics of the determinent
Solutions to Systems of Linear Equations
3 2 10
3 2 5
1
x y z
x y z
x y z
Using Matrix Nomenclature
111
231
123
A
z
y
x
X
1
5
10
B
and
AX=B
We can solve this problem using the matrix inverse approach
This approach is easy to understand, but its not the more efficient computationally
Matrix left division uses Gaussian elimination, which is much more efficient, and less prone to round-off error
What is X?
>> A = [1, 0; 0, 1];>> B = [5; 2]>> X = inv(A)*B
A. 1 1 B. 5 2
C. 5 1 D. 5 2