190 likes | 292 Views
CHAPTER (2). MATRIX OPERATIONS. Matrix Algebra with MATLAB. MATRIX is a two-dimensional, rectangular shaped data structure capable of storing multiple elements of data in an easily accessible format. These data elements can be numbers, characters, logical states of true or false.
E N D
CHAPTER (2) MATRIX OPERATIONS Eng. Mohamed EltaherEng.Ahmed Ibrahim
Matrix Algebra with MATLAB • MATRIX is a two-dimensional, rectangular shaped data structure capable of storing multiple elements of data in an easily accessible format. These data elements can be numbers, characters, logical states of true or false. • MATLAB is a matrix-based computing environment. All of the data that you enter into MATLAB is stored in the form of a matrix or a multidimensional array. Even a single numeric value like 100 is stored as a matrix. • >> A=100; • >> whos A • Name Size Bytes Class • A 1x1 8 double Eng. Mohamed EltaherEng.Ahmed Ibrahim
Constructing a Simple Matrix • The simplest way to create a matrix in MATLAB is to use the matrix constructor operator, [ ]. • Entering a Matrix MATLAB Format • >> A = [2 -3 5; -1 4 5] • A = • 2 -3 5 • -1 4 5 Eng. Mohamed EltaherEng.Ahmed Ibrahim
Entering a Row Vector in MATLAB MATLAB Format • >> x = [1 4 7] or >> x = [1, 4, 7] • x = • 1 4 7 • Entering a Column Vector in MATLAB MATLAB Format • >> x = [1; 4; 7]; • or • >> x = [1 4 7]' Eng. Mohamed EltaherEng.Ahmed Ibrahim
Specialized Matrix Functions • MATLAB has a number of functions that create different kinds of matrices. Eng. Mohamed EltaherEng.Ahmed Ibrahim
Getting Information About a Matrix • Dimensions of the Matrix • These functions return information about the shape and size of a matrix. Eng. Mohamed EltaherEng.Ahmed Ibrahim
Matrix Addition and Subtraction • Matrix addition and subtraction with MATLAB are achieved in the same manner as with scalars provided that the matrices have the same size. Typical expressions are shown below. • >> C = A + B • >> D = A - B • MATLAB has many error messages that indicate problems with operations. If the matrices have different sizes, the message is • ??? Error using ==> • Matrix dimensions must agree. Eng. Mohamed EltaherEng.Ahmed Ibrahim
Addition • Commutative: A+B=B+A • Associative: (A+B)+C=A+(B+C) • Subtraction • By adding a negative matrix Eng. Mohamed EltaherEng.Ahmed Ibrahim
Matrix Functions Eng. Mohamed EltaherEng.Ahmed Ibrahim
Example: - Given a matrix »mat=[1 2 -3;-3 -1 1;1 -1 1]; • Calculate the rank of a matrix »r=rank(mat); the number of linearly independent rows or columns Calculate the determinant »d=det(mat); mat must be square ! if determinant is nonzero, matrix is invertible • Get the matrix inverse »E=inv(mat); Eng. Mohamed EltaherEng.Ahmed Ibrahim
Two vectors: Inner product = scalar • Vector Products Inner product XTY is a scalar (1xn) (nx1) Outer product = matrix • Outer product XYT is a matrix (nx1) (1xn) Eng. Mohamed EltaherEng.Ahmed Ibrahim
Example:- • >> u = [3; 1; 4]; • >> v = [2 0 -1]; • >> x = v*uscalar • x = • 2 • >> X = u*vmatrix • >> X = • 6 0 -3 • 2 0-1 • 8 0 -4 Eng. Mohamed EltaherEng.Ahmed Ibrahim
Every operation in MATLAB is matrix operation by default • Non-matrix operation is generalized to matrix operation as element-wise operation (sin, cos, etc.) • To convert matrix operation to element-wise operation, add dot in front (.*,.^). >> A=ones(2); >> A.^2 ans = 1 1 1 1 >> A=ones(2); >> A^2 ans = 2 2 2 2 Eng. Mohamed EltaherEng.Ahmed Ibrahim
Transpose • The Transpose Operationinterchanges aij and aji. MATLAB uses the apostrophe operator (') to perform a complex conjugate transpose, and uses the dot-apostrophe operator (.') to transpose without conjugation. For matrices containing all real elements, the two operators return the same result. • The example matrix A is symmetric, so A' is equal to A. But B is not symmetric: • >>B = magic(3); • >>X = B' • Transposition turns a row vector into a column vector: • >>v = [2 0 -1]; • >>x = v' Eng. Mohamed EltaherEng.Ahmed Ibrahim
For a complex vector or matrix, z, the quantity z' not only transposes the vector or matrix, but also converts each complex element to its complex conjugate. That is, the sign of the imaginary part of each complex element changes. So if • >>z = [1+2i 7-3i 3+4i; 6-2i 9i 4+7i] • >>z' • ans = • 1.0000 - 2.0000i 6.0000 + 2.0000i • 7.0000 + 3.0000i 0 - 9.0000i • 3.0000 - 4.0000i 4.0000 - 7.0000i • The unconjugated complex transpose, where the complex part of each element retains its sign, is denoted by z.': • >>z.' • ans = • 1.0000 + 2.0000i 6.0000 - 2.0000i • 7.0000 - 3.0000i 0 + 9.0000i • 3.0000 + 4.0000i 4.0000 + 7.0000i Eng. Mohamed EltaherEng.Ahmed Ibrahim
Sets of Linear Equation • One of the most important problems in technical computing is the solution of systems of simultaneous linear equations. For example, consider the set of equations:- • The less favorable, but more straightforward method is to take • The preferable solution is found using the matrix left-division operator The residual error can be computed by Eng. Mohamed EltaherEng.Ahmed Ibrahim
Example:- Given a system of linear equations x+2y-3z=5 -3x-y+z =-8 x- y+ z =0 Solution • »A=[1 2 -3;-3 -1 1;1 -1 1]; • »b=[5;-8;0]; and solve with a single line of code! »x=A\b; x is a 3x1 vector containing the values of x, y, and z. the residual will be »res=A*x-b; Eng. Mohamed EltaherEng.Ahmed Ibrahim
Example:- Use MATLAB to solve the simultaneous equations below. Solution • »A=[??????]; • »b=[???? ]; and solve with a single line of code! »x=A\b; • Or » x = inv(A)*b Eng. Mohamed EltaherEng.Ahmed Ibrahim