I need to programmatically solve a system of linear equations in C, Objective C, or (if needed) C++.

Here's an example of the equations:

```
-44.3940 = a * 50.0 + b * 37.0 + tx
-45.3049 = a * 43.0 + b * 39.0 + tx
-44.9594 = a * 52.0 + b * 41.0 + tx
```

From this, I'd like to get the best approximation for `a`

, `b`

, and `tx`

.

Cramer's Rule and Gaussian Elimination are two good, general-purpose algorithms (also see Simultaneous Linear Equations). If you're looking for code, check out GiNaC, Maxima, and SymbolicC++ (depending on your licensing requirements, of course).

EDIT: I know you're working in C land, but I also have to put in a good word for SymPy (a computer algebra system in Python). You can learn a lot from its algorithms (if you can read a bit of python). Also, it's under the new BSD license, while most of the free math packages are GPL.

You can solve this with a program exactly the same way you solve it by hand (with multiplication and subtraction, then feeding results back into the equations). This is pretty standard secondary-school-level mathematics.

So you end up with:

If you plug these values back into A, B and C, you'll find they're correct.

The trick is to use a simple 4x3 matrix which reduces in turn to a 3x2 matrix, then a 2x1 which is "a = n", n being an actual number. Once you have that, you feed it into the next matrix up to get another value, then those two values into the next matrix up until you've solved all variables.

Provided you have N distinct equations, you can always solve for N variables. I say distinct because these two are not:

They are the

sameequation multiplied by two so you cannot get a solution from them - multiplying the first by two then subtracting leaves you with the true but useless statement:By way of example, here's some C code that works out the simultaneous equations that you're placed in your question. First some necessary types, variables, a support function for printing out an equation, and the start of

`main`

:Next, the reduction of the three equations with three unknowns to two equations with two unknowns:

Next, the reduction of the two equations with two unknowns to one equation with one unknown:

Now that we have a formula of the type

`number1 = unknown * number2`

, we can simply work out the unknown value with`unknown <- number1 / number2`

. Then, once you've figured that value out, substitute it into one of the equations with two unknowns and work out the second value. Then substitute both those (now-known) unknowns into one of the original equations and you now have the values for all three unknowns:The output of that code matches the earlier calculations in this answer:

For a 3x3 system of linear equations I guess it would be okay to roll out your own algorithms.

However, you might have to worry about accuracy, division by zero or really small numbers and what to do about infinitely many solutions. My suggestion is to go with a standard numerical linear algebra package such as LAPACK.

Take a look at the Microsoft Solver Foundation.

With it you could write code like this:

Here is the output:===Solver Foundation Service Report===

Datetime: 04/20/2009 23:29:55

Model Name: Default

Capabilities requested: LP

Solve Time (ms): 1027

Total Time (ms): 1414

Solve Completion Status: Optimal

Solver Selected: Microsoft.SolverFoundation.Solvers.SimplexSolver

Directives:

Microsoft.SolverFoundation.Services.Directive

Algorithm: Primal

Arithmetic: Hybrid

Pricing (exact): Default

Pricing (double): SteepestEdge

Basis: Slack

Pivot Count: 3

===Solution Details===

Goals:

Decisions:

a: 0.0785250000000004

b: -0.180612500000001

c: -41.6375875

Are you looking for a software package that'll do the work or actually doing the matrix operations and such and do each step?

The the first, a coworker of mine just used Ocaml GLPK. It is just a wrapper for the GLPK, but it removes a lot of the steps of setting things up. It looks like you're going to have to stick with the GLPK, in C, though. For the latter, thanks to delicious for saving an old article I used to learn LP awhile back, PDF. If you need specific help setting up further, let us know and I'm sure, me or someone will wander back in and help, but, I think it's fairly straight forward from here. Good Luck!

Template Numerical Toolkit from NIST has tools for doing that.

One of the more reliable ways is to use a QR Decomposition.

Here's an example of a wrapper so that I can call "GetInverse(A, InvA)" in my code and it will put the inverse into InvA.

Array2D is defined in the library.

From the wording of your question, it seems like you have more equations than unknowns and you want to minimize the inconsistencies. This is typically done with linear regression, which minimizes the sum of the squares of the inconsistencies. Depending on the size of the data, you can do this in a spreadsheet or in a statistical package. R is a high-quality, free package that does linear regression, among a lot of other things. There is a lot to linear regression (and a lot of gotcha's), but as it's straightforward to do for simple cases. Here's an R example using your data. Note that the "tx" is the intercept to your model.

In terms of run-time efficiency, others have answered better than I. If you always will have the same number of equations as variables, I like Cramer's rule as it's easy to implement. Just write a function to calculate determinant of a matrix (or use one that's already written, I'm sure you can find one out there), and divide the determinants of two matrices.

Personally, I'm partial to the algorithms of Numerical Recipes. (I'm fond of the C++ edition.)

This book will teach you why the algorithms work, plus show you some pretty-well debugged implementations of those algorithms.

Of course, you could just blindly use CLAPACK (I've used it with great success), but I would first hand-type a Gaussian Elimination algorithm to at least have a faint idea of the kind of work that has gone into making these algorithms stable.

Later, if you're doing more interesting linear algebra, looking around the source code of Octave will answer a lot of questions.