Vectors and Matrices - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Vectors and Matrices

Description:

Fall 2006. AE6382 Design Computing. 1. Vectors and Matrices ... D = magic(5) diag(D) diag(diag(D)) Z = [magic(3),zeros(3,2), -ones(3,1); 4*ones(2,4), eye(2,2) ... – PowerPoint PPT presentation

Number of Views:313
Avg rating:3.0/5.0
Slides: 22
Provided by: jimc3
Category:

less

Transcript and Presenter's Notes

Title: Vectors and Matrices


1
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
2
Using Matlab with Arrays and Matrices
  • Matlabs 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
  • Matlabs fundamental data structure is the array
    and vectors and matrices follow easily
  • BUT to see some of the power of Matlab for
    engineering applications, well 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
    its often hard to avoid math in engineering)

3
Basic Concepts
Scalars magnitude only
x, mass, color, 13.451
Vectors magnitude AND direction
Arrays can be 2D or higher dimension
4
Matlab Can Handle This
Scalars
gtgt 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
Vectors
gtgt force12.3, 5.67 force 12.3000
5.6700 gtgt hvec1, 5, -3, 4, 0 hvec 1
5 -3 4 0
Arrays
gtgt coef1, 2 -4, 3 coef 1 2 -4
3
5
Basic Array Operations
  • Addition/subtraction CAB where cij aijbij
  • Multiplication/division CA . B where cij
    aijbij
  • Exponentiation CA . 4 where cij aij4

gtgt CAB C -3 3 -7 9
B -4 1 -3 6
A 1 2 -4 3
gtgt CA.B C -4 2 12 18
gtgt CA./B C -0.2500 2.0000 1.3333
0.5000
gtgt CA.2 C 1 4 16 9
6
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

gtgt A1 2 3 4 5 gtgt 2.A ans 2 4
6 8 10 gtgt 2A ans 2 4 6
8 10 gtgt B2 4 6 8 10 gtgt A.B ans 2
8 18 32 50 gtgt AB ??? Error using gt
Inner matrix dimensions must agree.
7
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(311) to
    index)

gtgt A1 2 3 4 5 gtgt sin(A) ans 0.8415
0.9093 0.1411 -0.7568 -0.9589 gtgt
sqrt(A) ans 1.0000 1.4142 1.7321
2.0000 2.2361
8
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

gtgt g(1)1 g(2)3 g(3)-4 g 1 3 -4
gtgt Aones(2,3) A 1 1 1 1
1 1 gtgt B-3ones(1,5) B -3 -3 -3
-3 -3 gtgt Czeros(2,3) C 0 0
0 0 0 0
9
Lets Build Some Arrays
What will these produce?
gtgt A3eye(2,2) A 3 0 0 3 gtgt
Bdiag(1 2 3 4) B 1 0 0 0
0 2 0 0 0 0 3 0
0 0 0 4 gtgt Cdiag(1 2 1,1) C
0 1 0 0 0 0 2 0
0 0 0 1 0 0 0 0 gtgt
diag(A) ans 3 3
D magic(5) diag(D) diag(diag(D)) Z
magic(3),zeros(3,2), -ones(3,1) 4ones(2,4),
eye(2,2) Z(,3) mess 10rand(4,5) messy
10randn(4,5) test 1./(3ones(2,3)
10
Vectors and Matrices
  • Weve 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?
11
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

12
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 Cramers Rule
  • Other?
  • Lets try a more abstract notation

OR
13
A Simple Example-contd
  • 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)
14
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 AB ?
    BA
  • 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 3y6 and you need to find y
  • The usual approach y6/32 (division by 3)
  • Also useful y3-162 (multiplication by the
    inverse of 3)
  • If we dont 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)

15
Lets Solve Our Problem Using Matlab
gtgt coef3 -2 1 4 coef 3 -2 1
4 gtgt inv(coef) Matlab has the inv()
function ans 0.2857 0.1429 -0.0714
0.2143 gtgt b14 -14' b 14 -14 gtgt
zinv(coef)b z 2 -4 gtgt coefz
Let's check our answer! ans 14 -14
16
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

NOTEC\C1, and1anythinganything
17
Lets Try This Out
coef 3 -2 1 4 gtgt b b 14
-14 gtgt zzcoef\b zz 2.0000 -4.0000
OK, now what do you think these expressions yield?
coef\eye(2,2) coef\eye(2,2)coef
18
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
19
Lets Try It Out in Matlab
gtgt coefTcoef' coefT 3 1 -2
4 gtgt bTb' bT 14 -14 gtgt
zTbTinv(coefT) zT 2 -4 gtgt ALSO WE
CAN USE RIGHT DIVIDE gtgt zT2bT/coefT zT2
2.0000 -4.0000
20
Other Matlab Matrix Functions
  • So far weve only scratched the surface of
    Matlabs 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.

21
Finally, What About Vectors?
  • The matrix and array operations and functions can
    be used to manipulate vectors, but youll have to
    be careful
  • Vector dot product
  • ON YOUR OWN
  • Vector magnitude?
  • Vector cross product?

gtgt f1 2' f 1 2 gtgt g4 -3' g
4 -3 gtgt fdotgf'g fdotg -2
gtgt f1 2 f 1 2 gtgt g4 -3 g
4 -3 gtgt fdotgfg' fdotg -2 gtgt
gdotfgf' gdotf -2
Column vectors
Row vectors
Write a Comment
User Comments (0)
About PowerShow.com