<--- Return to Excel Statistics Guide
Solve Linear Equations in Excel using Matrix Inverse and Multiplication
Keith Greiner
Updated August 9, 2020
This essay is about finding the solution to a collection of linear equations, in Excel, using an easy two-step process. Step 1 is to find the inverse of a square matrix of xn coefficients. Step 2 is to multiply results of Step 1 by the vector of Y values. After explaining the two step process, I show that the Excel code statements can be combined into a single step. This essay is one of three on the subject of finding solutions to sets of equations. The other two are at: this link and this link.
First, lets take a brief review of some simple linear equations and their solutions.
Everyone who has taken high school algebra should be able to solve the following formula for m, given that you know the values of x and y.
y = mx
You simply divide both sides of the equation by x, which effectively moves the x from the right to the left, under the y.
y/x = m
Then, to follow convention, you simply turn it around so the unknown is on the left.
m = y/x
Solving for x, we have the following.
x = y/m
That’s easy enough, but what if there is more than 1 value of m? What if there is a collection of m’s as in, m1, m2, m3 … mn?
What if the equation looks like the following?
y = m1x1 + m2x2 + m3x3 + m4x4 ... mnxn
This essay discusses the solution to multi-variable linear problems by discussing what I’ll call the Inverse-Multiplication, or “IM” method. There is a discussion in the context of an Excel spreadsheet. The problem will be solved in two simple steps with an Excel spreadsheet. At the end of the essay, the two steps will be merged into one simple Excel code statement.
Lets say we have the following system of four equations. There are five unknowns, of x1 through x5. If we know the correct values for x, we multiply each x value by its corresponding coefficient of m1, m2, m3, m4, or m5, and the result will be that shown for y. The purpose of this analysis is to find a set of m1…5 values which could be used to find the four y values shown.
Prepare the matrix by removing the "x" text items from the matrix table, with the result shown below. The focus of calculations will be on only the m coefficient values.
Here, the blue area represents a matrix of the m coefficients, and the tan are represents the y values.
To solve for the needed m values, we simply follow the following steps.
Step 1. Find the inverse of the matrix using =MINVERSE(C21:G25). To accomplish Step 1, use the =MINVERSE(…) function and follow these steps. Microsoft describes this as an “Array Function” because it returns an array of cells. It requires a few extra steps to implement. Here are those steps.
For the example data, your intermediate (inverse) matrix should have the following values.
Step 2. Multiply the inverse by the vector of y values. Like the inverse, this is what Microsoft calls an “Array Function”. This time, it returns a single column of five values that will be the m1 ... m5 coefficients for x1 … x5. Here are the steps for this function. I will plan to put the output matrix in the range form D47:D51, which is below row 38 (for this example).
The five cells D47:D51 should now contain the results of your second step. These are the m1...m5 coefficients.
You can verify the coefficients by multiplying each coefficient by its corresponding x value. I did this by arranging the coefficients in a row beginning in cell C59, and building a matrix of multiplied values. Once the table is constructed, sum across the rows and you should have the original y values in column H. The Excel code is shown in range B68:H73. Here you can see that row 68 points to the coefficients obtained in Excel Image 4. The other calculations, where the Excel code is shown in rows 69 through 73, show the multiplication of the results of the m1...m5 coefficients in row 59, by the appropriate cells in the original matrix in the range C21:G25. Column H shows row sums that are equal to the original Y values provided in range H8:12 and repeated in H21:H25. With everything matching, we have shown that the use of the =MINVERSE(...) function followed by the =MMULT(...) provides the correct m1...m5 coefficients that correspond to x1...x5 in the original set of equations.
Summary of Step 1 and Step 2.
And so in summary, this process used two steps
The two functions could be combined to accomplish the task in one step by using the following.
=MMULT(MINVERSE(C21:G25), H21:H25)
Again, be sure to specify where the output range will be for the results of the =MMULT(...) function. In this example, it is D47:D51.
You will, of course, want to substitute the actual cell ranges of your data. If you are using a 5 x 5 matrix, try putting your data into the cell ranges shown and discussed here, and you should be successful. If not, you will have an opportunity to practice your debugging skills.
Remember, that the functions are what Microsoft calls "Array Functions" and need to be entered in a multi-step process. You can learn more about array functions at this link.
Postscript: Why do the examples have yellow borders instead of Microsoft Excel's grey or blue?
No doubt you have noticed that the borders of the example spreadsheets are yellow instead of the standard Microsoft grey. That is because these borders allow me to copy and paste large segments of an Excel sheet without having to do a screen print small portions for each segment and assemble multiple images into one large image. If you look at other essays on my site, you will see that I have presented extensive examples that include long selections from Excel examples. Some are in .html format and some are in .pdf format. In these examples, I want to show the entire set of Excel code and calculations. Sometimes it takes a lot of data to make the example as complete as possible. All those examples need to show the row and column of the spreadsheet, so readers can see exactly how the Excel code works. Microsoft doesn't allow users to copy and paste while including the row and column margins, so the next best thing is to use the A column and the number one row to show columns and rows. That is also why the top left corner shows as "A,1". Compare that to your Excel sheet to see the difference. I colored the borders yellow to make them stand out.