LECTURE_4 _Matrices and Vectors(Cont.) QH (1)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Introduction to Computing

Lecture

Matrices and Vectors(Cont.)

Huynh Tan Quoc, PhD


Some useful array functions

Command Description
cat(n,A,B,C,…) Creates a new array by concatenating the arrays A,B,C
and so on along the dimension n.
find(x) Computes an array containing the indices of the nonzero
elements of the array x.
[u,v,w]=find(A) Computes the arrays u and v, containing the row and
column indices of the nonzero elements of the matrix A,
and the array w, containing the values of the nonzero
elements. The array w may be omitted.
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 mxn
matrix.
linspace(a,b,n) Creates a row vector of n regularly spaced values
between a and b.
logspace (a,b,n) Creates a row vector of n logarithmically spaced values
between a and b.
Some useful array functions

Command Description
max(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. If any of the
elements are complex, max(A) returns the elements that
have the largest magnitudes.
Similar to max(A) but stores the maximum values in the
[x,k]=max(A) row vector x and their indices in the row vector k.
min(A) Same as max(A) but returns minimum values.
[x,k]=min(A) Same as [x,k] = max(A) but returns minimum values.
size(A) Returns a row vector [m,n] containing the sizes of the
mxn array A.
sort(A) Sorts each column of the array A in ascending order and
returns an array the same size as A.
sum(A) Sums the elements in each column of the array A and
returns a row vector containing the sums.
Element-by-Element Operations

Command Description Form Example


+ Scalar-array addition A+b [6,3]+2=[8,5]
- Scalar-array subtraction A-b [8,3]-5=[3,-2]
+ Array addition A+B [6,5]+[4,8]=[10,13]
- Array subtraction A-B [6,5]-[4,8]=[2,-3]
.* Array multiplication A.*B [3,5].*[4,8]=[12,40]
./ Array right division A./B [2,5]./[4,8]=[2/4,5/8]
.\ Array left division A.\B [2,5].\[4,8]=[2\4,5\8]
.^ Array exponentiation A.^B [3,5].^2=[3^2,5^2]
2.^[3,5]=[2^3,2^5]
[3,5].^[2,4]=[3^2,5^4]
Matrix Operations

Special matrices
Null matrix 0: 0A=A0=0
Identity (Unity) matrix I: IA=AI=A
Command Description
eye(n) Creates an nxn identity matrix
eye(size(A)) Creates an identity matrix the same size as the matrix A
ones(n) Creates an nxn matrix of ones
ones(m,n) Creates an mxn array of ones
ones(size(A)) Creates an array of ones the same size as the array A
zeros(n) Creates an nxn matrix of zeros
zeros(m,n) Creates an mxn array of zeros
zeros(size(A)) Creates an array of zeros the same size as the array A
Matrix Operations

Ex:
ì10 0 £ x £ 2
Plot the function ï
f (x ) = í 0 2 £ x £ 5
ï- 3 5 £ x £ 7
î
Polynomial Operations Using Arrays

Command Description
conv(a,b) Computes the product of the two polynomials described by the
coefficient arrays a and b. The two polynomials need not be of the
same degree. The result is the coefficient array
of the product polynomial.
Computes the result of dividing a numerator polynomial, whose
[q,r] = coefficient array is num, by a denominator polynomial represented by
deconv (num,den) the coefficient array den. The quotient polynomial is given by the
coefficient array q, and the remainder polynomial is given by the
coefficient array r.
Computes the coefficients of the polynomial whose roots are specified
poly(r) by the vector r. The result is a row vector that contains the
polynomial’s coefficients arranged in descending order of power.

Evaluates a polynomial at specified values of its independent variable


polyval(a,x) x, which can be a matrix or a vector. The polynomial’s coefficients of
descending powers are stored in the array a. The result is the same
size as x.

Computes the roots of a polynomial specified by the coeffcient array a.


roots(a) The result is a column vector that contains the polynomial’s roots.
Polynomial Operations Using Arrays

f ( x ) = a1 x n + a2 x n -1 + a3 x n - 2 + ... + an -1 x 2 + an x + an +1
f(x): the function of x, degree(order): n
ai (i=1,1,….,n+1): polynomialʼs coefficients
→ using row vector to describe a polynomial
→ f(x) can be described as follows:
[a , a , a ,..., a
1 2 3 n -1 , an , an +1 ]

Ex: [4,-8,7,-5] represents for: 4 x - 8x + 7 x - 5


3 2
Polynomial Operations Using Arrays

ØTo find polynomial roots → roots (a)


(a): array contaning the polynomialcoefficients.
ØTo compute the coefficients of the polynomial
whose roots are specified by the array (a)
→ poly(a)
Polynomial Operations Using Arrays

Given 2 polynomials
f ( x ) = 9 x 3 - 5 x 2 + 3 x + 7 Þ f = [9,-5,3,7]
g ( x) = 6 x 2 - x + 2 Þ g = [6,-1,2]
Polynomial Operations Using Arrays

Given 2 polynomials

f x = 9 x - 5 x + 3 x + 7 Þ f = [9,-5,3,7]
( ) 3 2

g ( x) = 6 x 2 - x + 2 Þ g = [6,-1,2]
Polynomial Operations Using Arrays

Plotting Polynomials
Polyval(a,x): evaluates a polynomial at specified
values of its independent variable x, which can be
a matrix or a vector.
Ex: f ( x ) = 9 x - 5 x + 3 x + 7
3 2

Find the value of f(x) at x=0,2,4,…,10


Polynomial Operations Using Arrays

Plotting Polynomials
Plot the polynomial f(x) for 0£x£10
f (x ) = 9 x - 5 x + 3x + 7
3 2
Practice 1
Type this matrix in Matlab and use Matlab to answer the following
questions:
é 3 7 - 4 12ù
ê- 5 9 10 2 ú
A=ê ú
ê 6 13 8 11ú
ê ú
ë 15 5 4 1 û

1. Create a vector v consisting of the elements in the second column of A.


2. Create a vector w consisting of the elements in the second row of A.
3. Create a 4x3 array B consisting of all elements in the second through fourth
columns of A.
4. Create a 4x3 array B consisting of all elements in the second through fourth
rows of A.
5. Create a 2x3 array D consisting of all elements in the first two rows and the
last three columns of A.
Practice 2
Given the matrix:

é 3 7 - 4 12ù
ê- 5 9 10 2 ú
A=ê ú
ê 6 13 8 11ú
ê ú
ë 15 5 4 1 û

1. Find the minimum and maximum values in each column, row.


2. Sort each column and store the result in an array B.
3. Sort each row and store the result in an array C.
4. Add each column and store the result in an array D.
5. Add each row and store the result in an array E.
Practice 3
Given the matrices:

é- 7 16ù é 6 - 5ù é - 3 - 9ù
A=ê ú B=ê ú C=ê ú
ë 4 9 û ë12 - 2 û ë 6 8 û

1. Find A+B+C
2. Find A-B+C
3. Find A.B
4. Find D=A-B.C
5. Find D raised to the third power element-by-element
Practice 4

Use Matlab to confirm that:


(20 x 3
)( )
- 7 x 2 + 5 x + 10 4 x 2 + 12 x - 3 =
80 x 5 + 212 x 4 - 124 x 3 + 121x 2 + 105 x - 30

Plot the polynomial:


y = x 3 + 13 x 2 + 52 x + 6

over the range -7£x£1


Practice 5
Practice 6
Practice 7
Practice 8
Practice 9

You might also like