问题
Possible Duplicate:
System of linear equations in C++?
I have the following 2 systems of equations:
For a,b,c,d:
0 = a * r1_x + b * r1_x * r1_y + c * r1_y + d
1 = a * r2_x + b * r2_x * r2_y + c * r2_y + d
0 = a * r3_x + b * r3_x * r3_y + c * r3_y + d
1 = a * r4_x + b * r4_x * r4_y + c * r4_y + d
For e,f,g,h:
0 = e * r1_x + f * r1_x * r1_y + g * r1_y + h
0 = e * r2_x + f * r2_x * r2_y + g * r2_y + h
1 = e * r3_x + f * r3_x * r3_y + g * r3_y + h
1 = e * r4_x + f * r4_x * r4_y + g * r4_y + h
I know the values of r1_x, r1_y, r2_x, r2_y, r3_x, r3_y, r4_x, r4_y, and need to solve for a,b,c,d in the first one, and ,e,f,g, h in the second.
I know how I would solve these with pencil and paper, but I'm really unsure how to program it. How could I solve the above equations in C or C++ (or psuedocode).
Thanks
回答1:
You can map it to a matrix system, A x = b
, where A
is the coefficient matrix, b
is the solution vector, and x
are the unknowns. You can either implement Gaussian elimination, or use a well known library. If you use LAPACK, the routine you want it dgesv
.
回答2:
Linear algebra and matricies are your friends here.
Eigen looks like a recent C++ linear algebra library. See if it can help you.
Here is what your system of equations looks like. This is the matrix:
(source: equationsheet.com)
This is the vector of unknowns:
(source: equationsheet.com)
Here is the right-hand-side vector:
(source: equationsheet.com)
You solve this system of equations by solving
Since your matrix is block diagonal, so is your solution.
You can enter your linear equation into Wolfram Alpha and get a symbolic solution.
Here is the solution for one of your systems. You can see the form that the matrix takes.
回答3:
You can use Gaussian elimination, but that's probably overkill if you'll only ever have 4 equations with 4 variables.
If you can solve it on paper, then solve it on paper, find the formulas for a, b, c, d
and e, f, g, h
then just plug them into your program.
来源:https://stackoverflow.com/questions/3276341/solving-a-system-of-equations-programmably