Guassian Elimination

advertisement
Guassian Elimination
Guassian elimination can be used to solve systems of equations where
there are n equations with n unknowns.
Steps:
1. Represent the system of n linear equations in n unknowns with an
augmented matrix. The augmented matrix has n rows and n + 1
columns.
2. Reduce the augmented matrix to triangular form. In triangular
form, the coefficients below the diagonal in the matrix of
coefficients are all 0.
3. Scale the rows so that the diagonal elements are all 1.
4. Use back substitution to solve for the unknowns.
This works if there is a unique solution to the system of equations.
Non-zero elements on the diagonal (pivot) imply a unique a solution.
Scaling (all 1's in the diagonal) and Triangularizing (all 0's
below the diagonal) the Augmented Matrix:
Legal Row Operations:
1) Multiply a row by a constant.
2) Interchange rows.
3) Add one row to another.
When we are working with a particular diagonal element, we call that
element the pivot.
1) Always place the largest possible coefficient (absolute value) in the
pivot position to minimize computational round-off errors.
Interchange rows if necessary.
2) Scale the pivot row, so that the pivot will take the desired value 1, by
multiplying the pivot row by an appropriate constant.
3) Set all coefficients in the column below the pivot to zero by adding
one row to another.
void gauss(double aug[N][N+1], /* input/output - augmented matrix */
int *sol_existsp)
/* output - flag indicating whether
{
system has a unique solution */
int
j, k, p;
double xmult, piv_recip;
/* reciprocal of pivot */
*sol_existsp = TRUE;
for (p = 0; *sol_existsp && p < (N - 1); ++p) {
/* Pivots with respect to the pth row and the pth column */
pivot(aug, p, sol_existsp);
if (*sol_existsp) {
/* Scales pivot row */
piv_recip = 1.0 / aug[p][p];
aug[p][p] = 1.0;
for (k = p + 1; k < N + 1; ++k)
aug[p][k] *= piv_recip;
/* Eliminates coefficients beneath pivot */
for (j = p + 1; j < N; ++j) {
xmult = -aug[j][p];
aug[j][p] = 0;
for (k = p + 1; k < N + 1; ++k)
aug[j][k] += xmult * aug[p][k];
}
}
}
/* If last coefficient is zero, there is no unique solution */
if (aug[N-1][N-1] == 0) {
*sol_existsp = FALSE;
} else if (*sol_existsp) { /* Scales last row */
piv_recip = 1.0 / aug[N-1][N-1];
aug[N-1][N-1] = 1.0;
aug[N-1][N] *= piv_recip;
}
}
/* Performs pivoting with respect to the pth row and the pth column
* If no nonzero pivot can be found, FALSE is sent back through
* piv_foundp.
*/
void pivot(double aug[N][N+1],
/* input/output - augmented matrix */
int
p,
/* input - current row
*/
int *piv_foundp)
/* output - whether or not nonzero
pivot found */
{
double xmax, xtemp;
int j, k, max_row;
/* Finds maximum pivot */
xmax = fabs(aug[p][p]);
max_row = p;
for (j = p+1; j < N; ++j) {
if (fabs(aug[j][p]) > xmax) {
xmax = fabs(aug[j][p]);
max_row = j;
}
}
/* Swaps rows if nonzero pivot was found */
if (xmax == 0) {
*piv_foundp = FALSE;
} else {
*piv_foundp = TRUE;
if (max_row != p) {
/* swap rows */
for (k = p; k < N+1; ++k) {
xtemp = aug[p][k];
aug[p][k] = aug[max_row][k];
aug[max_row][k] = xtemp;
}
}
}
}
/*
*
*
*
*
*/
Performs back substitution to compute a solution vector to a system
of linear equations represented by the augmented matrix aug.
Assumes that the coefficient portion of the augmented matrix has
been triangularized, and its diagonal values are all 1.
void back_sub(double aug[N][N+1], /* input - scaled, triangularized
augmented
matrix */
double x[N])
/* output - solution vector
*/
{
double sum;
int
i, j;
x[N - 1] = aug[N - 1][N];
for (i = N - 2; i >= 0; --i)
{
sum = 0;
for (j = i + 1; j < N; ++j)
sum += aug[i][j] * x[j];
x[i] = aug[i][N] - sum;
}
}
Download