Fall 2006AE6382 Design Computing1 Vectors and Matrices Lecture Basic vector & matrix concepts Creating arrays and matrices Accessing matrix components.

Slides:



Advertisements
Similar presentations
Matrices and MATLAB Dr Viktor Fedun
Advertisements

Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 3.
Refresher: Vector and Matrix Algebra Mike Kirkpatrick Department of Chemical Engineering FAMU-FSU College of Engineering.
MF-852 Financial Econometrics
GG313 Lecture 12 Matrix Operations Sept 29, 2005.
MOHAMMAD IMRAN DEPARTMENT OF APPLIED SCIENCES JAHANGIRABAD EDUCATIONAL GROUP OF INSTITUTES.
Computer Graphics (Fall 2005) COMS 4160, Lecture 2: Review of Basic Math
Vectors and Matrices Class 17.1 E: Ch. 5.
Matrix Mathematics in MATLAB and Excel
Lecture 7: Matrix-Vector Product; Matrix of a Linear Transformation; Matrix-Matrix Product Sections 2.1, 2.2.1,
Intro to Matrices Don’t be scared….
CE 311 K - Introduction to Computer Methods Daene C. McKinney
Modified Gary Larson Far Side cartoon
College Algebra Fifth Edition James Stewart Lothar Redlin Saleem Watson.
1 Chapter 3 Matrix Algebra with MATLAB Basic matrix definitions and operations were covered in Chapter 2. We will now consider how these operations are.
1 Chapter 2 Matrices Matrices provide an orderly way of arranging values or functions to enhance the analysis of systems in a systematic manner. Their.
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Foundations of Computer Graphics (Fall 2012) CS 184, Lecture 2: Review of Basic Math
Chapter 10 Review: Matrix Algebra
Compiled By Raj G. Tiwari
1 9/15/03 CS1371 Introduction to Computing for Engineers Vectors and Matrices.
Graphics CSE 581 – Interactive Computer Graphics Mathematics for Computer Graphics CSE 581 – Roger Crawfis (slides developed from Korea University slides)
Sundermeyer MAR 550 Spring Laboratory in Oceanography: Data and Methods MAR550, Spring 2013 Miles A. Sundermeyer Linear Algebra & Calculus Review.
6.837 Linear Algebra Review Patrick Nichols Thursday, September 18, 2003.
Matlab tutorial course Lesson 2: Arrays and data types
Array Math.
Matrices And Linear Systems
Some matrix stuff.
Review of Matrices Or A Fast Introduction.
1 Week 3: Vectors and Matrices (Part III) READING: 2.2 – 2.4 EECS Introduction to Computing for the Physical Sciences.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
Array Addition  Two arrays can be added if and only if both arrays have exactly the same dimensions.  Assuming the dimension requirement is satisfied,
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Learner’s Guide to MATLAB® Chapter 2 : Working with Arrays.
Matrix. REVIEW LAST LECTURE Keyword Parametric form Augmented Matrix Elementary Operation Gaussian Elimination Row Echelon form Reduced Row Echelon form.
CMPS 1371 Introduction to Computing for Engineers MATRICES.
Lecture 28: Mathematical Insight and Engineering.
Multivariate Statistics Matrix Algebra I W. M. van der Veld University of Amsterdam.
Solving Linear Systems of Equations
Linear algebra: matrix Eigen-value Problems Eng. Hassan S. Migdadi Part 1.
P1 RJM 06/08/02EG1C2 Engineering Maths: Matrix Algebra 1 EG1C2 Engineering Maths : Matrix Algebra Dr Richard Mitchell, Department of Cybernetics AimDescribe.
A string is an array of characters Strings have many uses in MATLAB Display text output Specify formatting for plots Input arguments for some functions.
Fundamentals of Engineering Analysis
ES 240: Scientific and Engineering Computation. Chapter 8 Chapter 8: Linear Algebraic Equations and Matrices Uchechukwu Ofoegbu Temple University.
Introduction to Linear Algebra Mark Goldman Emily Mackevicius.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
Unit 3 Matrix Arithmetic IT Disicipline ITD 1111 Discrete Mathematics & Statistics STDTLP 1 Unit 3 Matrix Arithmetic.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
STROUD Worked examples and exercises are in the text Programme 5: Matrices MATRICES PROGRAMME 5.
Linear System of Simultaneous Equations Warm UP First precinct: 6 arrests last week equally divided between felonies and misdemeanors. Second precinct:
STROUD Worked examples and exercises are in the text PROGRAMME 5 MATRICES.
Computer Graphics Mathematical Fundamentals Lecture 10 Taqdees A. Siddiqi
Linear Algebra Review Tuesday, September 7, 2010.
Matrices. Matrix - a rectangular array of variables or constants in horizontal rows and vertical columns enclosed in brackets. Element - each value in.
Matrix Algebra Basics Chapter 3 Section 5. Algebra.
Matrices, Vectors, Determinants.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Part 3 - Chapter 8 Linear Algebraic Equations and Matrices.
MTH108 Business Math I Lecture 20.
Chapter 7 Matrix Mathematics
CS1371 Introduction to Computing for Engineers
Lecture 03: Linear Algebra
Unit 3: Matrices
CSE 541 – Numerical Methods
Matrices.
Matrix Algebra.
Laboratory in Oceanography: Data and Methods
Game Programming Algorithms and Techniques
Presentation transcript:

Fall 2006AE6382 Design Computing1 Vectors and Matrices Lecture Basic vector & matrix concepts Creating arrays and matrices Accessing matrix components Manipulating matrices Matrix functions Solving simultaneous equations Learning Objectives Understand the nature of matrices Understand how to manipulate matrices in Matlab

Fall 2006AE6382 Design Computing2 Using Matlab with Arrays and Matrices Matlab’s origins are in the early efforts to develop fast and efficient programs for handling linear equations… –Operations with arrays, vectors and matrices are needed –Only the most computationally efficient routines are used –Matlab is very “C-like” but adds a number of operators and extends its syntax to handle a range of array, vector and matrix operations –Matlab’s fundamental data structure is the array and vectors and matrices follow easily –BUT… to see some of the power of Matlab for engineering applications, we’ll have to dig a bit more deeply into some of the underlying math (no, this is not going to turn into a math class, but it’s often hard to avoid math in engineering)

Fall 2006AE6382 Design Computing3 Basic Concepts Scalars: magnitude only Vectors: magnitude AND direction Arrays: can be 2D or higher dimension x, mass, color,

Fall 2006AE6382 Design Computing4 Matlab Can Handle This… Scalars: Vectors: Arrays: >> whos Name Size Bytes Class a 1x1 8 double array density 1x1 8 double array mass 1x1 8 double array resistance 1x1 8 double array s 1x1 8 double array stress 1x1 8 double array >> force=[12.3, 5.67] force = >> hvec=[1, 5, -3, 4, 0] hvec = >> coef=[1, 2; -4, 3] coef =

Fall 2006AE6382 Design Computing5 Basic Array Operations Addition/subtraction: C=A+B where c ij = a ij +b ij Multiplication/division: C=A.* B where c ij = a ij *b ij Exponentiation: C=A.^ 4 where c ij = a ij 4 A = B = >> C=A+B C = >> C=A.*B C = >> C=A./B C = >> C=A.^2 C =

Fall 2006AE6382 Design Computing6 Notes on Array Operations Arithmetic operations on arrays are just like the same operations for scalars but they are carried out on an element-by-element basis. –the dot (.) before the operator indicates an array operator; it is needed only if the meaning cannot be automatically inferred. –when combining arrays, make sure they all have the same dimensions –applies to vectors, 2D arrays, multi-dimensional arrays >> A=[ ]; >> 2.*A ans = >> 2*A ans = >> B=[ ]; >> A.*B ans = >> A*B ??? Error using ==> * Inner matrix dimensions must agree.

Fall 2006AE6382 Design Computing7 More Notes on Array Operations Most Matlab functions will work equally well with both scalars and arrays (of any dimension) Use brackets […] to construct arrays Use colon notation (e.g., A(:,2) or f(3:11) to index) >> A=[ ]; >> sin(A) ans = >> sqrt(A) ans =

Fall 2006AE6382 Design Computing8 Array Constructors Arrays are often read into Matlab from files or entered by the user… But building arrays from scratch can be tedious –Explicit: –Using Matlab array constructors: >> g(1)=1; g(2)=3; g(3)=-4 g = >> A=ones(2,3) A = >> B=-3*ones(1,5) B = >> C=zeros(2,3) C = 0 0 0

Fall 2006AE6382 Design Computing9 Let’s Build Some Arrays… >> A=3*eye(2,2) A = >> B=diag([ ]) B = >> C=diag([1 2 1],1) C = >> diag(A) ans = 3 What will these produce? D = magic(5) diag(D) diag(diag(D)) Z = [magic(3),zeros(3,2), -ones(3,1); 4*ones(2,4), eye(2,2)] Z(:,3)=[] mess = 10*rand(4,5) messy = 10*randn(4,5) test = 1./(3*ones(2,3)

Fall 2006AE6382 Design Computing10 Vectors and Matrices We’ve referred to vectors and matrices frequently… but exactly what are we talking about? –what is a matrix? –is it different from an array? ANSWER: –vectors and matrices are arrays with an “attitude” –that is, they look just like an array (and they are arrays), but they live by a very different set of rules! –Vectors: Can you explain what, if anything, results from these operations with vectors?

Fall 2006AE6382 Design Computing11 Why Matrices? A matrix is an array that obeys a different set of rules –addition & subtraction are same as for arrays, –but multiplication, division, etc. are DIFFERENT! –a matrix can be of any dimension but 2D square matrices are the most common by far A large and very useful area of mathematics deals with what is called “linear algebra” and matrices are an integral part of this. Many advanced computational methods in engineering make extensive use of linear algebra, and hence of matrices

Fall 2006AE6382 Design Computing12 A Simple Example A set of simultaneous linear algebraic equations will often arise in engineering applications How do you solve these? –Solve first for x in terms of y; substitute in second and solve for y; use this in first to find x –Use “Cramer’s Rule” –Other? Let’s try a more abstract notation: OR

Fall 2006AE6382 Design Computing13 A Simple Example-cont’d What do we mean by the * for this form? –Note that the column matrix, z, is multiplied times the first row of C on an element-by-element basis and the results are summed to get the first row of the answer –Ditto for the second row… –This is NOT array multiplication; it is matrix multiplication For two 2D matrices in general: NOTE: the number of columns in A must be equal to the number of rows in B (N in this example)

Fall 2006AE6382 Design Computing14 A Few Notes on Matrices Matlab handles matrix multiplication with the * symbol (NOTE: this is NOT array multiplication!) –From our formula we see that in general: A*B  B*A –In other words, matrix multiplication is NOT commutative Matrices behave just like arrays for addition and subtraction Matrix division is not strictly defined but a matrix inverse is available to address this situation, among others. –suppose: 3y=6 and you need to find y… –The usual approach: y=6/3=2 (division by 3) –Also useful: y=3 -1 *6=2 (multiplication by the inverse of 3) –If we don’t know how to divide, we can accomplish the same by using the notion of the inverse. Recall definition of inverse: –Turns out we know how to compute matrix inverses (but it requires a lot of computational effort)

Fall 2006AE6382 Design Computing15 Let’s Solve Our Problem Using Matlab >> coef=[3 -2; 1 4] coef = >> inv(coef) % Matlab has the inv() function ans = >> b=[14 -14]' b = >> z=inv(coef)*b z = 2 -4 >> coef*z % Let's check our answer! ans =

Fall 2006AE6382 Design Computing16 Some More Notes: Using the Matlab inv() function is not always best –It can take a VERY long time for large matrices –The inverse may have poor precision for some kinds of matrices If you just want to solve the set of equations, there are much quicker and more accurate methods –Uses powerful algorithms from linear algebra –Notation is tricky because it introduces the concept of a “left” and a “right” matrix division in Matlab NOTE: C\C=1, and 1*anything=anything

Fall 2006AE6382 Design Computing17 Let’s Try This Out… coef = >> b b = >> zz=coef\b zz = OK, now what do you think these expressions yield? coef\eye(2,2) coef\eye(2,2)*coef

Fall 2006AE6382 Design Computing18 Things Can Get Weird… We usually think of the unknown (z) as a column matrix and the RHS (b) as a column matrix also In some fields, it is more useful if these are ROW matrices –One formulation can easily be converted into the other! –We can treat either formulation in Matlab First, ON YOUR OWN, prove from our multiplication formula that: Now, using this, we take the transpose of our equation: where

Fall 2006AE6382 Design Computing19 Let’s Try It Out in Matlab: >> coefT=coef' coefT = >> bT=b' bT = >> zT=bT*inv(coefT) zT = 2 -4 >> % ALSO WE CAN USE RIGHT DIVIDE: >> zT2=bT/coefT zT2 =

Fall 2006AE6382 Design Computing20 Other Matlab Matrix Functions So far we’ve only scratched the surface of Matlab’s abilities to work with matrices… Matrices can contain COMPLEX numbers Some of the other matrix functions are: –det(A): determinant of the matrix –rank(A): rank of the matrix –trace(A): sum of diagonal terms –sqrtm(A): matrix square root (i.e., sqrtm(A)*sqrtm(A)=A) –norm(A): matrix norm (useful for vector magnitudes) –eig(A): eigenvalues and eigenvectors of matrix –… Keep in mind that Matlab is using some of the latest and most powerful algorithms to compute these functions.

Fall 2006AE6382 Design Computing21 Finally, What About Vectors? The matrix and array operations and functions can be used to manipulate vectors, but you’ll have to be careful Vector dot product: ON YOUR OWN: –Vector magnitude? –Vector cross product? >> f=[1 2]' f = 1 2 >> g=[4 -3]' g = 4 -3 >> fdotg=f'*g fdotg = -2 >> f=[1 2] f = 1 2 >> g=[4 -3] g = 4 -3 >> fdotg=f*g' fdotg = -2 >> gdotf=g*f' gdotf = -2 Column vectors Row vectors