Advertisement
rootUser

Gauss Elemination partial pivot

Jun 13th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.26 KB | None | 0 0
  1. package gausselemination;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class GaussianElimination
  6. {
  7.     public void solve(double[][] A, double[] B)
  8.  
  9.     {
  10.  
  11.         int N = B.length;
  12.  
  13.         for (int k = 0; k < N; k++)
  14.  
  15.         {
  16.  
  17.             /** find pivot row **/
  18.  
  19.             int max = k;
  20.  
  21.             for (int i = k + 1; i < N; i++)
  22.  
  23.                 if (Math.abs(A[i][k]) > Math.abs(A[max][k]))
  24.  
  25.                     max = i;
  26.  
  27.  
  28.  
  29.             /** swap row in A matrix **/    
  30.  
  31.             double[] temp = A[k];
  32.  
  33.             A[k] = A[max];
  34.  
  35.             A[max] = temp;
  36.  
  37.  
  38.  
  39.             /** swap corresponding values in constants matrix **/
  40.  
  41.             double t = B[k];
  42.  
  43.             B[k] = B[max];
  44.  
  45.             B[max] = t;
  46.  
  47.  
  48.  
  49.             /** pivot within A and B **/
  50.  
  51.             for (int i = k + 1; i < N; i++)
  52.  
  53.             {
  54.  
  55.                 double factor = A[i][k] / A[k][k];
  56.  
  57.                 B[i] -= factor * B[k];
  58.  
  59.                 for (int j = k; j < N; j++)
  60.  
  61.                     A[i][j] -= factor * A[k][j];
  62.  
  63.             }
  64.  
  65.         }
  66.  
  67.  
  68.  
  69.         /** Print row echelon form **/
  70.  
  71.         printRowEchelonForm(A, B);
  72.  
  73.  
  74.  
  75.         /** back substitution **/
  76.  
  77.         double[] solution = new double[N];
  78.  
  79.         for (int i = N - 1; i >= 0; i--)
  80.  
  81.         {
  82.  
  83.             double sum = 0.0;
  84.  
  85.             for (int j = i + 1; j < N; j++)
  86.  
  87.                 sum += A[i][j] * solution[j];
  88.  
  89.             solution[i] = (B[i] - sum) / A[i][i];
  90.  
  91.         }        
  92.  
  93.         /** Print solution **/
  94.  
  95.         printSolution(solution);
  96.  
  97.     }
  98.  
  99.     /** function to print in row    echleon form **/
  100.  
  101.     public void printRowEchelonForm(double[][] A, double[] B)
  102.  
  103.     {
  104.  
  105.         int N = B.length;
  106.  
  107.         System.out.println("\nRow Echelon form : ");
  108.  
  109.         for (int i = 0; i < N; i++)
  110.  
  111.            {
  112.  
  113.                for (int j = 0; j < N; j++)
  114.  
  115.                    System.out.printf("%.3f ", A[i][j]);
  116.  
  117.                System.out.printf("| %.3f\n", B[i]);
  118.  
  119.            }
  120.  
  121.            System.out.println();
  122.  
  123.     }
  124.  
  125.     /** function to print solution **/
  126.  
  127.     public void printSolution(double[] sol)
  128.  
  129.     {
  130.  
  131.         int N = sol.length;
  132.  
  133.         System.out.println("\nSolution : ");
  134.  
  135.         for (int i = 0; i < N; i++)
  136.  
  137.             System.out.printf("%.3f ", sol[i]);  
  138.  
  139.         System.out.println();    
  140.  
  141.     }    
  142. }
  143.  
  144. public class Main
  145. {
  146.     public static void main(String[] args)
  147.     {
  148.         Scanner scan = new Scanner(System.in);
  149.  
  150.         System.out.println("Gaussian Elimination Algorithm Test\n");
  151.  
  152.         /** Make an object of GaussianElimination class **/
  153.  
  154.         GaussianElimination ge = new GaussianElimination();
  155.  
  156.  
  157.  
  158.         System.out.println("\nEnter number of variables");
  159.  
  160.         int N = scan.nextInt();
  161.  
  162.  
  163.  
  164.         double[] B = new double[N];
  165.  
  166.         double[][] A = new double[N][N];
  167.  
  168.  
  169.  
  170.         System.out.println("\nEnter "+ N +" equations coefficients ");
  171.  
  172.         for (int i = 0; i < N; i++)
  173.  
  174.             for (int j = 0; j < N; j++)
  175.  
  176.                 A[i][j] = scan.nextDouble();
  177.  
  178.  
  179.  
  180.         System.out.println("\nEnter "+ N +" solutions");
  181.  
  182.         for (int i = 0; i < N; i++)
  183.  
  184.             B[i] = scan.nextDouble();
  185.  
  186.  
  187.  
  188.         ge.solve(A,B);
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement