You remember the ‘elimination method’ of solving a system of equations, right? You eliminate variables from each equation. Start with this:
AX + BY + CZ = Constant 1
DX + EY + FZ = Constant 2
GZ + HZ + IZ = Constant 3
and wind up with this:
AX + BY + CZ = Constant 1
DY + EZ = Constant 2
FZ = Constant 3
Once you have the value of Z, you substitute that value in the second equation to find Y and then substitute the values of Z and Y in the first equations. Only, with all the writing and rewriting, I always had the problem of writing down the wrong sign or wrong coefficient and having to start over. If I were to work these equations today, no doubt I’d still be careless.
Now, a system of equations can be easily solved by entering the coefficients into a matrix, entering the constants (on the right side) into a matrix, and multiplying the inverse of the former by the latter. ([A][sup]-1[/sup] * **) But suppose I want to write an program to solve the system the old-fashioned way? Not that I would, since the matrix method is easier. But what if?
I suppose I’d multiply equation 1 by the leading coefficient of equation 2, and vice versa. Add them together. If the leading coefficient of the result is not equal to zero, multiply one of the equations by negative one and re-add. And so forth.
Maybe this should be in MPSIMS, since it’s utterly pointless. But has anyone tried writing such a program just as an exercise? Would my brute force approach work?
I believe what you’re looking for is “Gauss-Jordan elimination.” And yes, people have written programs to do it; try Googling “Gauss-Jordan elimiation calculator.”
It sounds like you’re describing row-reduction. (See also.) A related technique along the same lines, [url=]Gauss-Jordan elimination, is actually commonly used in computer algorithms to invert matrices.
Say, I remember that term! Looking it up, I now remember ‘reduced row echelon form’ too. According to Wiki, Gauss-Jordan elimination results in a matrix and uses multiplication by the inverse. I was wondering about a more ‘brute force’ method. That is, doing it the same way I’d do it with a pencil and paper. Now that I think of it, even that is sort of a matrix; you just still have variables in it instead of just the coefficients. I see a program on Wiki’s REF page. Not a language I know, but I can probably figure it out. (I know Easytrieve, which is a sequential language, and kind of remember basic c.1980 BASIC.)
Unless I’m confusedly mis-remembering my terminology, Gauss-Jordan elimination is row-reduction: it involves using row operations to get a matrix into reduced row echelon form so that you can easily see what the solution(s) to the system are. It doesn’t involve finding the inverse to a matrix (although finding the inverse of a matrix also is often done by row-reduction). It does involve working with a matrix, but the rows of the matrix are just the coefficients of your equations, with the variables and + signs left out, and you do to the matrix essentially what you’d be doing to the equations.
You may also be interested in Cramer’s Rule. It involves a lot of straightforward computation, but is less general than Gauss-Jordan.
Edited to add:
The Wiki article on Gauss-Jordan may not be clear. It’s probably mentioning the inverse because Gauss Jordan is the most common method of finding the inverse. (Also, if they use “I” it means the Identity Matrix, not the inverse). See here for a concise display of the method.
Cramer’s Rule is an interesting mathematical oddity, but should never be recommended as an actual method for actually solving equations, since there are far faster methods which can be used either by hand or on a computer.
You could program it up yourself, but numerical algorithms are generally finicky enough that it’s worth your while to use somebody else’s code if you can. Both R and Matlab will do it for you, and there’s probably something in the Boost libraries.
That said, if I had to write this myself, I’d definitely do an LU decomposition and work from there.
I missed this part of the OP the first time around. This is a pretty standard first homework assignment in a numerical methods course, and you would normally use LU decomposition rather than Gauss-Jordan elimination.
As I said, it’s just idly curious. I have stacks of work at the job, so it’s not going to be something I’d try (especially when I can pull out the calculator if I ever need to solve such a thing).