views:

268

answers:

4

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

+3  A: 

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:

alt text

This is the vector of unknowns:

alt text

Here is the right-hand-side vector:

alt text

You solve this system of equations by solving

alt text

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.

duffymo
No time now. Maybe later. Is it untrue? Is it completely unhelpful? The OP doesn't even seem to be aware of them.
duffymo
@duffymo - if you accept that the OP can indeed solve them with pencil and paper, then he is aware of them.
IVlad
I'm not accepting anything. The OP coming here to ask this question suggests that they have no idea what to do next.
duffymo
"Pencil and paper" does not imply knowledge of linear algebra. We did these things in high school by rearranging the equations until we could substitute something into something else in another equation. OP was probably looking for a coded version of the same algorithm.
Jurily
+10  A: 

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.

eduffy
+1 best answer here
Dave
@eduffy, could you please correct the spelling of Gauss, helps searches afterwards ;-)
Jens Gustedt
A: 

Linear equations can be solved by many methods. Please check this link.

Praveen S
+1  A: 

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.

IVlad
How is this less general or simpler than the other question? Guassian elimination is the solution to the other one as well, and it is worth using even for 4 variables (or even 3, or even 2 — though in that case it's indistinguishable from a lot of other algorithms :-)).
ShreevatsaR
@IVlad: I thought so too... but plugging them into Mathematica gives an answer of over 10 lines for each variable.
Thomas
Full Gaussian elimination is not the way to go. Without pivoting it can be unstable. If this is your idea of an improved answer you have a lot to learn about applying linear algebra in the real world.
duffymo
@duffymo - and you have a lot to learn about paying attention before shutting someone down. The wikipedia link explains pivoting.
IVlad
Yup, you're probably right. It's not nearly as important to me as it appears to be to you. I've solved these kinds of problems for a living. You haven't done much besides university that I can see.
duffymo
@duffymo - I'm not sure how any of that is relevant. I offered a reasonable suggestion and linked to a page that explains it in decent depth and has references to other pages that explain it even better. If you've done this for a living then you have my respect, but I can't respect you coming up with personal arguments against my suggestion and vague references to your work history and to broad fields such as "linear algebra". I removed the -1 from your post, that's a lot more helpful and I'm pretty sure it didn't take more than a few seconds to write.
IVlad
Linear algebra is what we're talking about. "Broad fields"? It's the topic at hand. I don't always have time to do all that I'd like. Sometimes I'll go back and revise if the urge and time allow. I wouldn't be as worried about chiding others to improve their answers the way you did. I tend to worry about myself and only vote down things that are blatantly wrong. Your "-1 until you improve this" seemed rather nagging and unnecessary to me. I didn't appreciate it much this time and won't in the future.
duffymo