Exercise 2: Gaussian Elimination
Exercise 2: Gaussian Elimination
Gaussian Elimination
Introduction:
Engineers often need to solve large systems of linear equations;
for example, in determining the forces in a large framework or
finding currents in a complicated electrical circuit. The method of
Gauss elimination provides a systematic approach to their
solution.
In linear algebra, Gaussian elimination (also known as row
reduction) is an algorithm for solving systems of linear equations.
It is usually understood as a sequence of operations performed on
the associated matrix of coefficients. This method can also be
used to find the rank of a matrix, to calculate the determinant of a
matrix, and to calculate the inverse of an invertible square matrix.
The method is named after Carl Friedrich Gauss (17771855),
although it was known to Chinese mathematicians as early as 179
CE (see History section).
To perform row reduction on a matrix, one uses a sequence
of elementary row operations to modify the matrix until the lower
left-hand corner of the matrix is filled with zeros, as much as
possible. There are three types of elementary row operations: 1)
Swapping two rows, 2) Multiplying a row by a non-zero number, 3)
Adding a multiple of one row to another row. Using these
operations, a matrix can always be transformed into an upper
triangular matrix, and in fact one that is in row echelon form.
Once all of the leading coefficients (the left-most non-zero entry in
each row) are 1, and every column containing a leading
coefficient has zeros elsewhere, the matrix is said to be
in reduced row echelon form. This final form is unique; in other
words, it is independent of the sequence of row operations used.
For example, in the following sequence of row operations (where
multiple elementary operations might be done at each step), the
third and fourth matrices are the ones in row echelon form, and
the final matrix is the unique reduced row echelon form.
Code:
package exercise2 ;
import javax.swing.JOptionPane;
int i,j;
for(i=0;i<n;i++)
JOptionPane.showMessageDialog(null,"Equation:"+(i+1)+"\n");
for(j=0;j<n;j++)
v[i][j]=Integer.parseInt(y);
v[i][j]=Integer.parseInt(w);
}
gauss(v,n,n+1);
int i=0,j=0,k=0,m=0,n=0,x=0;
double temp=0;
for(i=0;i<r-1;i++)
temp=v[i][i];
for(j=0;j<c;j++)
t[j]=v[i][j]/temp;
v[i][j]=t[j];
k=0;
for(m=i+1;m<r;m++)
temp=v[m][x];
for(n=0;n<c;n++)
v[m][n]=v[m][n]-(t[n]*temp);
for(k=0;k<c;k++)
t[k]=0;
temp=0;
x++;
for(i=r-1;i>0;i--)
v[i][c-1]=v[i][c-1]/v[i][i];
v[i][i]=0;
for(j=i-1;j>=0;j--)
v[j][c-1]=v[j][c-1]-(v[j][i]*v[i][c-1]);
v[j][i]=0;
}
System.out.println();
for(i=0;i<r;i++)
{