Download presentation
Presentation is loading. Please wait.
Published byFrederick Mitchell Modified over 9 years ago
2
LINEAR ALGEBRA Matrix analysis Linear equations Eigenvalues and singular values Matrix functions
3
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))
4
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
5
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)))
6
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.
7
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
8
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));
9
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,
10
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.
11
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 rows D
12
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)
13
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
14
TRIGONOMETRIC
15
Cont,…
16
EXPONENTIAL
17
COMPLEX
18
ROUNDING AND REMAINDER
19
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
20
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
21
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
22
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
23
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
24
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
25
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
26
To multiply x² + 2x + 1 and x + 1, we use
27
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.
28
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).
29
The partial fraction expansion of (2s + 5) / (s3 + 5s² + 8s + 4) is found by
30
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)
31
NUMERICAL INTEGRATION AND DIFFERENTIATION 1.Function interpolation 2.Numerical integration 3.Numerical differentiation
32
Function interpolation The interpolation problem – Given values of an unknown function f(x) at values x = x 0, x 1, …, x n, find approximate values of f(x) between these values Polynomial interpolation – Find nth-order polynomial p n (x) that approximates the function f(x) and provides exact agreement at the n+1 node points: – Can prove that the polynomial p n (x) is unique (see text) – Interpolation: evaluate p n (x) for x 0 < x < x n – Extrapolation: evaluate p n (x) for x 0 > x > x n
33
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 p n (x) of sufficiently high order:
34
Lagrange Interpolation Linear interpolation – Interpolate the two points [x 0,f(x 0 )], [x 1,f(x 1 )] – Lagrange polynomial Quadratic interpolation – Interpolate the three points [x 0,f(x 0 )], [x 1,f(x 1 )], [x 2,f(x 2 )] – Lagrange polynomial
35
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
36
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.
37
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)
38
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)
39
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)
40
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.
41
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
42
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.
43
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.