100% found this document useful (1 vote)
180 views16 pages

Excel Demas

excel

Uploaded by

DavidAlejo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
180 views16 pages

Excel Demas

excel

Uploaded by

DavidAlejo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 16

R. D.

Busch, 2004

UNM ChNE-231, Page XLG-1

Module XLG Matrices, Vectors, and Arrays in EXCEL


Learning Objectives
After completing this module, the student should be able to:
1.

Create arrays, vectors, and matrices in excel.

2.

Calculate the inverse and determinant of a square matrix.

3.

Calculate the product of matrices.

4.

Use matrix functions to solve simultaneous equations.

Assignments
1.

Read the information in this module.

2.

Complete the exercises included in this module.

3.

Work the problem at the end of this module.

R. D. Busch, 2004

UNM ChNE-231, Page XLG-2

Module XLG Matrices, Vectors, and Arrays in EXCEL

Matrices, Vectors, and Arrays


A matrix is a two-dimensional set of values whose size is identified by the number of
rows, R, by the number of columns, C. Thus, if you have a 4x2 matrix, there will be 4 rows each
having two columns. A square matrix has the same number of rows and columns, so an example
of a square matrix might be a 3x3 or a 5x5. A vector is a one-dimensional set of values identified
in the same manner as a matrix. You can have a 1x5 vector (1 row, 5 columns) or a 3x1 vector (3
rows, 1 column). Matrices and vectors can be added, subtracted, and multiplied. Excel allows
you to do this by its array operations. In EXCEL, matrices and vectors are represented by arrays;
an array being a block of cells that is referenced as a single unit.
Matrix Algebra
Linear algebra (the algebra of matrices) is an especially useful form of expression for
many engineering problems. Some of its uses include:
1. solving linear simultaneous equations
2. solving differential equations
3. calculating statistics, especially Analysis of Variance and Regression
4. developing models for chemical reactions and kinetics.
Definition of matrix A:
a11 a12
a
a22
21
A = (aij) =
M M

an1 an 2

L
L
L
L

a14
a2 n
M

ann

where aij is an element of A , i is the row number, and j is the column number. A
matrix may have any number of rows (R) or columns (C) and is said to be of
order r x c (read as "order R by C"). A vector is a single row or a single column:
v1
r v2
V
M

vn
r
V v1 v2 L vn
These are also known as row vectors, or column vectors, respectively, and are usually
denoted by capital letters and a short overhead arrow. The column vector is an n x 1
matrix while the row vector is a 1 x n matrix. Note that matrices can be denoted by either
parentheses or square brackets.

R. D. Busch, 2004

UNM ChNE-231, Page XLG-3

Module XLG Matrices, Vectors, and Arrays in EXCEL


Single vertical lines are not used to enclose matrices as they denote the determinant of a
matrix, which we will discuss later in the module.
Identity Matrix
The identity matrix provides a means for representing the number "one" in matrix terms.
The identity matrix is also called the unit matrix. It consists simply of a square matrix in which
all the elements of the main diagonal are "1" whereas all of the off-diagonal elements are zero. A
subscript may be used to indicate the order of the identity matrix:

1 0 0

I 0 1 0
0 0 1

or

0
I M

0
0

or, to clarify the order, I3 or In.

0
1
M
0
0

L
L
O
L
L

0
0
M
1
0

0
1

The identity matrix is a special diagonal matrix where the diagonal elements are one, but other
values are possible in diagonal matrices too. A matrix of the form kI is called a scalar matrix.
Multiplication by a scalar matrix yields the same result as multiplication by a scalar: kIA = kA.
The transpose of a matrix A, denoted by A or AT, is the matrix obtained by interchanging the
rows and columns of A.
3 1
3 1 2

T
A
then A 1 4
1
4
0

2 0
Note that transposing a matrix can change its order. In the example above, A is a 2x3 matrix
while AT is a 3x2 matrix. The definition of a transpose determines that the original matrix can be
recovered by transposing the transpose, i.e., the transpose operation is reflexive: (AT)T = A.
Matrix Multiplication
The multiplication properties of matrices underlie their greatest value for engineering
applications. A matrix may be multiplied by a scalar, or a vector, or by another matrix. For scalar
multiplication, we have an element-by-element product:
a11 a12
a
a22
kA k 21
M M

am1 am 2

L
L
L
L

a14
a2 n
M

amn

ka11 ka12
ka21 ka22

M
M

kam1 kam 2

L
L
L
L

ka 14
ka 2 n
M

ka mn

R. D. Busch, 2004

UNM ChNE-231, Page XLG-4

Module XLG Matrices, Vectors, and Arrays in EXCEL


For the product, C, of two matrices, A, and B, where matrix A is of order, m x n, and matrix B is
of order n x k:

Cmxk Amxn * Bnxk

The basic rule is that "adjacent subscripts" must be equal for the matrices to be compatible for
multiplication. It is a simple matter, then, to determine the order of the matrix resulting from a
series of multiplications:

Amxn * Bnxp * C pxr * Erxs * Fsxt * Gtxw H mxw

For a matrix A, of order n x m , a product with matrix B will exist under three circumstances:
1. AB if B has m rows,
2. BA if B has n columns,
3. both AB and BA exist if B is m x n
Note also that the matrix AB is said to be matrix A post-multiplied by B, or as A multiplied on
the right by B . Similarly, BA is A pre-multiplied by B , or A multiplied on the left by B. Because
matrices can be multiplied by other matrices, scalars, or vectors, it is useful to remember the
following four results:
a. A row vector post-multiplied by a column vector results in a scalar;
b. A column vector post-multiplied by a row vector results in a matrix;
c. A matrix post-multiplied by a column vector yields a column vector, and;
d. A row vector post-multiplied by a matrix becomes a row vector.
Now, let us look at an example of a matrix multiplication: First, the general definition of an
element of the resultant matrix C is:

C A * B where the cij aim bmj


m

Using the matrices A and B where:


a
a
b b
A 11 12
B 11 12
a21 a22
b21 b 22
then element c11 is the sum of the products of each element of Row 1 of A multiplied by each
element of Column 1 of B , and so on:

a11b11 a12b21
a21b11 a22b21

a11b12 a12b22
a21b12 a22b22

R. D. Busch, 2004

UNM ChNE-231, Page XLG-5

Module XLG Matrices, Vectors, and Arrays in EXCEL


A numeric example yields the following result:
3 1
1 0 1
A 1 0
B

2 1 1

2 3

3*1 1* 2 1 3*0 1*1 1 3*1 1*1 2

C A * B 1*1 0* 2 1
1*0 0*1 0
1*1 0*1 1
2*1 3* 2 8
2*0 3*1 3
2*1 3*1 5

1 1 2
C 1 0 1

8 3 5

This example is relatively simple so you can follow the process. However, many applications
have much larger matrices requiring significantly more calculations so using a tool like EXCEL
makes the process easier, quicker, and more accurate.
Matrix Operations in Excel
Excel can do matrix operations very efficiently, either directly, or through the use of
matrix functions. Microsoft prefers to use the term "Array" to "Matrix", so most references in
their manuals and help system can be found under the term array. Key to understanding the use
of matrix operations in Excel is the concept of the Matrix (Array) formula. Such a formula uses
matrix operations and returns a result that can be a matrix, a vector, or a scalar, depending on the
computations involved. Whatever the result may be, an area on the spreadsheet of precisely
the correct size must be selected before the formula is typed in (otherwise you will either
lose some of the answer or get added and possibly confusing information).
Named Ranges
To do operations with arrays, Excel requires that the array be identified. The best
way of identifying an array is to name it similar to the way single cells are named. To do
this, highlight the cells to be included in the range and then go to the name cell and type
in the name. For example if we want to create a matrix that is 3 x 2 (e.g., 3 rows by 2
columns) and is located in cells B3:C5. First highlight all the cells in the matrix as shown
in the figure below, and then go to the name cell and type in matA and then press Enter.
The sheet should look like that below. Once you have named an array, you can enter any
values desired in the cells or change values. We are going to use this matrix and another
to follow the matrix multiplication example above. So, we need to enter the values for
matrix A into the cells. After doing this, the sheet should look like the second one on the
next page.

R. D. Busch, 2004

UNM ChNE-231, Page XLG-6

Module XLG Matrices, Vectors, and Arrays in EXCEL

Now we want to repeat the process for matrix B, which is 2 x 3 (e.g., 2 rows by 3
columns) and is located in cells B7:D8. So first highlight the desired cells and then name
them matB. Then fill in the values as shown in the figure below.

R. D. Busch, 2004

UNM ChNE-231, Page XLG-7

Module XLG Matrices, Vectors, and Arrays in EXCEL

Once you have named a range, you may use it in any formula that allows for a
range as an argument.
Matrix Multiplication
Now that we have our two matrices, we can multiply them together and check our answer
from above. To multiply two matrices, use the MMULT function. Remember, to do matrix
multiplication, the two matrices must be compatible such that the number of columns for the
matrix on the left is equal to the number of rows of the matrix on the right. For our example, we
want to multiply A*B, so A having two columns and B having two rows means this operation can
be perfomed. To compute the product of A*B, use the formula:
= MMULT(matA,matB)
However, there are two caveats when doing array operations. First you must select a
results area that is the correct size for the operation being performed. In this case, we are
multiplying A and B, which should create a 3 x 3 matrix C. So before typing the formula, we
need to highlight 3 rows and 3 columns that will contain matrix C. So lets put matrix C in cells
E4:G6, and name it matC. Note that naming the array is unnecessary unless we want to refer to
it in further operations. After doing this, the sheet should look like the following:

R. D. Busch, 2004

UNM ChNE-231, Page XLG-8

Module XLG Matrices, Vectors, and Arrays in EXCEL

Now, we will type the formula in the formula bar as MMULT(matA,matB) , and the
sheet should look like this.

Now, it is time for the second caveat. Instead of simply pressing Enter when you have
completed typing the formula, you need to use CTRL, SHIFT, and ENTER all pressed together.
We will represent this as Ctrl-Shift-Enter. If you dont do this you will only get a part of

R. D. Busch, 2004

UNM ChNE-231, Page XLG-9

Module XLG Matrices, Vectors, and Arrays in EXCEL


the answer. For our example, if you only press Enter, you will get the value 1 in C5 and blanks in
the other 8 cells of the array. When we press Ctrl-Shift-Enter after typing the formula,
the product of A and B should appear in the matrix C array as follows.

So as you can see Excel got the same answer as we did above. Now what happens if we
multiply matrix B by matrix A. This time we will get a 2 x 2 matrix that we will call D. So
highlight cells E10:F11 and name them matD. Then type = MMULT (matB,matA) as the
formula and then press Ctrl-Shift-Enter. This should produce the following:

R. D. Busch, 2004

UNM ChNE-231, Page XLG-10

Module XLG Matrices, Vectors, and Arrays in EXCEL


When you type a formula and then Ctrl-Shift-Enter , Excel will place the formula
within braces so it appears in the formula bar as {=mmult(matB,matA)}. You should not type the
braces as these are only to be added by Excel to indicate an array formula.
Note that matrix multiplication is not commutative, that is, the product of A*B is
different than the product of B*A. In fact, one or both of the products may not exist if the
number of columns of the pre-multiplying matrix does not equal the number of rows of the postmultiplying matrix.
When matrix computations are performed using Ctrl-Shift-Enter , the "result
areas" will be updated immediately whenever any of the numbers in the "input areas" change
(unless automatic recomputation has been turned off). This can be a great help when one wishes
to evaluate the effects of changes in assumptions, initial conditions, etc
To select a named range, choose Edit Go to (or the equivalent key), followed by the
range name. Alternatively, use the drop-down list of names located just above and to the left of
the spreadsheet. When a named range is selected, the name will appear in the window for this
list. (In fact, you can name ranges by selecting them, then typing the name in this box; however,
this sometimes allows conflicts to creep in and should be avoided).
Once you have named a range, you may use it in any formula that allows for a
range as an argument. To modify or delete the formula, select the entire region
beforehand.
Matrix Inversion
Sometimes you will need to compute the inverse of a matrix, say matrix E. This operation
is only valid for square matrices with non-zero determinants. The inverse is by definition the
matrix that when multiplied by matrix E produces the identity matrix. The inverse matrix of E is
denoted as E-1. So E*E-1 = I or E-1*E = I. To produce the inverse of a matrix, use the MINVERSE
function, as
= MINVERSE(matE). For a matrix E, its inverse is as shown in the figure
below.

R. D. Busch, 2004

UNM ChNE-231, Page XLG-11

Module XLG Matrices, Vectors, and Arrays in EXCEL


Determinant of A Matrix
An important warning is that a matrix has an inverse if and only if the determinant of the
matrix is not zero. A determinant is a polynomial of the elements of a square matrix.
Determinants exist only for square matrices and are typically denoted by the name of the matrix
enclosed in vertical fences, e.g. the determinant of matrix A is det A, or, |A |. The value of |A | is
found by a process known as expanding the determinant. The procedure below is evaluation of
the determinant by the process of "expansion by minors." The general case (i.e., for an n- order
determinant) is done by selecting a row or a column and then successively multiplying each
element of that row or column by the determinant of its minor. The minor is the part of the
original matrix that remains after striking out the row and the column to which the multiplier
element belongs. The general definition for this process, where |Mij| is the determinant of the
minor of element aij, and where we are expanding by any row, i, is:

Following this procedure for a 3 X 3 matrix gives:

For matrices larger than 3x3, the expansion by minors process to calculate determinants
is very tedious. However, Excel will calculate the determinant of a matrix quite quickly. For a
square matrix, enter the values and assign a name to the array in excel. Then in a single cell,
enter the array formula, = mdeterm(arrayname) and Ctrl-Shift-Enter. This will
place the calculated determinant in the cell. If the determinant is non-zero, then you can calculate
the inverse of the matrix. If the determinant is zero, then the inverse is undefined, and any
subsequent operations involving the inverse will also be undefined.
Combining Matrix Operations
In Excel, you may combine matrix operations in a single formula. Remember, however,
that everything must conform, that the output range should be the correct size for the final result,
and that you must press CTRL-SHIFT-ENTER to enter the formula in the output range. As in all
complex formulas, it never hurts to include sufficient parentheses to explicitly identify each
mathematical operation.
Linear Simultaneous Equations
In many engineering applications, there will be multiple unknowns and a set of equations
relating the unknowns. For example, a problem that arises in circuits is the analysis of currents in
a Wheatstone bridge containing 5 resistors and a voltage source.

R. D. Busch, 2004

UNM ChNE-231, Page XLG-12

Module XLG Matrices, Vectors, and Arrays in EXCEL


For a balanced system where R2*R3 = R1*R4, the 3 equations with 3 unknowns (i1, i2,
and i3) are:
R2(i1-i2) + R4(i1-i3) = E
R1i2 + R5(i2-i3) + R2(i2-i1) = 0
R3i3 + R4(i3-i1) + R5(i3-i2) = 0
These can be written in matrix form as the product of the resistance matrix times the variable
matrix is equal to a constants matrix, or R*curr = const where:
R

(R2 + R4)
=
-R2
-R4

-R2
-R4
(R1+R2+R5)
-R5
curr=
-R5
(R3+R4+R5)

i1
i2
i3

const=

E
0
0

So using matrix operations, we can solve for the currents as curr = R-1 * const. Or using Excel,
the current vector is 3x1 and will be computed from the formula; =MMULT(MINVERSE(R),
const). Lets say, E = 10 volts, R1 = 2 ohms, R2 = 6 ohms, R3 = 1 ohm, R4 = 3 ohms, and R5
= 3 ohms (balanced condition as 6*1 = 2*3), then we will enter the following into Excel for
matrix R and the const vector.
R

9
-6
-3

-6
11
-3

-3
-3
7

const=

10
0
0

After naming the arrays and entering the array formula, Excel indicates that i1 = 2.22, i2 = 0.98,
and i3 = 1.37. The calculation should look like the sheet below.

R. D. Busch, 2004

UNM ChNE-231, Page XLG-13

Module XLG Matrices, Vectors, and Arrays in EXCEL


This technique will work for any number of simultaneous equations of n equations and n
unknowns.
a11x1 + a12 x2 + + a1nxn = c1
a21x1 + a22 x2 + + a2nxn = c2
+ ++ =
an1x1 + an2 x2 + + annxn = cn
So by defining the following matrices in Excel,
a11 a12
a
a22
21
A = (aij) =
M M

an1 an 2
x1

x2

a14
a2 n
M

ann

L
L
L
L

c1

c2

x
n

const

c
n

Then the unknowns in the x vector are calculated by entering the formula
=mmult(minverse(matA),const). Again, there is the caveat, that the determinant of A
must exist or the system of simultaneous equations will not uniquely define the unknowns. This
can also be stated as the equations are not linearly independent.

R. D. Busch, 2004

UNM ChNE-231, Page XLG-14

Module XLG Matrices, Vectors, and Arrays in EXCEL


Exercises
1.

Calculate the determinants of the following:


1
7

0
2

0
0

1
0

0
2

1
0

1
4

0
2

1
1

1
2

2.

Calculate the inverses of the matrices given in exercise 1.

3.

Calculate the following products: A*B, B*A, A*C, A*B*C, B*A*C, and A*I
(remember I is used to denote the Identity matrix, of the same order as A in this case).
1
A 0
1

4.

0
1
0

1
0
3

2
1

0
1

1
1

Solve the following set of simultaneous equations:


x1 x 2 x3

3 x1 2 x 2 2 x3 6
3 x1

x 2 x3 2

1
C 0
1

R. D. Busch, 2004

UNM ChNE-231, Page XLG-15

Module XLG Matrices, Vectors, and Arrays in EXCEL


Problems
1.

(Taken From Applied Numerical Methods by Carnahan, Luther, and Wilkes) When a pure
sample of gas is bombarded by low energy electrons in a mass spectrometer, the
galvanometer shows peak heights that correspond to individual m/e (mass-to-charge)
ratios for the resulting mixture of ions. For the i th peak produced by a pure sample j, one
can then assign a sensitivity, sij, (peak height per micron Hg sample pressure). These
coefficients are unique for each type of gas. A distribution of peak heights may also be
obtained for an n-component gas mixture that is to be analyzed for partial pressures, p1,
p2, , pn of each of its constituents. The height, h i, of a certain peak is a linear
combination of the products of the individual sensitivities and partial pressures.
n

hi s ij * p j
j 1

The sensitivities in the table below are related to the analysis of a hydrocarbon gas
mixture.
Peak
index

M/e
ratio

1
Hydroge
n

2
3
Methane Ethylen
e

4
Ethane

5
Propylene

6
Propane

7
n-Pentane

1
2
3
4
5
6
7

2
16
26
30
40
44
72

16.87
0.0
0.0
0.0
0.0
0.0
0.0

0.165
27.7
0.0
0.0
0.0
0.0
0.0

0.317
0.062
13.05
11.28
0.0
0.0
0.0

0.234
0.073
4.42
0.0
9.85
0.299
0.0

0.182
0.131
6.001
1.110
1.684
15.98
0.0

0.110
0.120
3.043
0.371
2.108
2.107
4.67

0.2019
0.862
22.35
0.0
0.0
0.0
0.0

A particular gas sample produced the following peak heights: h1 = 17.1, h2 = 65.1, h3 =
186.0, h4 = 82.7, h5 = 84.2, h6 = 63.7, h7 = 119.7. The measured total pressure of the
mixture was 39.9 microns Hg, which can be compared with the sum of the computed partial
pressures. Compute the partial pressures of each constituent.
Find all solutions of each set of equations
2.

5x + y + z = 2
x - 2y z = -2
2x + y + 3z = 6

3.

x - 7y + z = 3
2x + 5y - 9z = 2

R. D. Busch, 2004

UNM ChNE-231, Page XLG-16

Module XLG Matrices, Vectors, and Arrays in EXCEL


8x +
4.

y - 25z = 13

Suppose a parabola has its axis parallel to the x-axis and passes through the points
(1,-1), (2,0) and (2,2). The parabola then has an equation of the type
(y-k)2 = 4p(x-h)

or y2 + Ay + Bx + C = 0

If you substitute the coordinates of the three given points in the latter equation, you
should create a set of three equations for the 3 unknowns: A, B, and C. Solve these for
A, B, and C to determine the equation of the parabola.
5.

For matrix A and B given below, calculate A*B and B*A. For each product, give the
order of the resulting matrix and the value in each cell.
1

1
A
1

1
1
0
1

1
0

1
0

1
1

2
1

You might also like