Best way to solve a linear equation in code [duplicate]
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 + 开发者_JAVA技巧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?
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.
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
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
i think we've seen this question already: Solving a linear equation
精彩评论