![]() |
|
#1
|
|||
|
|||
Gauss-Jordan Elimination on Matrices - Help NeededHi guys, I'm new to c++, i'm writing code to do Gauss-Jordan elimination on a matrix.
It is doing it almost correctly, except that every other entry in my inverse is coming up as zero! There must be a problem with my loops, but the numbers that ARE there are correct, so it must be something minor. Any help appreciated. Very sorry for the messy and unproffessional but I am a new student! YOU CAN IGNORE MOST OF THE CODE, The problem function in question is gaussJordan(.,.) called through inv(.,.), it's near the bottom. Try running the program and ask it to do inverse for N= 5 or 6, you'll see a 'diamond' pattern of zeros in the inverse matrix. Thanks very much CPP / C++ / C Code:
|
|
#2
|
|||
|
|||
Re: Gauss-Jordan Elimination on Matrices - Help NeededQuote:
It's an impressive amount of work, and I think you missed it by that much (If I had my webcam turned on, you could see that I am holding my forefinger about a millimeter from my thumb. But I won't do that, since you could also see my bunny-rabbit pajamas, and that's just too, too cutsie.) Well, what works and what doesn't work? Does it solve the first equation correctly? If you give it N = 2; what is the answer? Is it what you expect? (We'll skip that part for now, since you didn't complain about part a.) How about the second part: If you give it N = 2, so that the matrix that it will try to invert is Code:
What is the correct answer? I'm thinking it would be Code:
So: what did you get? If it turns out that it can't give the correct answer for N = 2, then lets use that for debugging. Then we can calculate all steps by hand (by brain, actually) and that will definitely be an advantage when going through the program's steps. My point is that if you would give us an example of the exact input that you got and the expected output and the program's output then we might save some time and board bandwidth. Sometimes I get really carried away and end up answering questions that weren't asked. Oh, well... I will press on. And, pick the easiest, smallest example that exhibits the bad behavior. Sometimes by just formulating a question to the forum you can actually see things in a way that previously escaped you. I don't know how many times that, as I was trying to articulate my take on a problem so that I could explain it to someone else, the answer just jumped out at me. It happens. Since you say that the answer given by the program is not what you expected, why not let the program tell you what it is working on at each step. Of course you should always print what it's starting with, and I'm glad to see that you do that. Also for the augmentation step. Good show! So run the Gauss-Jordan part for N = 2. I am assuming you got the wrong answer. So: what the heck could have gone wrong???? Now something went wrong between input and output, so why not show the results at each step of the way? Loop1: The upper triangular reduction? Loop2: The diagonalization? Loop3: The normalization How about the cleanup? All of these cause changes in the things that affect the answer. Why not print out augmented matrix values after each loop? (Also, just to make things easier to do by hand, you might comment out the pivot operation for now, since this particular matrix won't need it.) So: CPP / C++ / C Code:
Now, if this is OK, but the final answer is incorrect, then go to the next step: CPP / C++ / C Code:
If this is OK, then go to the next: CPP / C++ / C Code:
Note that I give the output enough context to make sure I know where the program is when it does each matrix printout. I am always surprised to see people with unlabeled output statements sprinkled throught their code with the result that they can't keep track of what the extra printed stuff means. (It also makes it easier to find the extra output statements when you want to delete them after debugging if you put a little extra narrative in the print statements.) Of course if you get the wrong answer after any given loop, then stop and look at what the loop is doing. If you can't see any problems, then put print statements inside the loop. Loop 3 looks kind of hairy, but you have obviously put a lot of thought into it, and it may very well be ok. If the answer at the end of loop 3 is what you expect, Then there is only one thing left: the cleanup. See footnote. Now, after the cleanup loop you print the answer. So, if loop3 gives the right answer but the final is wrong, then the problem must be there, right? If you get stuck, ask again, but I'm thinking that you can get to the bottom of things from here. (Don't forget to go back and uncomment the pivot statement before further testing.) By manual calculation, I think the answers after loop1, 2, and 3 should be Code:
(but I could be wrong). Is that what you think? Is that what you got? Regards, Dave Footnote: "Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth." ---Sherlock Holmes in several stories by Sir Arthur Conan Doyle, 1859-1930 |
|
#3
|
|||
|
|||
Re: Gauss-Jordan Elimination on Matrices - Help NeededDave, thanks so much for your helpful reply. Stupidly I hadn't thought of just using the 2x2 case to debug it. You were right, the first loop does give the right answer, it's the cleanup that messes everything up!
I figured it out, i was missing a call to fabs(.) in the 'if' of my cleanup subroutine, meaning that any negative values were getting wiped out! The interesting thing is that no matter what size matrix you choose, the negatives (and therefore the incrrect zeros) accur in a diamond pattern! I haven't time to work out the maths behind that, it's probably linked to the formula used to initalise the matrix. Or is it something to do eigenvalues? (Anyway, don't bother answering that...) Thanks for your time, I've learned a valuable approach to debugging. |
|
#4
|
|||
|
|||
Re: Gauss-Jordan Elimination on Matrices - Help NeededQuote:
I think you are approaching understanding of the meaning of the statement by Richard W. Hamming that, "The purpose of computing is insight not numbers." It's human nature to "connect the dots"; that is to discover the nature of the process that results in an unexpected pattern. Sometimes it helps our logical thought process and sometimes not. (Sometimes it leads to new discoveries but doesn't always help to debug the program.) Regards, Dave "Research is when you don't know what you are doing." ---davekw7x |
Recent GIDBlog
Toyota - 2008 September Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The