Solve Matrices in Excel

Embed Size (px)

Citation preview

  • 7/27/2019 Solve Matrices in Excel

    1/4

    Working With Matrices In Excel

    As an illustration, we will work with the following system of equations

    2x 2y + z = 33x + y z = 7x 3y + 2z = 0

    which can be written as the matrix equation

    2 2 13 1 1

    1 3 2

    xy

    z

    =

    37

    0

    Recall that in order to solve the matrix equation

    Ax = b

    (where A is a square matrix), we need only find the inverse (A1) ofA (if it exists) and the solution will begiven by

    x = A1b

    We will do all of the above in Excel.

    When working with matrices in Excel, before you enter any kind of formula, press the F2 key (to tell Excelyou are working with arrays), followed by the = sign.

    The two built-in Excel functions we will use are as follows:

    MMULT(matrixA,matrixB) for finding the matrix product AB (note the order)MINVERSE(matrixA) for finding A1

    Consider our system above. Let

    A =

    2 2 13 1 1

    1 3 2

    b =

    37

    0

    We need to find A1 and the solution we are looking for is given by the matrix product A1b.

    1

  • 7/27/2019 Solve Matrices in Excel

    2/4

    Finding A1

    Enter the numbers in A in any 3 3 cell block in Excel, putting each number in its own cell, in the rows andcolumns as they appear in A. (If your matrix is an n n matrix, use an n n cell block.

    Since A1 is a 3 3 matrix, select a 3 3 cell block anywhere else in your spreadsheet.

    Perform the following steps (in the order given):

    Press the F2 key.

    Press the = sign.

    Type in MINVERSE(.

    Select the cells containing A1.

    Close the bracket ). You should now have something that looks like the screen shot shown on the leftbelow.

    Hold down the keys Ctrl and Shift at the same time and press the Enter key while holding

    down the other two.

    A1 should now appear in the block of cells you had selected (see screen shot on the right below).

    If something weird appears (like only one number appearing), you may have pressed the Enter key withoutholding down the Ctrl and Shift keys. If this is the case re-select the cells you had chosen for your A1 andpress the Delete key, then repeat the above steps.

    If A1 does not exist, e.g. the matrix B =

    2 4

    3 6

    has no inverse since det(B) = 0, you make get

    something that looks like this:

    2

  • 7/27/2019 Solve Matrices in Excel

    3/4

    Finding a matrix product AB

    Suppose you want to find the product AB. You will first need to determine the order of the resulting matrix.

    Suppose A is an nm matrix and B is an mp matrix. The order ofAB is then np.

    To find AB in Excel, simply enter the numbers in the matrices anywhere on your spreadsheet. Perform thefollowing steps:

    Select an empty n p block of cells in your spreadsheet(if you know your matrix product is an n pmatrix).

    Press the F2 key.

    Press the = sign.

    Type in MMULT(.

    Select the cells containing A and enter a comma (,).

    Select the cells containing B.

    Close the bracket ).

    Hold down the keys Ctrl and Shift at the same time and press the Enter key while holdingdown the other two.

    As an illustration, let A be as given above and C =

    1 12 1

    2 3

    . Since A is a 3 3 matrix and C is a 3 2

    matrix, the product AC is a 3 2 matrix. Performing the above steps will give the following screen shots:

    We are now ready to find the solution to our system of equations.

    3

  • 7/27/2019 Solve Matrices in Excel

    4/4

    Solving the system of Equations

    Enter the right-hand-side column vector b =

    37

    0

    into a 31 block of cells in your spreadsheet. Also enter

    the matrix A into a 3 3 block of cells in your spreadsheet.

    Since we will get the solution from the matrix product A1b, we first determine the order of the resultant

    matrix. In this case, we are multiplying a 3 3 matrix by a 3 1 vector, we will get a 3 1 vector.

    Select a 3 1 block of empty cells. Type the following: F2 = MMULT(MINVERSE( and select the cellscontaining matrix A. Close bracket ). Type a comma and select the cells containing the vector b and closebracket ). You should have something that looks like this:

    Hold down the Ctrl and Shift keys and press the Enter key while still holding the other two keys. Thevalues ofx, y and z should now appear (see screen shot below).

    Note that we did not have to calculate A1 separately. We simply asked Excel to calculate it for us and thenmultiply the inverse by b. Most Excel functions can be nested in this way.

    4