# Matlab - Chapter 2 MATLAB Expressions Matlab has an...

This preview shows pages 1–8. Sign up to view the full content.

Chapter 2 MATLAB Expressions Matlab has an interactive mode – Many expressions are obvious (like addition, multiplication, etc.) Some expressions (exponentials, natural logarithms, Bessel Functions, etc.) are not as obvious –T h e help , lookfor commands can be useful Variables can be stored in Matlab –a = 5 – b = 2^3+6 –c = a + b Some variables are built in. Be careful, you can change them ans –p i –I n f NaN

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Matrices and Vectors Arrays are a collection of values referred to by a single variable name Matrices can be defined several different ways 1. Manual Entry 2. Expressions that evaluate a matrix 3 Buil -in functions that return matrices 3. Built in functions that return matrices 4. User-written functions that return matrices 5. Importing matrix data from files on disk “Roger, Roger. What’s our vector, Victor?” Airplane Manual Entry •V e c t o r s >> u=[6 3 8] >> v = [3.1; 4.2; 0.5; 9.0] u = 6 3 8 v = 3.1000 4.2000 0.5000 9.0000 Matrices >> A =[467;532;174] A [4 6 7; 5 3 2; 1 7 4] A = 4 6 7 “The human mind has never invented a labor-saving machine equal to algebra.” Anonymous 5 3 2 1 7 4
Expressions (e.g. Transpose) >> A = [4 6 7; 5 3 2; 1 7 4]; >> B=A' B = 4 5 1 6 3 7 7 2 4 Built in functions diag, eye, ones, rand, length, linspace, logspace, zeros, size >> eye(4) % 4x4 Identity Matrix ans = 1 0 0 0 0 1 0 0 0 0 1 0 0001 0 0 0 1 >> rand(3,4) % Matrix of random numbers between 0 and 1 ans = 0.8147 0.9134 0.2785 0.9649 0 9058 0 6324 0 5469 0 1576 0.9058 0.6324 0.5469 0.1576 0.1270 0.0975 0.9575 0.9706

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
More Built in Functions >> linspace(0,100,11) points ans = 0 10 20 30 40 50 60 70 80 90 100 >> x = logspace(1,5,3) x = 10 1000 100000 10 1000 >> A =[135;217;432;169] ; 4 rows and 3 columns A [1 3 5; 2 1 7; 4 3 2; 1 6 9]; >> size(A) ans = 4 3 Subscript Notation >> A = [1 3 5; 2 1 7; 4 3 2; 1 6 9]; % A is 4x3 matrix >> A(2,3) % What is the value of A in the second row, 3 rd column? ans = 7 >> A(2,3)=6 % Change the value of A in the 2nd row and 3 rd column A = 1 3 5 2 1 6 4 3 2 1 6 9
Colon Notation >> v = 1:10 % Create a vector starting at 1 and ending at 10 v = 1 2 3 4 5 6 7 8 9 10 >> u=1:2:10 % Create a vector starting at 1 going by twos u = 1 3 5 7 9 >> A = [1 3 5; 2 1 7; 4 3 2; 1 6 9]; % Create a 4x3 matrix >> A(:,2) % create a vector of the second column of A ans = 3 1 3 6 Math Matrix Operations Addition and Subtraction (u+v, u-v), Matrices must be same size “At the source of every error which is blamed on the computer you will find at least two human errors including the error of blaming it on the computer.” Anonymous Review matrix multiplication >> A=ones(2,3); B=[1 2; 3 4; 5 6]; % Create Matrices >> A*B % Matrix Multiply ans = 9 12 9 12 Dot Product >> u = [10 9 8 6]; v = [1;2;3;4]; % Create vectors >> u*v % Dot product really just matrix multiplication >> uv ans = 76

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Vectorization and Array Operators In Matlab, function is applied to all elements >> x = 0:pi/4:pi x = 0 0.7854 1.5708 2.3562 3.1416 >> y = cos(x) y = 1.0000 0.7071 0.0000 -0.7071 -1.0000 Vectorization and Array Operators Element by element operation can be performed by using a period directly >> A = [1 2 3; 4 5 6; 7 8 9]; Element by element operation can be performed by using a period directly before the operator >> B = [9 8 7; 6 5 4; 3 2 1]; >> A *B % This is much different than A*B >> A. B % This is much different than AB ans = 9 16 21 24 25 24 21 16 9 >> A*B 30 24 18 84 69 54 138 114 90
Reshaping Matrices OutputMatrix

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 79

Matlab - Chapter 2 MATLAB Expressions Matlab has an...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online