Download LAB ASSIGNMENT -6 MATRIX OPERATIONS IN EXCEL 8.1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Bra–ket notation wikipedia , lookup

Mathematics of radio engineering wikipedia , lookup

Determinant wikipedia , lookup

Non-negative matrix factorization wikipedia , lookup

Matrix calculus wikipedia , lookup

Transcript
LAB ASSIGNMENT -6
MATRIX OPERATIONS IN EXCEL
8.1 ARRAYS IN EXCEL:
Excel uses arrays to store a collection of variables. These arrays can be used to perform
matrix operations. To declare a bunch of numbers as an array, you select the numbers that make
up the matrix and type the name of the array in the “Name Box” as shown in the Figure 8.1.
Figure 8.1 Naming arrays
Alternately, we can name an array by selecting the numbers and clicking on the
“Formulas” ribbon and choosing “Define name”. Using named arrays ensures that we can use the
names instead of the cell range in array formulas. It is not necessary to name the arrays for
matrix operations in Excel but this helps in keeping the spreadsheet organized and easy to
understand.
8.2 BASIC MATRIX OPERATIONS:
8.2.1 ADDITION: Before performing the addition, you should ensure that the matrices to be
added are of the same size. Name the two matrices as arrays A and B using the method described
above.
Figure 8.2 Matrix addition using array math in excel
In the above example, A and B are both square matrices of size 3x3. The resulting matrix (A+B)
would also be of the order 3x3. When using array math in Excel, we should always indicate the
1
size of the resulting array. So, we select 3 rows and 3 columns and then we enter the formula
(=A+B) in the top-left cell of the selected cell range.
Figure 8.3 Highlighting the size of the resulting array
Notice that when you use the named arrays in the formula, Excel highlights the arrays. This helps
you check that the correct arrays are being used in the formula.
To obtain the result, DO NOT PRESS [ENTER]. To tell Excel that we are using array math, we
should use a special character sequence. Press and Hold [Ctrl]+[Shift] and then press then
[Enter] key. The result will be displayed as below once you press [Ctrl]+[Shift]+[Enter]
Figure 8.4 Displaying the result
Alternately, this can also be done without using the named arrays by simply using the cell ranges
as shown in Figure 8.5.
2
Figure 8.5 Using cell ranges instead of named arrays
8.2.2 SUBTRACTION: Repeat the above exercise by replacing the addition (+) with the
subtraction operation (-)
8.2.3 MULTIPLYING A MATRIX BY A SCALAR:
When we multiply a matrix by a scalar, we are simply multiplying each element of the matrix
with the scalar. This can be accomplished easily by using array math in Excel.
1. Select the elements in the matrix and name it “A”
2. Highlight empty cells matching the size of the matrix A.
3. Type the formula (=10*A) to multiply the matrix A by a scalar 10.
4. Press [Ctrl]+[Shift]+[Enter] to display the result.
Figure 8.6 a) and 8.6 b) Multiplying a matrix by a scalar
8.2.4 MATRIX MULTIPLICATION: Multiplication between two matrices is only possible if
the number of columns of the first matrix is equal to the number of rows of the second matrix.
1. Define the arrays A and B in the Excel sheet using name boxes.
2. The resulting matrix after the multiplication of A and B will have rows equal to the first
matrix and columns equal to the second matrix. Therefore highlight the required number
of rows and columns. (Note: If you underestimate the size of the resulting matrix, the
matrix will be truncated. However if you overestimate the size of the matrix, the
additional rows and columns will be filled with #N/A)
3
3. Enter the formula (=MMULT(A,B))
4. Press [Ctrl]+[Shift]+[Enter] to display the result.
Figure 8.7 Matrix multiplication
Figure 8.8 Matrix multiplication result.
Figure 8.9 Error due to over-estimating the size of the resulting matrix.
8.2.5 MATRIX TRANSPOSE: The transpose of a matrix is obtained by switching the rows and
columns. This can be done in two ways:
a) PASTE SPECIAL METHOD:
4
1. Select the cells that contain the values of the matrix and “Copy” them into the clipboard
by pressing [Ctrl]+c or Right clicking on the selected cells and selecting “Copy” or
clicking the “Copy” button on the “Home -> Clipboard”.
2. Click on a cell on the spreadsheet where you want the resulting matrix to be displayed
3. Right click on the cell and choose “Paste special” or click on the “Paste” button on the
“Home -> Clipboard” and choose “Paste special”
4. Check the “Values” and “Transpose” options in the dialog box and click “OK”
5. Transpose of the matrix will be displayed.
Figure 8.10 Dialog box invoked by the paste special option
b) ARRAY OPERATIONS:
The above method is done using a simple copy paste method and does not involve any
array operations. This can also be done using the TRANSPOSE() function in Excel as follows:
1. Name the matrix as an array A using the name box.
2. Estimate the order of the resulting matrix and select a space on the spreadsheet
matching the order for the matrix to be displayed.
3. Enter the formula ‘=TRANSPOSE(A)”
4. Press [Ctrl]+[Shift]+[Enter] to display the results.
8.2.6 MATRIX DETERMINANT:
To calculate the determinant of a matrix, the matrix must be a square matrix, and nonsingular. Excel uses the function MDETERM() to calculate the determinant of matrix.
1. Name the matrix as an array A
2. Select a cell to display the result. Since determinant is a single number, only one cell
has to be selected.
3. Press [Enter] to display the result.
As an exercise, check the following:
1. Determinant of a matrix with a row/column containing all zeros is zero.
2. Determinant of a matrix with any two identical rows/columns is zero.
5
3. Determinant of a matrix whose row/column can be expressed as a linear combination
of other rows is zero.
8.2.7 MATRIX INVERSE:
The inverse of a matrix exists if, and only if the matrix is non-singular, i.e. the
determinant of the matrix is not zero. Before calculating the inverse of a matrix, check that the
determinant of the matrix is not zero. If it is zero, it can be concluded that the inverse does not
exist.
1. Name the matrix as an array.
2. Check that the determinant is not zero using the Excel function MDETERM(A)
3. Select the cells to display the resulting matrix. The resulting matrix will have the
same size as the original matrix.
4. Type the formula “=MINVERSE(A)”
5. Press [Ctrl]+[Shift]+[Enter] to display the result.
As an exercise, check that the multiplication of the original matrix and its inverse yields the
identity matrix. (Note: In array math, the volume of calculations can result in round-off errors.
So any numbers which are to the order of 10-8 can be assumed as zero.)
8.2.8 IMPORTANT NOTES:
1. Just as variables in a program, only one variable name can exist per array in a
spreadsheet. Therefore, you cannot have several matrices of the name A in the same
spreadsheet.
2. As explained before, it is not required to name your arrays to perform array operations in
Excel. These functions work just as good with cell ranges.
3. In this course, all the arrays should be named.
8.3 APPLICATIONS:
8.3.1 SOLVING LINEAR SYSTEM OF EQUATIONS:
As discussed in the lecture, matrices can be used to solve a set of linear equations. We
can use the array operations learnt in the above section to solve these equations.
1. Write the sets of linear equations in the matrix form.
2. Name the coefficient matrix, right-hand-side matrix.
3. Find the determinant of the co-efficient matrix using MDETERM() function to ensure
that the inverse exists.
4. If the determinant is not zero, find the inverse of the co-efficient matrix using
MINVERSE() function.
5. Multiply the inverse of the co-efficient matrix with the right-hand-side matrix using
the MDETERM().
6. The resulting matrix from the above multiplication is the solution matrix.
7. Check the solution.
Consider the following three equations in three unknowns:
4 x1 + 8 x2 + 2 x3 =
2
9 x1 + 5 x2 + x3 =
6
7 x1 + 3 x2 − 10 x3 =
7
Find the values of x1, x2, x3 using the matrix operations in excel.
6
Figure 8.11 Setting up the spreadsheet
Figure 8.12 Calculating the inverse of the coefficient matrix
Figure 8.13 Calculating the solution matrix
7
Figure 8.14 Performing a final check.
8