LINEAR ALGEBRA Matrix analysis Linear equations Eigenvalues and singular values Matrix functions.
-
Upload
frederick-mitchell -
Category
Documents
-
view
239 -
download
0
Transcript of LINEAR ALGEBRA Matrix analysis Linear equations Eigenvalues and singular values Matrix functions.
DAY-3
LINEAR ALGEBRA
LINEAR ALGEBRA
• Matrix analysis
• Linear equations
• Eigenvalues and singular values
• Matrix functions
MATRIX ANALYSIS
1.MATRIX DETERMINANT
Syntax
d = det(X)
Description
d = det(X) returns the determinant of the square matrix X.
Algorithms
The determinant is computed from the triangular factors obtained by Gaussian elimination
[L,U] = lu(A)
s = det(L) % This is always +1 or -1
det(A) = s*prod(diag(U))
EXAMPLE
• I=[1 2 3;9 4 5; 6 7 8]
I =
1 2 3
9 4 5
6 7 8
• det(I)
ans =
30.0000
VECTOR AND MATRIX NORMS• SYNTAX
n = norm(X,2)
n = norm(X)
n = norm(X,1)
n = norm(X,'fro')
• DESCRIPTION• The norm function calculates several different types of matrix and vector norms. If the input is
a vector or a matrix:
• n = norm(X,2) returns the 2-norm of X.• n = norm(X) is the same as n = norm(X,2).• n = norm(X,1) returns the 1-norm of X.
sqrt(sum(A)).• n = norm(X,'fro') returns the Frobenius norm of X.
sqrt(sum(diag(A'*A)))
RANK OF MATRIX
• Syntax
k = rank(A)
• Description
The rank function provides an estimate of the number of linearly independent rows or columns of a full matrix.
EXAMPLE• I=[1 2 3;4 5 6;0 0 0]
I =
1 2 3
4 5 6
0 0 0
• rank(I)
ans =
2
• I=[1 2 3;4 5 6;0 0 7]
I =
1 2 3
4 5 6
0 0 7• rank(I)
ans =
3
TRACE
• SUM OF DIAGONAL ELEMENTS • Syntax
b = trace(A)• Description
b = trace(A) is the sum of the diagonal elements of the matrix A.
• Algorithms
t = sum(diag(A));
EIGENVALUES AND EIGENVECTORS
• Syntax
d = eig(A)
d = eig(A,B)
[V,D] = eig(A)
• Description
d = eig(A) returns a vector of the eigenvalues of matrix A.
d = eig(A,B) returns a vector containing the generalized eigenvalues, if A and B are square matrices
[V,D] = eig(A) produces matrices of eigenvalues (D) and eigenvectors (V) of matrix A,
SINGULAR VALUE DECOMPOSITION
• In linear algebra, the singular value decomposition (SVD) is a factorization of a real or complex matrix, with many useful applications in signal processing and statistics.
• Applications of the SVD• Pseudoinverse• The singular value decomposition can be used
for computing the pseudoinverse of a matrix.
Cont,…• Let D be a data matrix:
D has n rows (one for each image)
D has c columns (c = number of pixels)
Basic result in linear algebra (when n > c)
D = U S V
where U = n x c matrix of weights
S = c x c diagonal matrix
V = c x c matrix, with rows = basis vectors
(also known as singular vectors or eigenvectors)
This is known as the singular value decomposition (SVD) of D
All matrices can be represented in this manner
c columns
n rowsD
Cont,…
• In MATLAB, we can calculate this using the svd.m function, i.e., [u, s, v] = svd(D);
• If matrix D is non-square, we can use svd(D,0)
EXAMPLE• i=[1 2 3;4 5 6;7 8 9]
i =
1 2 3
4 5 6
7 8 9• [u s v]=svd(i)
u =
-0.2148 0.8872 0.4082
-0.5206 0.2496 -0.8165
-0.8263 -0.3879 0.4082
s =
16.8481 0 0
0 1.0684 0
0 0 0.0000
v =
-0.4797 -0.7767 -0.4082
-0.5724 -0.0757 0.8165
-0.6651 0.6253 -0.4082
TRIGONOMETRIC
Cont,…
EXPONENTIAL
COMPLEX
ROUNDING AND REMAINDER
GENERATE LIST OF PRIME NUMBERS
• Syntax
p = primes(n)• Description
p = primes(n) returns a row vector of the prime numbers less than or equal to n. A prime number is one that has no factors other than 1 and itself.
• Examples
p = primes(37)
p = 2 3 5 7 11 13 17 19 23 29 31 37
LEAST COMMON MULTIPLE
• Syntax
L = lcm(A,B)• Description
L = lcm(A,B) returns the least common multiple of corresponding elements of arrays A and B. Inputs A and B must contain positive integer elements and must be the same size (or either can be scalar).
• Examples• lcm(8,40)
ans =
40
GREATEST COMMON DIVISOR • Syntax
G = gcd(A,B)• Description
G = gcd(A,B) returns an array containing the greatest common divisors of the corresponding elements of integer arrays A and B.
Examples
gcd(4,8)
ans =
4
ARRAY ELEMENTS THAT ARE PRIME NUMBERS
• Syntax
TF = isprime(A)• Description
TF = isprime(A) returns an array the same size as A containing logical 1 (true) for the elements of A which are prime, and logical 0 (false) otherwise. A must contain only positive integers.
• Examples• c = [2 3 0 6 10]
c =
2 3 0 6 10• isprime(c)
ans =
1 1 0 0 0
PRIME FACTORS
• Syntax
f = factor(n)• Description
f = factor(n) returns a row vector containing the prime factors of n.
• Examples• f = factor(123)
f =
3 41
POLYNOMIAL ROOTS
• Matlab also provides tools for manipulating polynomials and rational
functions.
– To use these tools, the polynomial should be represented as a vector with
the leftmost number being the highest power and the rightmost number
being the constant.
• For example, x² + 2x + 1 would be represented as [1 2 1].
– The r o o t s function gives the roots of the polynomial and polyval
evaluates the polynomial at the given value.
– Multiplying and dividing polynomials can be done with conv and deconv
Cont,…• Syntax
r = roots(c)
• Description• r = roots(c) returns a column vector whose elements are the roots of the polynomial c. • Row vector c contains the coefficients of a polynomial, ordered in descending powers. If
c has n+1 components, the polynomial it represents is c1sn + … + cns + cn + 1
• Examples• The polynomial s3 – 6s2 – 72s – 27 is represented in MATLAB software as• p = [1 -6 -72 -27]• The roots of this polynomial are returned in a column vector by• r = roots(p)
r =
12.1229
-5.7345
-0.3884
To multiply x² + 2x + 1 and x + 1, we use
Note that deconv will return two vectors, the first contains the coefficients for the quotient polynomial, and the second contains the coefficients for the remainder polynomial. The following example divides x3 + 3x² + 3x + 2 by x + 1
If the left hand side of the equation didn't contain two variables, the answer would only have the quotient and the remainder would be discarded.
• Matlab also has a function that will give the partial fraction decomposition of a rational function. – This is very useful when working with Laplace
transforms. – The function residue takes two polynomials and
returns the residues, the poles, and the direct term (quotient).
The partial fraction expansion of
(2s + 5) / (s3 + 5s² + 8s + 4) is found by
• There is a pole at –1 and a repeated pole at –2. • There is no direct term since the order of the
numerator was less than the order of the denominator.
– (2s + 5) / (s3 + 5s² + 8s + 4) = -3 / (s + 2) -1 / (s + 2)² + 3 / (s + 1)
NUMERICAL INTEGRATION AND DIFFERENTIATION
1. Function interpolation
2. Numerical integration
3. Numerical differentiation
Function interpolation
• The interpolation problem– Given values of an unknown function f(x) at values x =
x0, x1, …, xn, find approximate values of f(x) between these values
• Polynomial interpolation– Find nth-order polynomial pn(x) that approximates the
function f(x) and provides exact agreement at the n+1 node points:
– Can prove that the polynomial pn(x) is unique (see text)
– Interpolation: evaluate pn(x) for x0 < x < xn
– Extrapolation: evaluate pn(x) for x0 > x > xn
)()(),()(),()( 1100 nnnnn xfxpxfxpxfxp
MOTIVATION FOR POLYNOMIAL INTERPOLATION
• Practical– Polynomials are readily differentiated and integrated– Polynomials are linearly parameterized
• Theoretical – Weierstrass approximation theorem– Any continuous function f(x) can be approximated to
arbitrary accuracy on an interval with a polynomial pn(x) of sufficiently high order:
bxaJxxpxfIn n :)()(
nnn xaxaxaaxp 2
210)(
Lagrange Interpolation
• Linear interpolation– Interpolate the two points [x0,f(x0)], [x1,f(x1)]
– Lagrange polynomial
• Quadratic interpolation– Interpolate the three points [x0,f(x0)], [x1,f(x1)], [x2,f(x2)]
– Lagrange polynomial
)()()()(
)()()()()(
)()()()()()()(
111001
01
1001
01
011
101
00
10
111001
xfxpxfxp
baxxx
xfxxfxx
xx
xfxfxp
xfxx
xxxf
xx
xxxfxLxfxLxp
)()()()()()(
)())((
))(()(
))((
))(()(
))((
))(()(
)()()()()()()(
222112002
21202
101
2101
200
2010
212
22211002
xfxpxfxpxfxp
xfxxxx
xxxxxf
xxxx
xxxxxf
xxxx
xxxxxp
cbxaxxfxLxfxLxfxLxp
Lagrange Interpolation cont.
• General case
– Formulas for Lagrange polynomials given in text
• Error estimate– If f(x) has a continuous (n+1)-st derivative, then the polynomial
approximation has the error:
– The error is zero at the node points and small near the node points more node points improve accuracy
– The error may be large away from the node points extrapolation is very risky
)()(
)()()()()(
00k
n
k kk
kk
n
kkn xf
xl
xlxfxLxpxf
1
1
10
)(
)!1(
1)())(()()()(
n
n
nnn dx
tfd
nxxxxxxxpxfx
1-D data interpolation• Syntax
yi = interp1(x,Y,xi)
yi = interp1(Y,xi)
yi = interp1(x,Y,xi,method)
yi = interp1(x,Y,xi,method,'extrap')
Description
yi = interp1(x,Y,xi) interpolates to find yi, the values of the underlying function Y at the points in the vector or array xi. x must be a vector. Y can be a scalar, a vector, or an array of any dimension, subject to the following conditions:
If Y is a vector, it must have the same length as x. A scalar value for Y is expanded to have the same length as x. xi can be a scalar, a vector, or a multidimensional array, and yi has the same size as xi.
If Y is an array that is not a vector, the size of Y must have the form [n,d1,d2,...,dk], where n is the length of x. The interpolation is performed for each d1-by-d2-by-...-dk value in Y. The sizes of xi and yi are related as follows:
If xi is a scalar or vector, size(yi) equals [length(xi), d1, d2, ..., dk].
If xi is an array of size [m1,m2,...,mj], yi has size [m1,m2,...,mj,d1,d2,...,dk].
yi = interp1(Y,xi) assumes that x = 1:N, where N is the length of Y for vector Y, or size(Y,1) for matrix Y.
Cont,…yi = interp1(x,Y,xi,method) interpolates using alternative methods:
'nearest‘ -Nearest neighbor interpolation
'linear‘ -Linear interpolation (default)
'spline‘ -Cubic spline interpolation
'pchip‘ -Piecewise cubic Hermite interpolation
'cubic‘ -(Same as 'pchip')
For the 'nearest', 'linear', and 'v5cubic' methods, interp1(x,Y,xi,method) returns NaN for any element of xi that is outside the interval spanned by x. For all other methods, interp1 performs extrapolation for out of range values.
yi = interp1(x,Y,xi,method,'extrap') uses the specified interpolation algorithm specified by method to perform extrapolation for out of range values.(x,Y,xi,method)
EXAMPLE
• Generate a coarse sine curve and interpolate over a finer abscissa.
x = 0:10;
y = sin(x);
xi = 0:.25:10;
yi = interp1(x,y,xi);
plot(x,y,'o',xi,yi)
2-D DATA INTERPOLATION
• Syntax
ZI = interp2(X,Y,Z,XI,YI)
ZI = interp2(Z,XI,YI)• Description
ZI = interp2(X,Y,Z,XI,YI) returns matrix ZI containing elements corresponding to the elements of XI and YI and determined by interpolation within the two-dimensional function specified by matrices X, Y, and Z. X and Y must be monotonic, and have the same format ("plaid") as if they were produced by meshgrid. Matrices X and Y specify the points at which the data Z is given. Out of range values are returned as NaNs.
XI and YI can be matrices, in which case interp2 returns the values of Z corresponding to the points (XI(i,j),YI(i,j)). Alternatively, you can pass in the row and column vectors xi and yi, respectively. In this case, interp2 interprets these vectors as if you issued the command meshgrid(xi,yi)
ZI = interp2(Z,XI,YI) assumes that X = 1:n and Y = 1:m, where [m,n] = size(Z)
NUMERICALLY EVALUATE INTEGRAL
• Syntax
q = integral(fun,xmin,xmax)
q = integral(fun,xmin,xmax,Name,Value)
• Description
• q = integral(fun,xmin,xmax) approximates the integral of function fun from xmin to xmax using global adaptive quadrature and default error tolerances.
• q = integral(fun,xmin,xmax,name,value) specifies additional options with one or more Name,Valuepair arguments.
EXAMPLE
• Evaluate Improper Integral• Create the anonymous function
f(x) = e-x2(ln x)2.• fun = @(x) exp(-x.^2).*log(x).^2; • Evaluate the integral from x=0 to x=Inf.• q = integral(fun,0,Inf)
q = 1.9475
NUMERICALLY EVALUATE DOUBLE INTEGRAL
• Syntax
q = integral2(fun,xmin,xmax,ymin,ymax)
q = integral2(fun,xmin,xmax,ymin,ymax,Name,Value)
• Description
q = integral2(fun,xmin,xmax,ymin,ymax) approximates the integral of the function z = fun(x,y) over the planar region xmin ≤ x ≤ xmax and ymin(x) ≤ y ≤ ymax(x).
q = integral2(fun,xmin,xmax,ymin,ymax,Name,Value) specifies additional options with one or more Name,Value pair arguments.
EXAMPLE • Integrate Triangular Region with Singularity at the Boundary• The function
• is undefined when x and y are zero. integral2 performs best when singularities are on the integration boundary.
• Create the anonymous function.• fun = @(x,y) 1./( sqrt(x + y) .* (1 + x + y).^2 )• Integrate over the triangular region bounded by 0 ≤ x ≤ 1 and 0 ≤ y ≤ 1 – x.• ymax = @(x) 1 - x• q = integral2(fun,0,1,0,ymax)
q =
0.2854