Advertisement
Guest User

Untitled

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