Ch 2 - MATLAB Expressions Matlab has an interactive mode...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
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 –N a N 1 Matrices and Vectors Arrays are a collection of values referred to by a single variable name
Background image of page 1

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

View Full DocumentRight Arrow Icon
•V e c t o r s Matrices Manual Entry >> A = [4 6 7; 5 3 2; 1 7 4] A = 4 6 7 5 3 2 1 7 4 >> u=[6 3 8] u = 6 3 8 >> v = [3.1; 4.2; 0.5; 9.0] v = 3.1000 4.2000 0.5000 9.0000 “The human mind has never invented a labor-saving machine equal to algebra.” Anonymous 3 Expressions (e.g. Transpose) >> A = [4 6 7; 5 3 2; 1 7 4]; >> B=A' B = 4 5 1 6 3
Background image of page 2
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 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.1270 0.0975 0.9575 0.9706 5 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 >> A = [1 3 5; 2 1 7; 4 3 2; 1 6 9]; 4 rows and 3 columns >> size(A) ans = 4 3 6
Background image of page 3

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

View Full DocumentRight Arrow Icon
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 7 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 8
Background image of page 4
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 Dot Product >> A=ones(2,3); B=[1 2; 3 4; 5 6]; % Create Matrices >> A*B % Matrix Multiply ans = 9 12 9 12 >> u = [10 9 8 6]; v = [1;2;3;4]; % Create vectors >> u*v % Dot product really just matrix multiplication ans = 76 9 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 10
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 27

Ch 2 - MATLAB Expressions Matlab has an interactive mode...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online