views:

515

answers:

6

I have a system of 22 linear equations (exactly 22 equations and 22 unknowns) which are dynamically generated in an Excel spreadsheet. Is there a way to have Excel solve the system and produce a value for each of the unknown variables?

Another SO question has a lot of good information about the algorithms used to solve such equations. Cramer's rule in particular looks promising, but I'm not sure how to implement it in Excel.

Any help is appreciated!

+2  A: 

See here.

BlueRaja - Danny Pflughoeft
+4  A: 

This page gives the following simple method:

  1. Enter the coefficient matrix in an nxn range.

  2. Enter in the vector of constants in a n-tall column. Select a blank n-tall column. It is important that you not have only one cell selected.

  3. Type, =MMULT(MINVERSE(coefficients_matrix_range), constants_vector_range) in the formula text box and press [CTRL][SHIFT][ENTER]. It is important that you not simply press [ENTER].

This should work adequately, since you only have a small system (22x22). While, ideally, you wouldn't invert the matrix directly, it shouldn't matter in this case.

You definitely don't want Cramer's rule. Due to its fairly poor numerical properties, it's more of theoretical than practical interest.

Peter
Thank you, Peter. That's nice and simple!
e.James
+1  A: 

You should not ever consider Cramer's rule to solve a linear system! It is so inefficient it is impossible to carry out for even small systems.

Excel is not the right tool to solve a linear system. If I were you I'd write a simple python script that reads the excel file, solve the equation (with numpy) and save the result in a file that excel can read. (even better, avoid Excel altogether if you can).

Olivier
Thank you for the warning about Cramer's rule. I'm not too worried about the speed here, since this only needs to be updated once in a while. Avoiding Excel altogether would not be an option, since all of the source data is already in Excel. Besides, I find Excel to be one of the most powerful and useful programs that is readily available on just about every computer in my line of work!
e.James
If speed is a major concern, Python is not exactly the best choice...
BlueRaja - Danny Pflughoeft
That's why I wrote "with numpy". numpy is a python interface to the standard linear algebra libray written in C or fortran (BLAS/LAPACK), so it is very fast.
Olivier
+1  A: 

The Excel LINEST function should do it - and will return better results than MMULT(MINVERSE(...)...) in many cases.

You might also find the LOGEST, GROWTH and TREND functions useful.

Joe Erickson
I'm pretty sure LINEST and the others all deal with *linear* data only, i.e. with only one unknown (x). I'm working with a system of equations with 22 unknowns, so I don't think LINEST will do the trick.
e.James
+1  A: 

If you have any problems with in the inverse matrix calc, e.g. over / under flow / truncation etc, you could always use the Solver - else get a copy of numerical recipes in C and translate into VBA.

DangerMouse
+2  A: 

What about Microsoft's Solver Foundation? I seem to remember there's some example for a smaller set of simultaneous equations in the documentation that comes with the express version.

Bohdan Szymanik
+1 That's pretty cool! I'd never even heard of it until today. Peter's solution worked for me this time, but I can see MSF being handy if I'm ever dealing with larger or more dynamic data sets. Thank you for the info.
e.James