Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to solve a linear equation in code [duplicate]

Tags:

.net

math

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?

like image 636
RaindeerJohn Avatar asked Dec 08 '10 03:12

RaindeerJohn


People also ask

What is the best way to solve a linear system in two variables?

There are three ways to solve systems of linear equations in two variables: graphing. substitution method. elimination method.


2 Answers

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.

like image 186
Ben Voigt Avatar answered Nov 02 '22 10:11

Ben Voigt


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

like image 42
DGH Avatar answered Nov 02 '22 09:11

DGH