|
|
0 1 0 3 4 5 6 7 8 |
A[j][i]=0
. Also, since you will already have cleared all columns before the ith, you could start the loop on line 6 at k=i, rather than k=0. Overall, this will half the amount of computation, as well as reducing round-off error.(3) If you are genuinely doing gaussian elimination then you should simultaneously be doing the same pivoting on the vector b; I can see no evidence of it, nor of the back-substitution afterwards to find x. |
|
|
|
|
|
|
|
|
I hope that you have been doing Gaussian elimination on a COPY of A and b, because you change the original matrices. For the back-substitution: 1 2 3 4 5 6 7 // Back-substitute for ( int i = p - 1; i >= 0; i-- ) { x[i] = b[i]; for ( int k = i + 1; k < p; k++ ) x[i] -= A[i][k] * x[k]; x[i] /= A[i][i]; } In your back-substitution code (the middle sample of your post) on line 5, A[i][p] would be going outside array bounds, line 8 should be cumulating changes to x[i], rather than replacing them, and what is currently line 9 should be after the j loop. You should think how you do this by hand with a small example. |