Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
75 views

Arrays and Matrices

The document discusses arrays and matrices in MATLAB. It defines arrays as collections of numbers that can be treated as a single variable. Matrices are two-dimensional arrays with rows and columns. The document provides examples of how to create row and column vectors, as well as matrices using brackets and semicolons. It also discusses array operations like addition, subtraction, and element-by-element operations. Finally, it introduces solving systems of linear equations and eigenvalue problems using matrices.

Uploaded by

Marian Celeste
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Arrays and Matrices

The document discusses arrays and matrices in MATLAB. It defines arrays as collections of numbers that can be treated as a single variable. Matrices are two-dimensional arrays with rows and columns. The document provides examples of how to create row and column vectors, as well as matrices using brackets and semicolons. It also discusses array operations like addition, subtraction, and element-by-element operations. Finally, it introduces solving systems of linear equations and eigenvalue problems using matrices.

Uploaded by

Marian Celeste
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

ARRAYS AND MATRICES

One of the strengths of MATLAB is the capability to handle collections of numbers, called arrays, as if they were a single variable. Single numbers are called scalars, whereas, arrays are collections of scalars arranged in a specific order. An example of an array variable is one that contains the numbers 0, 1, 3, and 6, in the order. We can use square brackets to define the variable x to contain this collection by typing >>x = [ 0, 1, 3, 6].

The elements of the array may be separated by commas or spaces. A one-column array or one-row array is called vector. Thus, v1 = [5 7 2] is a row-vector 5 and 2 = 7 is a column vector. 2

In general, array can have more than one column and more than one row. Furthermore, a two-dimensional array is called a matrix. An m x n matrix with m row and n columns. Example: 2 3 = 6 0 is a 3 x 2 matrix. 1 2

CREATING ARRAYS
A. ROW VECTOR
Type the elements inside a pair of square brackets, separating the elements inside with a space or a comma. Example: >>A = [10 3 4 6] or >>A = [10, 3, 4, 6]

B. COLUMN VECTOR
Separate the elements by semicolons or create a row vector first and then use the transpose notation ( ) which converts a row vector into a column vector, or vice versa. Example: >>B=[5; 3; 6; 0; 2] or >>B=[5, 3, 6, 0, 2]

Another way is to type a left bracket and the first element, press ENTER, type the second element, press ENTER, and so on until you type the last element followed by the right bracket. Example: >>B=[5 3 6 0 2]

C. MATRICES
Type the matrix row by row, separating the elements in a given row with spaces or commas and separating the rows with semicolons. Example: 7 4 3 To create = , type 1 2 4 >>A=[7, 4, 3; 1, 2, 4]

APPENDING ARRAYS
Array can be created by appending one array to anther. Illustration: 1. Let array1 = [4, 6, 2] and array2 = [2, 5]. Type >>array3 = [array1, array2] 4 2 6 2 4 2. Let = and = 7 0 9 5 1 Typing >>C = [A , B]

SPECIAL WAY OF CREATING MATRICES


1. Using : The colon ( : ) generates a vector of regularly spaced elements. Typing >>x = [m: q: n] creates a vector x with first value m and last value n with a spacing q. The last value is less than n if m n is an integer not multiple of q.

Example:
>>x = [2: 2: 8] >>y = [1: 3: 11] >>z = [20: 2: 10] If q is omitted, it is presumed to be 1. Example: >>w = [3: 2]

2. Using linspace Linspace command creates a linearly spaced row vector by specifying the number of elements rather than the increment. The syntax >>x = [a, b, n] means creating a vector x where a and b are the lower and upper limits, respectively and n is the number of elements. Example: >>linspace (5, 8, 31). It contain 31 elements. If n is omitted, the default value is 100.

3. Using logspace It creates an array of logarithmically spaced elements. The syntax is >>logspace (a, b, n) where n is the number of elements between 10a and 10b. Example: >>x = logspace (1, 1, 6) If n is omitted, the number of elements defaults to 50.

SOME USEFUL ARRAY FUNCTIONS


Functions magic(n) Descriptions creates an n x n matrix where the sum of its diagonal, rows and columns are equal creates an n x n identity matrix creates an n x n matrix of ones creates an m x n matrix of ones creates an n x n matrix of zeros creates an m x n matrix of zeros

eye(n) ones(n) ones(m,n) zeros(n) zeros(m,n)

size(A)

returns a row vector [m, n] containing the sizes of the m x n array A length(A) computes either the number of elements of A if A is a vector or the largest value of m or n if A is an m x n matrix sum(A) sums the elements in each column of the array A and returns a row vector containing the sums sort(A) sorts each column of the array A in ascending order and returns an array the same size as A

max(A)

min(A)

returns the algebraically largest element in A if A is a vector. Returns a row vector containing the largest elements in each column if A is a matrix same as max(A) but returns minimum values

diag(A)

returns the main diagonal of A

Practice Set: 1. Let A = [1, 10, 6, 4; 7, 2, 5, 11; 12, 0, 0, 8; 1, 3, 5, 6]. Find: sum (A), max(A), min(A), length(A), size(A), sort(A), diag(A) 2. Let B = magic(6). Verify that the sum of the rows, columns and diagonals are equal. 3. Create an identity matrix with a size as matrix A in problem 1.

ARRAY ADDRESSING/INDEXING
Array indices are the row and column numbers of an element in an array and are used to keep track of the arrays elements. Illustration:
Address v(2) Description refers to 2nd element in the vector v

A(3,4)
v(:)

refers to the element in row 3, column 4


represents all the row or column elements of the vector v

represents the 1st through 3rd elements of v A(:, 2) denotes all the elements in the 2nd column of A A(:, 1:4) denotes all the elements in the 1st through 4th column of A A(3, :) denotes all the elements in the 3rd row of A A(2:4, :) denotes all the elements in the 2nd through 4th row of A A(2:3,1:3) denotes all the elements in the 2nd through 3rd row that are also in the 1st v(1:3)

Practice Set: 1. Let v = [4, 2, 6, 0, 8] and A = [1, 7, 4, 0; 1, 5, 3, 2; 3, 1, 8, 5]. Determine the output of the following: a. v(5) b. v(2:4) c. A(2,4) d. A(3,2) e. A(:, 3) f. A(:, 2:4) g. A(2,:) h. A(2:3, :)

i. A(2:3, 3:4) j. A(2,4) = 6 k. A(1, 5) = 2 l. B = A(:, 4: 1:2) m. C = [1 1 1]; B(2, :) = C

ARRAY OPERATIONS (Element-by-element operations)


Symbols + + .* ./ .\ .^ Operations Scalar-array addition Scalar-array subtraction Array addition Array subtraction Array multiplication Array right division Array left division Array exponentiation

Practice Set: 1. Given: A = [2, 3] B = [6, 7] b = 6 Find: A + b, A b, A + B, A B, A .* B, A ./ B, A .\ B, A .^ b, A .^ B 2. Given: 1 3 2 3 5 7 = 2 6 0 = 0 5 10 0 4 1 1 2 3 0 0 0 3 5 6 = = 0 0 0 4 1 2


Find: A + B, C + D, A + C, B A, C B

3 2 1 3. Given: = = 5 4 2 Find: 4, 2,
1 2

4 6

4. Given:
2 3 1 1 5 = 0 4 2 = 2 2 1 5 1 3 1 0 1 0 = 4 4 2 2 6 5

Find: AB, BA, CA, AC

MATRIX OPERATIONS
Applies the same rule in ordinary matrices operations in Mathematics. Note: 1. + and apply the same rule as in elementby-element operations. 2. A/B = A*B-1 , where B-1 is the inverse of B. 3. A^2 = A*A 4. AB is not defined .

Example: Let M = [1, 3, 6; 2, 4, 0; 5, 8, 9], N = [4, 2; 6, 3; 1, 5] and O = [2, 2, 1; 3, 4, 0; 1, 3, 7]. Find: a. MN b. M2 c. Show that MM-1=1 d. Show that M/O = MO-1
Use the command inv(A) to evaluate the inverse of matrix A.

Determinants
Use the command det(A) to evaluate the determinant A. Examples: Evaluate the following determinants:
3 a. 9 5 12 0 2 3 0 c. 6 4 3 2 5 5 0 5 4 4 7 0

2 3 1 b. 1 1 2 3 4 1

Eigenvalues and Eigenvectors

Solution of Linear Systems of Algebraic Equations


Consider a linear system of algebraic equations with equations and unknowns:

11 1 + 12 2 + + 1 = 1 21 1 + 22 2 + + 2 = 2 1 1 + 2 2 + + =

Solution of Linear Systems of Algebraic Equations


In these equations, aij and bi are constants, and the unknowns are xi. We can rewrite the equations as: = where
11 21 = 1 12 1 1 , = , =

Solution of Linear Systems of Algebraic Equations


We can find all the unknowns in the vector x by doing a few simple matrix manipulations. If we premultiply both sides of the matrix equation by the inverse of the A matrix we get: = 1 = 1 = 1 = 1 which gives us our solution.

An Example
Consider the system of 3 equations and 3 unknowns: 21 + 32 3 = 1 1 + 22 + 33 = 9 2 + 23 = 5
2 = 1 0 3 1 1 1 2 3 , = 9 , so = 1 = 1 1 2 5 2

An Example
Using MatLab: >> A=[2 3 -1;-1 2 3;0 1 2] A= 2 3 -1 -1 2 3 0 1 2 >> b=[-1 9 5]' b= -1 9 5

An Example
Using MatLab: >> x=inv(A)*b x= -1 1 2

Eigenvalues and Eigenvectors


Many problems present themselves in terms of an eigenvalue problem: = In this equation A is an n-by-n matrix, v is a non-zero n-by-1 vector and is a scalar (which may be either real or complex). Any value of for which this equation has a solution is known as an eigenvalue of the matrix A. It is sometimes also called the characteristic value.

Eigenvalues and Eigenvectors


The vector, v, which corresponds to this value is called an eigenvector. The eigenvalue problem can be rewritten as = 0 = 0 = 0 If v is non-zero, this equation will only have a solution if = 0

Eigenvalues and Eigenvectors


= 0 This equation is called the characteristic equation of A, and is an nth order polynomial in with n roots. These roots are called the eigenvalues of A. We will only deal with the case of n distinct roots, though they may be repeated. For each eigenvalue there will be an eigenvector for which the eigenvalue equation is true.

Example
0 1 If = 2 3

then the characteristic equation is 0 = 2 1 3 0 0 =0

1 = 2 + 3 + 2 = 0 2 3

Example
and the two eigenvalues are 1 = 1, 2 = 2

All that's left is to find the two eigenvectors. Let's find the eigenvector, v1, associated with the eigenvector, 1, first. 1 = 1 1 = 0 1 1 = 0 2 3 1 1,1 1 1 =0 1,2 2 2

Example
so clearly

1,1 + 1,2 = 0, and 2 1,1 + 2 1,2 = 0, so 1,1 = 1,2 and the first eigenvector is any 2 element column vector in which the two elements have equal magnitude and opposite sign. +1 1 = 1 1 where k1 is an arbitrary constant. Note that we didn't have to use +1 and -1, we could have used any two quantities of equal magnitude and opposite sign.

Example
Going through the same procedure for the second eigenvalue: = 2 2,1 2 1 =0 2,2 2 1 2 2,1 + 2,2 = 0, and 2 2,1 + 2,2 = 0, so 2 2,1 = 2,2 and +1 2 = 2 2

Using Matlab
A=[0 1;-2 -3] A= 0 1 -2 -3 [v,d]=eig(A) v= 0.7071 -0.4472 -0.7071 0.8944 d= -1 0 0 -2

Using Matlab
The eigenvalues are the diagonal of the "d" matrix; 1=-1, 2=-2. The eigenvectors are the columns of the "v" matrix. Note that MatLab chose different values for v1,1, etc..., but that the ratio of v1,1 to v1,2 and the ratio of v2,1 to v2,2 are the same as our solution. (MatLab chooses the values such that the sum of the squares of the elements of the eigenvector equals unity).

You might also like