Introduction to Programming for Mechanical Engineers (ME 319) Lecture 2
Quote of the day “He who has overcome his fears will truly be free.” Aristotle (384 BC – 322 BC)
Quick review of Lecture 1 Work on the Command window by creating variables and performing simple mathematical expressions. Assign proper variables and files names. Assigning a value to a variable. Writing your commands inside a script file. Properly comment words or any other text. Run the script file.
Order of Precedence A scalar variable is a variable that contains a single number. (1×1 matrix) MATLAB uses the symbols + - * / ^ for addition, subtraction, multiplication, right division and exponentiation respectively. \ is used for left division in which denominator comes first. Example: 2\7 = 7/2 = 3.5
Scalar arithmetic operations
Creating Arrays and vectors Command Syntax Description Example The simplest way to create simple vectors, not convenient for complex vectors line = [1 3 5] line = 1 3 5 Where xi = the initial element in the array. xf = the destination element in the array. d = the step size (Default value=1) xi could be bigger than xf, but d should be –ve in this case line = 1:0.4:5 Columns 1 through 9 1.00 1.40 1.80 2.20 2.60 3.00 3.40 3.8 4.20 Columns 10 through 11 4.60 5.00 n = the number of elements in between If n is not provided, the default value is 100 line = linspace (1,5,11) 1.00 1.40 1.80 2.20 2.60 3.00 3.40 3.80 4.20 4.6000 5.0000
Creating Arrays and vectors (contd..) Creates zeros matrix of dimensions i x j zr = zeros(2,2) zr = 0 0 Creates ones matrix of dimensions k x l on = ones(2,3) on = 1 1 1 Creates an identity matrix of dimensions m x m idn = eye(2) idn = 1 0 0 1
Creating Arrays and vectors All variables in MATLAB are arrays. A scalar variable is an array of single element. The array variable is defined by the input when it is assigned. Therefore there is no need to define the size of an array, since it will take the size of the input automatically. You can still perform any operation on any array to change its name, contents, dimensions, or type. You can fill in an array with scalar values, predefined variables, or expressions.
Examples >> A = 5; B = power (A,3); % define two variables A and B >> C = [A, 27, sqrt (B), B - A^2] % define an array C of 4 elements C = 5.0000 27.0000 11.1803 100.0000 >> C(4) = 99 % assign a different value for element 4 5.0000 27.0000 11.1803 99.0000 >> C(6) = 4 % add two elements to the existing C array, the 5th is zero by default 5.0000 27.0000 11.1803 99.0000 0 4.0000 >> C (2:5) % chose elements 2, 3, 4 and 5 ans = 27.0000 11.1803 99.0000 0 >> C (1:2:5) % chose elements 1, 3 and 5 5.0000 11.1803 0 In C(4) and C(6), ‘4’ and ‘6’ are the array indices of matrix ‘C’.
Creating Arrays and vectors (Examples continued) >> D = [linspace(A,B,5); 100:-20:20; % define a matrix D of 5 elements between A and B 1 2 3 4 5] % and another 5 elements between 100 and 20 D = % and another 5 elements 1 2 3 4 5 5 35 65 95 125 100 80 60 40 20 1 2 3 4 5 >> E = [D(2:3,2:4)] % define E that is a submatrix of D E = 80 60 40 2 3 4 >> F = [C(1:3);E(1,:)] % define matrix F that’s a combination of the 1st three elements of C F = % as its 1st row, and the complete 1st row of E as its 2nd row 5.0000 27.0000 11.1803 80.0000 60.0000 40.0000 >> F(2,2) = F(2,1) - F(2,3) + 10*E(2,3) % change element (2,2) of matrix F using the expression to the right F = 80.0000 80.0000 40.0000
Creating Arrays and vectors (Examples continued) >> G = F‘ % define matrix G as the transpose of F G = 5.0000 80.0000 27.0000 80.0000 11.1803 40.0000 >> G (1,:) % Choose the 1st row in matrix G ans = 5 80 >> H = G(1:2,:) % define matrix H as the complete 1st two rows of G H = 27 80 >> I = inv (H) % define matrix I as the inverse of H I = -0.0455 0.0455 0.0153 -0.0028 >> J = eig (I) % define matrix J as the eigen values of the square matrix I J = -0.0581 0.0098
Creating Arrays and vectors Examples (continue): >> J (:,2:3) = [1 2;3 4] % add complete 2nd and 3rd columns to the existing matrix J J = -0.0581 1.0000 2.0000 0.0098 3.0000 4.0000 >> K = [J F] % define matrix K as the combination of J and F K = % setting next to each others -0.0581 1.0000 2.0000 5.0000 27.0000 11.1803 0.0098 3.0000 4.0000 80.0000 80.0000 40.0000 >> L = [F ; J] % define matrix L as the combination of J and F L = % setting over each others 5.0000 27.0000 11.1803 80.0000 80.0000 40.0000 -0.0581 1.0000 2.0000 0.0098 3.0000 4.0000 >> L (1:3,:) = [ ] % delete the complete 1st three rows of L by replacing the L = % rows with the empty matrix notation [ ] >> L (1) = [ ] % delete the 1st element of L by replacing its content with L = % the empty matrix notation [ ] 3 4
Built-in functions for handling arrays Examples (continue): >> length_J = length (J) % check the maximum number of elements in a row or a column length_J = 3 >> [row_J , column_J] = size (J)% return the size of J row_J = 2 column_J = >>M = rand (3) % create a random matrix M of dimensions 3x3 M = 0.9218 0.4057 0.4103 0.7382 0.9355 0.8936 0.1763 0.9169 0.0579 >> N = diag(M) % create a vector of diagonal elements in M N = 0.9218 0.9355 0.0579 >> O = [6 -12 20] O = 6 -12 20 >> P = diag (O) % create a matrix of diagonal P = % elements in O 6 0 0 0 -12 0 0 0 20
Creating Strings and Strings as variables Examples (continue): SYNTAX: >> student_name = 'Robert Smith' student_name = Robert Smith >> student_name (8) ans = S >> student_info = char ('student info=’ , student_name , ‘Grade’, 'B+') student_info = student name Grade B+
Thank you for the attention Any Questions and Suggestions please…