Ch2 MATLAB Interactive Computing - Spring 2011-1

Ch2 MATLAB Interactive Computing - Spring 2011-1 - PGE 310...

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

View Full Document Right Arrow Icon
Interactive Computing with MATLAB Chapter 2 of Numerical Methods with MATLAB, Gerald Recktenwald PGE 310: Formulation and Solution in Geosystems Engineering Dr. Balhoff Spring 2011 1 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 2
Background image of page 1

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

View Full Document Right Arrow Icon
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. 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 3 •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 4
Background image of page 2
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 5 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 6
Background image of page 3

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

View Full Document Right Arrow Icon
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 7 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 8
Background image of page 4
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 9 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 10
Background image of page 5

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

View Full Document Right Arrow Icon
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 11 Vectorization and Array Operators >> A = [1 2 3; 4 5 6; 7 8 9]; >> B = [9 8 7; 6 5 4; 3 2 1]; >> A.*B % This is much different than A*B ans = 9 16 21 24 25 24 21 16 9 >> A*B 30 24 18 84 69 54 138 114 90 Element by element operation can be performed by using a period directly before the operator 12
Background image of page 6
Reshaping Matrices OutputMatrix = reshape
Background image of page 7

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

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 29

Ch2 MATLAB Interactive Computing - Spring 2011-1 - PGE 310...

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

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