720 likes | 904 Views
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
E N D
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 columns • Single 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
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 number of elements in the array initial value in the array final value in the array
“What’s the output” >>A = [1 2 3; 3 5 6] This statement stores the matrix: 1. A= 2. A= 3. A= 4. A=
What’s the output? >>A = [0:0.1:0.5] This statement stores the matrix: 1. A= 2. A= A = 3. A=
What’s the output? >>A = 5*[0:0.1:0.5] This statement stores the matrix: 1. A= 2. A= A = 3. A=
What’s the output? >>A = linspace(0,10,3) This statement stores the matrix: 1. A= 2. A= A = 3. A=
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];
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 • 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. S = [3.0, 1.5, 3.1]; T = [1 2 3; S] B = [1.5, 3.1]; S = [3.0, B] S = 3.0 1.5 3.1 T = 1 2 3 3.0 1.5 3.1 S(2) T(2, 2) T(4)
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: 2. B= 7 8 B= 6 8 4. B= 7 8 3. B= 5 7
MATLAB uses function names consistent with most major programming languages For example • sqrt • sin • cos • log
Trigonometric Functions sin(x) sine cos(x) cosine tan(x) tangent asin(x) inverse sine sinh(x) hyperbolic sine asinh(x) inverse hyperbolic sine sind(x) sine with degree input asind(x) inverse sin with degree output
Data Analysis max(x) min(x) mean(x) median(x) sum(x) prod(x) sort(x)
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) 3 6 2 a) 6 3 5 6 6 5 6
What does C store? >>A = [1 2 3; 3 5 6;1 2 1] >>B = max(A) >>C=max(B) 3 6 2 a) 6 3 5 6 6 5 6
What does B and C store? >>A = [1 2 3; 3 5 6;1 2 1] >>[B,C] = max(A) B = 3 6 2 C = 3 3 2 B= 6 C = 4 B = 3 5 6 C = 2 2 2 B = 6 5 6 C = 1 1 1
What does D and E store? >>A = [1 2 3; 3 5 6;1 2 1] >>[B,C] = max(A) >>[D,E] = max(B) D = 6 E = 3 D= 6 E = 4 D = 3 5 6 E = 3 D = 6 E = 2
What is B? >>A = [4 4 4 1 3 1 1] >>B = median(A) B = 3 B = 4 B = 1 B = 7
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 matrix multiplication 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 each element of B with the same element of A. In order to use ‘.*’, 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) 2 8 18 5 a) 6 20 6 2 10 24 12 1 4 12 30 6
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 • color • Try usinghelp plotfor a list of available styles
Specify your choices in a string • For example plot(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)
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 columns • subplot(m,n,p) rows columns location
subplot(2,2,1) 2 columns 2 1 2 rows 3 4
Other Types of 2-D Plots • Polar Plots • Log/semi-log (semilogx,semilogy,etc.) • Bar Graphs • Pie Charts • Histograms • X-Y graphs with 2 y axes • Function Plots
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 code • The comment lines immediately after the first line are returned when you query the help function
Logical Operators & and ~ not | or