Lecture+22 - Engineering 101 Engineering 101 Lecture 22...

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: Engineering 101 Engineering 101 Lecture 22 11/20/07 Matrices in MATLAB Quote of the Day Quote of the Day An eye for an eye makes the whole world blind. ­ Mahatma Gandhi Announcements Announcements Project 6 is due tonight – 9pm Project 7 will be released after break New due dates: P7 – Wed, 12/5 P8 – Wed, 12/12 Creating Matrices Creating Matrices Matrices are automatically assumed to be in row order separated by commas or spaces. Rows can be separated by semicolons (;) or new lines. a = [1.0, 3.0, 5.0; 2.0, 4.0, 6.0] b = [1.0 3.0 5.0 2.0 4.0 6.0 ] Creating Matrices Creating Matrices The single quote after a matrix definition causes it to transpose v=[0 1 2 3] w=v’ Is equivalent to the statement w = [ 0; 1; 2; 3] or w = [ 0 1 2 3 ] Creating Matrices Creating Matrices The number of elements in every row must be the same and the number of elements in every column must be the same. b = [ 10 15 20; 6 9 ] error! Creating Matrices Creating Matrices An element of a matrix may be referenced using parentheses. Note that, unlike C++, rows and columns in MATLAB are numbered starting at 1. If c has not been previously created c(2, 3) = 5; Is equivalent to the statement c = [ 0 0 0 0 0 5 ]; Creating Matrices Creating Matrices You may also use algebraic operations or references to data in the definition of an array a = [ 0 1+1 1+3*4 ]; b = [ a(2) a(1) a ]; Is equivalent to a = [ 0 2 13 ]; b = [ 2 0 0 2 13 ]; Shortcut Expressions for Matrix Shortcut Expressions for Matrix Initialization If you want to create a vector in which the terms are regularly spaced you can use the colon operator. first:increment:last x = 1:2:8; Is equivalent to x = [ 1 3 5 7 ]; you can leave out the increment if it is 1 For example y=1:4 is the same as y=[1 2 3 4] Shortcut Expressions for Matrix Shortcut Expressions for Matrix Initialization You can turn a row into a column using the transpose operator (‘). x = [1:6]’ Is equivalent to x = [ 1; 2; 3; 4; 5; 6 ] Shortcut Expressions for Matrix Shortcut Expressions for Matrix Initialization If you want a matrix to have all zeros or all ones you can use a built­in function. zeros(n) creates an n x n matrix of zeros zeros(n, m) creates an n x m matrix of zeros ones(n) creates an n x n matrix of ones ones(n, m) creates an n x m matrix of ones eye(n) creates an n x n identity matrix eye(n, m) creates an n x m identity matrix Shortcut Expressions for Matrix Shortcut Expressions for Matrix Initialization You can use the size of a previous matrix as a template for a new matrix length(arr) returns the length of a vector or the longest dimension of a matrix size(arr) returns the rows and columns of an array x = [1 2 3; 4 5 6]; y = zeros(size(x)); News Flash! Did NSA Put a Secret Backdoor in New Encryption Standard? Random numbers are important parts of cryptography Dual_EC_DRBG, a random number generator based on elliptic curves, has a problem http://www.wired.com/politics/security/commentary/securitymat http://www.wired.com/politics/security/commentary/securityma News Flash! “The numbers have a relationship with a second, secret set of numbers that can act as a kind of skeleton key.” “If you know the secret numbers, you can predict the output of the random­number generator after collecting just 32 bytes of its output.” “Monitoring only one internet encryption connection is needed tocrack the security of the protocol. If you know the secret numbers, you can completely break any instantiation of Dual_EC_DRBG.” Who do you think might know those secret numbers??? News Flash! News Flash! My favorite random number generator: Lavarand! Getting Data From the User Getting Data From the User You can use the input function to prompt the user for input. val = input(‘Enter a number:’); The user can enter a number or an array in brackets If you need to get a string then val = input(‘Enter a string:’, ‘s’); Exercise Exercise After these commands b=? a = [7:2:11]’ b = [a a a] 1237 7 9 11 7 7 7 7 9 11 9 9 9 9 11 7 9 11 11 11 11 7 9 11 7 9 11 4- 7 9 11 7 9 11 7 9 11 Exercise Exercise After these commands b=? a = [7:2:11]’ b = [a a a] 1237 7 9 11 7 7 7 7 9 11 9 9 9 9 11 7 9 11 11 11 11 7 9 11 7 9 11 4- 7 9 11 7 9 11 7 9 11 Exercise Exercise a = [7:2:11]’ Exercise Exercise a = [7 9 11]’ Exercise Exercise a = 7 9 11 Exercise Exercise a = 7 9 11 b = [ a a a ] Exercise Exercise a = 7 9 11 b = 7 9 11 7 9 11 7 9 11 Exercise Exercise After these commands b=? b = 3 * eye(4); b(1, 4) = 3; b(4, 1) = 5; 1- 3 0 0 5 0 3 5 0 0 3 3 0 3 0 0 3 2- 3 0 0 3 0 3 3 0 0 5 3 0 5 0 0 3 3- 3 0 0 3 0 3 0 0 0 0 3 0 5 0 0 3 4- 3 0 0 5 0 3 0 0 0 0 3 0 3 0 0 3 Exercise Exercise After these commands b=? b = 3 * eye(4); b(1, 4) = 3; b(4, 1) = 5; 1- 3 0 0 5 0 3 5 0 0 3 3 0 3 0 0 3 2- 3 0 0 3 0 3 3 0 0 5 3 0 5 0 0 3 3- 3 0 0 3 0 3 0 0 0 0 3 0 5 0 0 3 4- 3 0 0 5 0 3 0 0 0 0 3 0 3 0 0 3 Exercise Exercise b = 3 * eye (4) b = 3 0 0 0 0 3 0 0 0 0 3 0 0 0 0 3 Exercise Exercise b = 3 * eye (4) b(1, 4) = 3 b = 3 0 0 0 0 3 0 0 0 0 3 0 3 0 0 3 Exercise Exercise b = 3 * eye (4) b(1, 4) = 3 b(4, 1) = 5 b = 3 0 0 5 0 3 0 0 0 0 3 0 3 0 0 3 Scalar Operations Scalar Operations The following operations are defined between two scalars: Addition Subtraction Multiplication Division Exponentiation a + b a – b a * b a / b a ^ b Scalar / Array Operations Scalar / Array Operations The same operations may be performed between an array and a scalar. In this case the operation is performed to every element of the array. Array vs. Matrix operations Array vs. Matrix operations When two arrays are being operated on MATLAB makes a distinction between array an matrix operations. Matrix operations are the standard Linear Algebra operations Array operations are done on an element­by­ element basis Array vs. Matrix operations Array vs. Matrix operations Array or Matrix operations (no difference) Addition Subtraction a + b a – b Array (Element­by­element) operations Multiplication Right Division Left Division Exponentiation a .* b a ./ b a .\ b a .^ b Array vs. Matrix operations Array vs. Matrix operations Matrix operations Multiplication Right Division Left Division Multiplication does standard matrix multiplication. The columns of a must equal the rows of b. Division inverts the denominator matrix and multiplies by the numerator. a / b b \ a a * b Match each expression to the matrix that corresponds to the result of the operation. a = [1 2 ; 5 –1 ] b = [0 1; 1 2 ] Exercise Exercise 1­ a+b A­ 1 4 1 ­3 2­ a­b B­ 0 5 2 ­2 3­ a.*b C­ 2 ­1 4­ a*b 5 3 D­ 1 6 3 1 Match each expression to the matrix that corresponds to the result of the operation. a = [1 2 ; 5 –1 ] b = [0 1; 1 2 ] 1 5 2 ­1 0 1 1 2 Exercise Exercise 1­ a+b A­ 1 4 1 ­3 2­ a­b B­ 0 5 2 ­2 3­ a.*b C­ 2 ­1 4­ a*b 5 3 D­ 1 6 3 1 Matrix operations Matrix operations Recall that we often want to solve problems where: [A] x = b That is where [A] is a matrix and b is a set of known quantities and we want to know x. We can solve this using MATLAB by the command: x = b / A Recall our General Statics Problem Recall our General Statics Problem W1 T1 T5 W 2 y x T6 T4 W/2 T3 W/2 OPEN 24 HOURS T2 Recall our General Statics Problem Recall our General Statics Problem W1 T1 T5 W 2 y x T1 + W1x = 0 − T1 − T5 / 2 + T2 / 2 = 0 W1y = 0 − T5 / 2 − T6 − T2 / 2 = 0 W2 x + T4 + T5 / 2 = 0 T6 T4 W/2 T3 T2 W2 y + T5 / 2 = 0 T3 − T4 = 0 − W / 2 + T6 = 0 − T3 − T2 / 2 = 0 − W / 2 + T2 / 2 = 0 W/2 OPEN 24 HOURS Convert to a Matrix Convert to a Matrix We were able to convert these equations into a matrix which could be solved by computer. ­1 1/√2 0 ­1/√2 00 00 10 00 0 ­1/√2 0 1/√2 00 00 0 0 0 0 0 0 ­1 0 1 0 0 ­1/√2 0 ­1/√2 1 1/√2 0 1/√2 00 00 00 00 ­1 0 00 0 ­1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 T1 T2 T3 T4 T5 = T6 W1x W1y W2x W2y 0 0 0 0 0 0 0 W/2 0 W/2 M­Files: MATLAB Programs M­Files: MATLAB Programs We can solve this problem by creating an M­ file. Once we create an M­file we can execute it by simply typing the name of the M­file leaving off the “.m” Comments in M­files are preceded by a % sign. cs = 1/sqrt(2.0); labels= ['T1 ';'T2 ';'T3 ';'T4 ';'T5 ';'T6 ';'W1x';'W1y';'W2x';'W2y‘]; matrix = [ -1 0 0 0 1 0 0 0 0 0 cs -cs 0 0 0 0 -cs cs 0 0 0 0 0 0 0 0 -1 0 1 0 0 0 1 0 0 0 0 0 -1 0 -cs -cs cs cs 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 ; ; ; ; ; ; ; ; ; ]; W = 1.0; b=[ 0 0 0 0 0 0 0 W/2.0 0 W/2.0 ]'; sol = matrix\b; Making Your Own MATLAB Making Your Own MATLAB Functions You can use an M­File to create a new function The syntax for defining a MATLAB function is: function [outputs] = functionname (inputs) % H1 Line % Help text % Help text Body C++ function and corresponding C++ function and corresponding MATLAB function double multiply( double a, double b){ return a*b; } function c = multiply( a, b) c = a*b C++ function and corresponding C++ function and corresponding MATLAB function void printthem( double a, double b){ cout << a << endl; cout << b << endl; return; } function printthem( a, b) a b C++ function and corresponding C++ function and corresponding MATLAB function void means( double a, double b, double &c, double & d){ c = sqrt(a*b); d = 0.5*(a+b); return; } function [c, d] = means( a, b) c = sqrt(a*b); d = 0.5*(a+b); Which is the MATLAB equivalent of the C++ function? void process (vector <double> list, double & g, vector <double> & h) { for(int x=0; x<list.size(); x++){ h[x] = list[x]*3.0; if (x==0 or g < h[x]) g= h[x]; } } 1­ Exercise Exercise 2­ 3­ Which is the MATLAB equivalent of the C++ function? void process (vector <double> list, double & g, vector <double> & h) { for(int x=0; x<list.size(); x++){ h[x] = list[x]*3.0; if (x==0 or g < h[x]) g= h[x]; } } 1­ Exercise Exercise 2­ 3­ Next Lecture Next Lecture MATLAB Programming ...
View Full Document

Ask a homework question - tutors are online