Você está na página 1de 15

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

Quick review of Lecture 1


1.

Work on the Command window by creating variables

and performing simple mathematical expressions.


2. 3. 4. 5. 6.

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.

Creating Arrays and vectors


Command Syntax
vector _ name [type vector elements]

Description
The simplest way to create simple vectors, not convenient for complex vectors

Example
line = [1 3 5] line = 1 3 5 line = 1:0.4:5 line = 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

array _ name xi : d : x f

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 Where xi = the initial element in the array. xf = the destination element in the array. n = the number of elements in between If n is not provided, the default value is 100

array _ name linspace ( xi , x f , n)

line = linspace (1,5,11) line = Columns 1 through 9 1.00 1.40 1.80 2.20 2.60 3.40 3.80 4.20 Columns 10 through 11 4.6000 5.0000

3.00

Creating Arrays and vectors (contd..)


zr zeros (i, j )
zr = zeros(2,2) zr = 0 0 0 0

Creates zeros matrix of dimensions i x j

on ones (k , l )

Creates ones matrix of dimensions k x l

on = ones(2,3) on = 1 1 1 1

1 1

idn eye (m)

Creates an identity matrix of

dimensions m x m

idn = eye(2) idn = 1 0 0 1

Creating Arrays and vectors


1.

All variables in MATLAB are arrays. A scalar variable is an array of single element.

2.

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.

3.

You can still perform any operation on any array to change its name, contents, dimensions, or type.

4.

You can fill in an array with scalar values, predefined variables,


or expressions.

Examples
>> A = 5; B = power (A,3); >> C = [A, 27, sqrt (B), B - A^2] C= 5.0000 27.0000 11.1803 100.0000 >> C(4) = 99 C= 5.0000 27.0000 11.1803 99.0000 >> C(6) = 4 C= 5.0000 27.0000 11.1803 99.0000 >> C (2:5) ans = 27.0000 11.1803 99.0000 >> C (1:2:5) ans = 5.0000 11.1803 % define two variables A and B % define an array C of 4 elements

% assign a different value for element 4

% add two elements to the existing C array, the 5th is zero by default 0 4.0000 % chose elements 2, 3, 4 and 5

0 % chose elements 1, 3 and 5

Creating Arrays and vectors (Examples continued)


>> D = [linspace(A,B,5); 100:-20:20; 1 2 3 4 5] D= 5 35 65 95 125 100 1 80 2 60 3 40 4 20 5 % define E that is a submatrix of D % define a matrix D of 5 elements between A and B % and another 5 elements between 100 and 20 % and another 5 elements 1 2 3 4 5

>> E = [D(2:3,2:4)] E= 80 60 40 2 3 4

>> F = [C(1:3);E(1,:)] F= 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) F= 5.0000 27.0000 11.1803 80.0000 80.0000 40.0000

% define matrix F thats a combination of the 1st three elements of C % as its 1st row, and the complete 1st row of E as its 2nd row

% change element (2,2) of matrix F using the expression to the right

Creating Arrays and vectors (Examples continued)


>> G = F G= 5.0000 80.0000 27.0000 80.0000 11.1803 40.0000 >> G (1,:) ans = 5 80 >> H = G(1:2,:) H= 5 80 27 80 >> I = inv (H) I= -0.0455 0.0455 0.0153 -0.0028 >> J = eig (I) J= -0.0581 0.0098 % define matrix J as the eigen values of the square matrix I % define matrix I as the inverse of H % define matrix H as the complete 1st two rows of G % define matrix G as the transpose of F

% Choose the 1st row in matrix G

Creating Arrays and vectors


Examples (continue): >> J (:,2:3) = [1 2;3 4] J= -0.0581 0.0098 K= -0.0581 0.0098 L= 5.0000 27.0000 11.1803 80.0000 80.0000 40.0000 -0.0581 0.0098 1.0000 3.0000 2.0000 4.0000 % delete the complete 1st three rows of L by replacing the 1.0000 3.0000 2.0000 1.0000 3.0000 2.0000 4.0000 % add complete 2nd and 3rd columns to the existing matrix J

>> K = [J F]

% define matrix K as the combination of J and F


% setting next to each others 5.0000 27.0000 11.1803 % define matrix L as the combination of J and F % setting over each others 4.0000 80.0000 80.0000 40.0000

>> L = [F ; J]

>> L (1:3,:) = [ ]

L=
0.0098 L= 3 4 3.0000 4.0000 >> L (1) = [ ]

% rows with the empty matrix notation [ ]


% delete the 1st element of L by replacing its content with % the empty matrix notation [ ]

Built-in functions for handling arrays


Examples (continue): >> length_J = length (J) length_J = 3 % check the maximum number of elements in a row or a column

>> [row_J , column_J] = size (J)% return the size of J row_J = 2 column_J = 3 >>M = rand (3) M= 0.9218 0.7382 0.1763 0.4057 0.9355 0.9169 0.4103 0.8936 0.0579 % create a random matrix M of dimensions 3x3 >> O = [6 -12 20] O= 6 -12 20

>> N = diag(M) N= 0.9218 0.9355 0.0579

% create a vector of diagonal elements in M

>> 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): student_name = 'Robert Smith' student_name = Robert Smith >> student_name (8) ans = S

string _ name char (' string1' , ' string 2' , ' string 3' ,...)
>> student_info = char ('student info= , student_name , 'Grade , 'B+') student_info = student name Robert Smith Grade B+

The power of MATLAB: Orthonormalization


Examples (continue): >> Q = rand (4,5) Q= 0.9501 0.2311 0.6068 0.4860 % randomly define a 4x5 matrix Q 0.8913 0.7621 0.4565 0.0185 0.8214 0.4447 0.6154 0.7919 0.9218 0.7382 0.1763 0.4057 0.9355 0.9169 0.4103 0.8936 % find the orthonormal basis for the range of Q 0.3821 -0.5907 0.4073

>> R = orth (Q) R= -0.6878 -0.1787 -0.3438 -0.4195

-0.4824 -0.5830 -0.5114

0.2635 0.5761 0.6932 0.7475 -0.5105 -0.0679 % check that orth (Q)T x orth (Q) is the eye (rank (Q)) 0.0000 0.0000 0.0000 1.0000

>> S = R * R R= 1.0000 -0.0000 -0.0000 -0.0000 1.0000 -0.0000 -0.0000 -0.0000 0.0000 0.0000 1.0000 0.0000

How long does it take you to find orth (Q)? How many

Orthonormalization process

Thank you for the attention

Any Questions and Suggestions please

Você também pode gostar