Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming for Mechanical Engineers (ME 319)

Similar presentations


Presentation on theme: "Introduction to Programming for Mechanical Engineers (ME 319)"— Presentation transcript:

1 Introduction to Programming for Mechanical Engineers (ME 319)
Lecture 2

2 Quote of the day “He who has overcome his fears will truly be free.”
Aristotle (384 BC – 322 BC)

3 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.

4 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

5 Scalar arithmetic operations

6 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 = 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 Columns 10 through 11 n = the number of elements in between If n is not provided, the default value is 100 line = linspace (1,5,11)

7 Creating Arrays and vectors (contd..)
Creates zeros matrix of dimensions i x j zr = zeros(2,2) zr = Creates ones matrix of dimensions k x l on = ones(2,3) on = Creates an identity matrix of dimensions m x m idn = eye(2) idn =

8 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.

9 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 = >> C(4) = 99 % assign a different value for element 4 >> C(6) = 4 % add two elements to the existing C array, the 5th is zero by default >> C (2:5) % chose elements 2, 3, 4 and 5 ans = >> C (1:2:5) % chose elements 1, 3 and 5 In C(4) and C(6), ‘4’ and ‘6’ are the array indices of matrix ‘C’.

10 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 ] % and another 5 elements between 100 and 20 D = % and another 5 elements >> E = [D(2:3,2:4)] % define E that is a submatrix of D E = >> 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 >> 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 =

11 Creating Arrays and vectors (Examples continued)
>> G = F‘ % define matrix G as the transpose of F G = >> G (1,:) % Choose the 1st row in matrix G ans = >> H = G(1:2,:) % define matrix H as the complete 1st two rows of G H = >> I = inv (H) % define matrix I as the inverse of H I = >> J = eig (I) % define matrix J as the eigen values of the square matrix I J = 0.0098

12 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 = >> K = [J F] % define matrix K as the combination of J and F K = % setting next to each others >> L = [F ; J] % define matrix L as the combination of J and F L = % setting over each others >> 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 [ ]

13 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 = >> N = diag(M) % create a vector of diagonal elements in M N = 0.9218 0.9355 0.0579 >> O = [ ] O = >> P = diag (O) % create a matrix of diagonal P = % elements in O

14 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+

15 Thank you for the attention
Any Questions and Suggestions please…


Download ppt "Introduction to Programming for Mechanical Engineers (ME 319)"

Similar presentations


Ads by Google