MATLAB Handbook with Applications to Mathematics, Science, Engineering, and Finance
Total Page:16
File Type:pdf, Size:1020Kb
This article was downloaded by: 10.3.98.104 On: 26 Sep 2021 Access details: subscription number Publisher: CRC Press Informa Ltd Registered in England and Wales Registered Number: 1072954 Registered office: 5 Howick Place, London SW1P 1WG, UK MATLAB Handbook with Applications to Mathematics, Science, Engineering, and Finance José Miguel, David Báez-López, David Alfredo Báez Villegas Matrices and Linear Algebra Publication details https://www.routledgehandbooks.com/doi/10.1201/9781315228457-4 José Miguel, David Báez-López, David Alfredo Báez Villegas Published online on: 15 Jan 2019 How to cite :- José Miguel, David Báez-López, David Alfredo Báez Villegas. 15 Jan 2019, Matrices and Linear Algebra from: MATLAB Handbook with Applications to Mathematics, Science, Engineering, and Finance CRC Press Accessed on: 26 Sep 2021 https://www.routledgehandbooks.com/doi/10.1201/9781315228457-4 PLEASE SCROLL DOWN FOR DOCUMENT Full terms and conditions of use: https://www.routledgehandbooks.com/legal-notices/terms This Document PDF may be used for research, teaching and private study purposes. Any substantial or systematic reproductions, re-distribution, re-selling, loan or sub-licensing, systematic supply or distribution in any form to anyone is expressly forbidden. The publisher does not give any warranty express or implied or make any representation that the contents will be complete or accurate or up to date. The publisher shall not be liable for an loss, actions, claims, proceedings, demand or costs or damages whatsoever or howsoever caused arising directly or indirectly in connection with or arising out of the use of this material. Chapter 4 Matrices and Linear Algebra 4.1 Introduction ...................................................... 91 4.2 Matrices .......................................................... 92 4.3 Basic Operations with Matrices ................................. 96 4.3.1 The Dot Operator ....................................... 98 4.4 The Characteristic Polynomial ................................... 98 4.5 Vectors ........................................................... 99 4.5.1 Norm of a Vector ........................................ 103 4.5.2 Vector Generation ....................................... 104 4.6 Dot and Cross Products ......................................... 105 4.6.1 Dot Product ............................................. 105 4.6.2 Cross Product ........................................... 106 4.7 Matrix and Vector Functions .................................... 106 4.8 Systems of Simultaneous Linear Equations ...................... 107 4.8.1 LU Factorization ......................................... 109 4.9 Eigenvalues and Eigenvectors .................................... 110 4.10 Cell Arrays ....................................................... 112 4.11 Structures ........................................................ 113 4.12 Concluding Remarks ............................................. 114 4.1 Introduction As mentioned in Chapter 1, MATLAB was originally designed to carry out operations with matrices, and received the name of Matrix Laboratory that subsequently was reduced to MATLAB. Thus, it deserves special attention to learn the way in which MATLAB works with them. Matrices are practically found in all the areas of knowledge. In this manner, it is possible to find applications of matrices not only in the mathematics areas, but also in a great deal of applications in engineering, in physics, in finance, in accounting, in the arts, music, in anthropology, chemistry and biology, to mention a few. Another important application of matrices is given in the solution of sys- tems of simultaneous equations because the equations’ coefficients are ar- ranged in a matrix. Simultaneous equations systems appear in many problems in engineering such as in optimization of systems, automatic control, food en- 91 Downloaded By: 10.3.98.104 At: 20:04 26 Sep 2021; For: 9781315228457, chapter4, 10.1201/9781315228457-4 R 92 MATLAB HANDBOOK with Applications gineering, chemical engineering, etc. Chapters 9 to 14 present several matrix applications in the solution of problems. A matrix is defined as an array of numbers (or entities). From this defini- tion of a matrix, we also refer to a matrix as an array. Both terms are used indistinctly. MATLAB has an array editor that allows users to introduce the elements of a matrix or to modify the elements of a matrix given previously and already stored as a MATLAB variable. This array editor will be described later in the chapter. Additionally to the basic operations with matrices and vectors, MATLAB has the potential to handle arrays by means of indexing. In this manner car- rying out calculations using arrays becomes very efficient and fast. Finally, the chapter covers cell and structure arrays. Cell arrays are also matrices, but the fundamental difference is that they can handle combinations of different variable types such as names of people or things and numbers. These arrays allow us to handle data in a more efficient and more practical way. A structure is similar to a cell but each element is identified by a name. The chapter begins with a description of matrices and the basic opera- tions that can be performed with them. It continues with vectors and how to compute dot and vector products. A section is dedicated to matrix and vec- tor functions. A section is presented to solve simultaneous equations systems, and for LU factorization of a matrix. The chapter ends with cells arrays and structures. 4.2 Matrices MATLAB handles in matrix form all the variables defined in a MATLAB session, whether they are user-defined or they are in the predefined functions inside MATLAB. It is then convenient to consider the concept of a matrix that any book of linear algebra defines as: A Matrix is an array of numbers or objects. In this way, a matrix can be an array of numbers, letters, objects, or any thing. The only condition that a matrix should satisfy to be a matrix is for it to be an ordered array. For example, a table of numbers is a matrix. The pixels of an image also form a matrix. These two matrices are ordered in a plane. If an image changes with time, as is the case of television images or motion pictures, then we are adding another dimension to the definition of such matrices and thus we are talking about multidimensional matrices. The objects that form the matrix are called elements of the matrix. An example of a matrix is Downloaded By: 10.3.98.104 At: 20:04 26 Sep 2021; For: 9781315228457, chapter4, 10.1201/9781315228457-4 Matrices and Linear Algebra 93 1 3 0 8 −6 4 A = 2− 7 5 4 9 5 − which has 4 rows and 3 columns. We say that matrix A is of dimension 4 3. If a matrix has n rows and m columns, it is a n m matrix. The quantity n× m is called the dimension of the matrix. In MATLAB,× the matrix A is defined× by A = [ 1 -3 0 ; 8 -6 4 ; 2 7 5 ; -4 9 5 ] That is, we have separated the elements of the same row by a space (they can be also separated with a comma) and the rows are separated with a semicolon. Matrix A is shown in Figure 4.1. Each element from the matrix A has a position i, j which corresponds to its position in the ith row and in the jth column. The numbers i and j are called the indices of the (i, j) element. For example, in matrix A the element 8 has the position 2, 1 while the element 7 has the position 3, 2. The command A(i, j) defines the element of row i and column j. If the number of rows is equal to the number of columns, that is, if n = m we say that matrix A is a square matrix of dimension n. Other commands are given in Table 4.1. TABLE 4.1: Matrix commands Matrix command Description A (a:b, c:d) Submatrix consisting of rows a to b and columns c to d. :)A(k, Vector formed by the elements in the k-th row. :,A( j) Vector formed by the elements in the j-th column. MATLAB has an array editor. To illustrate its use we consider matrix A given above. We write again matrix A in the Command Window. This is shown in Figure 4.2 where we have also displayed the Workspace window. In this window we see matrix A and in the Workspace we see the icon for the variable A specified as a 4 3 array. If we double click on this icon the Array Editor will open as a new× window attached to MATLAB as shown in Figure 4.2. There we can clearly see the element values which now can be either modified or deleted. We can even add new elements to matrix A. In this example we add a fifth row to A. The elements that are going to be added are 5 10 -7 Downloaded By: 10.3.98.104 At: 20:04 26 Sep 2021; For: 9781315228457, chapter4, 10.1201/9781315228457-4 R 94 MATLAB HANDBOOK with Applications FIGURE 4.1: MATLAB main window. FIGURE 4.2: MATLAB array editor window. Downloaded By: 10.3.98.104 At: 20:04 26 Sep 2021; For: 9781315228457, chapter4, 10.1201/9781315228457-4 Matrices and Linear Algebra 95 FIGURE 4.3: MATLAB and array editor with new elements. TABLE 4.2: Special matrices Matrix name Description ones(m, n) Matrix of m n dimension where each element has unity value.× ones(n) Square matrix of order n where each element has unity value. zeros(m, n) Matrix with every element having zero value. zeros(n) Square matrix of order n where each element has zero value. eye(n) Square identity matrix of order n eye(m, n) Matrix of m n dimension with 1’s in the main diagonal. The× remaining elements are zeros. We see in Figure 4.3 the elements added. If in the Command Window we write A, the matrix will be displayed as shown in Figure 4.3. Another way to open the array editor is by writing >> open name_of_the_matrix The array editor is then open showing the elements of the variable selected.