0% found this document useful (0 votes)
53 views8 pages

Exercise 2: Gaussian Elimination

The document describes Gaussian elimination, an algorithm for solving systems of linear equations. It involves performing elementary row operations on the coefficient matrix to transform it into row echelon form. The three elementary row operations are swapping two rows, multiplying a row by a non-zero number, and adding a multiple of one row to another row. Once in row echelon form, back substitution can be used to solve for the variables. The code provided implements Gaussian elimination to solve systems of linear equations input by the user.

Uploaded by

Bea Oro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views8 pages

Exercise 2: Gaussian Elimination

The document describes Gaussian elimination, an algorithm for solving systems of linear equations. It involves performing elementary row operations on the coefficient matrix to transform it into row echelon form. The three elementary row operations are swapping two rows, multiplying a row by a non-zero number, and adding a multiple of one row to another row. Once in row echelon form, back substitution can be used to solve for the variables. The code provided implements Gaussian elimination to solve systems of linear equations input by the user.

Uploaded by

Bea Oro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Exercise 2

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;

/** Class GaussianElimination **/


public class Exercise2
{
public void solve(double[][] A, double[] B)
{
int N = B.length;
for (int k = 0; k < N; k++)
{
/** find pivot row **/
int max = k;
for (int i = k + 1; i < N; i++)
if (Math.abs(A[i][k]) > Math.abs(A[max][k]))
max = i;

/** swap row in A matrix **/


double[] temp = A[k];
A[k] = A[max];
A[max] = temp;

/** swap corresponding values in constants matrix **/


double t = B[k];
B[k] = B[max];
B[max] = t;

/** pivot within A and B **/


for (int i = k + 1; i < N; i++)
{
double factor = A[i][k] / A[k][k];
B[i] -= factor * B[k];
for (int j = k; j < N; j++)
A[i][j] -= factor * A[k][j];
}
}

/** Print row echelon form **/


printRowEchelonForm(A, B);
/** back substitution **/
double[] solution = new double[N];
for (int i = N - 1; i >= 0; i--)
{
double sum = 0.0;
for (int j = i + 1; j < N; j++)
sum += A[i][j] * solution[j];
solution[i] = (B[i] - sum) / A[i][i];
}
/** Print solution **/
printSolution(solution);
}
/** function to print in row echleon form
* @param A
* @param B **/
public void printRowEchelonForm(double[][] A, double[] B)
{
int N = B.length;
System.out.println("\nRow Echelon form : ");
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
System.out.printf("%.3f ", A[i][j]);
System.out.printf("| %.3f\n", B[i]);
}
System.out.println();
}
/** function to print solution
* @param sol **/
public void printSolution(double[] sol)
{
int N = sol.length;
System.out.println("\nSolution : ");
for (int i = 0; i < N; i++)
System.out.printf("%.3f ", sol[i]);
System.out.println();
}
/** Main function
* @param args **/
public static void main (String[] args)
{
JOptionPane.showMessageDialog(null,"Gaussian Elimination
Algorithm Test\n");
/** Make an object of GaussianElimination class **/
Exercise2 ge = new Exercise2 ();

String x = JOptionPane.showInputDialog("\nEnter number of


variables:");
int n=Integer.parseInt(x);

double v[][]=new double[n][n+1];

int i,j;

for(i=0;i<n;i++)

JOptionPane.showMessageDialog(null,"Equation:"+(i+1)+"\n");

for(j=0;j<n;j++)

String y = JOptionPane.showInputDialog("Enter the


coefficient of variable:"+(j+1));

v[i][j]=Integer.parseInt(y);

String w = JOptionPane.showInputDialog("\nEnter the value of


the right hand side of equation:"+(i+1));

v[i][j]=Integer.parseInt(w);

}
gauss(v,n,n+1);

public static void gauss(double v[][],int r,int c)

double t[]=new double[c];

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++)
{

JOptionPane.showMessageDialog(null,"The value of term "+


(i+1)+" is "+v[i][c-1]);

You might also like