6

Possible Duplicate:
Solving a linear equation

I need to programmatically solve a system of linear equations in C# AND VB

Here's an example of the equations:

 12.40 = a * 56.0 + b * 27.0 + tx
-53.39 = a * 12.0 + b * 59.0 + tx
 14.94 = a * 53.0 + b * 41.0 + tx

I'd like to get the best approximation for a, b, and tx.

Should i use some sort of matrix class or something?

Community
  • 1
  • 1
RaindeerJohn
  • 63
  • 1
  • 1
  • 4

4 Answers4

4

Gauss-Jordan elimination is the most straightforward and easiest to understand method for solving a system of simultaneous linear equations like this. LU decomposition is a little more numerically stable, but your matrix doesn't look poorly conditioned so I don't think you need the extra complexity.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Gaussian Elimination was what I meant to say, but at 3.55am, for some reason my head said Simplex! Rectified. http://en.wikipedia.org/wiki/Gaussian_elimination – Orbling Dec 08 '10 at 03:55
  • ah yes, simplex has a step involving gaussian elimination, the other steps are used to determine which combinations of equations give a solution in the feasible region, and to move to adjacent vertices in the direction of improved goal function. Of course, I have the advantage that it's only 10PM here and my brain is not yet such a fuzz. – Ben Voigt Dec 08 '10 at 03:58
2

If you store the coefficients in a matrix, you can solve it by computing the LU decomposition of the matrix. I'm not terribly familiar with the exact algorithm, but wikipedia's pages on this should be a good starting point:

http://en.wikipedia.org/wiki/System_of_linear_equations#Solving_a_linear_system
http://en.wikipedia.org/wiki/LU_decomposition

DGH
  • 11,189
  • 2
  • 23
  • 24
2

Use Cramer's Rule It is easy to solve linear equations by this rule.

To solve matrices use http://www.codeproject.com/KB/cs/CSML.aspx

Javed Akram
  • 15,024
  • 26
  • 81
  • 118
1

i think we've seen this question already: Solving a linear equation

Community
  • 1
  • 1
Letseatlunch
  • 2,432
  • 7
  • 28
  • 33
  • The language is slightly different, but the method is the same and we seem to be talking about algorithms anyway, so yeah, there's nothing new in this question. – Ben Voigt Dec 09 '10 at 00:53
  • 6
    Comments. Identification of duplicates belong in the comments. – dmckee --- ex-moderator kitten Dec 10 '10 at 02:58
  • 2
    I would argue this is NOT a duplicate. C, Objective C and C++ have completely different libraries than C#. Come on, this is not something you want to do yourself. Re-use, re-use and re-use! – lahjaton_j Nov 17 '16 at 07:15