Advertisement
adwas33

Untitled

Dec 30th, 2022
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. public static double[] solve(double[][] A, double[] B)// KOD z neta
  2. {
  3. int N = B.length;
  4. for (int k = 0; k < N; k++)
  5. {
  6. /** find pivot row **/
  7. int max = k;
  8. for (int i = k + 1; i < N; i++)
  9. if (Math.abs(A[i][k]) > Math.abs(A[max][k]))
  10. max = i;
  11.  
  12. /** swap row in A matrix **/
  13. double[] temp = A[k];
  14. A[k] = A[max];
  15. A[max] = temp;
  16.  
  17. /** swap corresponding values in constants matrix **/
  18. double t = B[k];
  19. B[k] = B[max];
  20. B[max] = t;
  21.  
  22. /** pivot within A and B **/
  23. for (int i = k + 1; i < N; i++)
  24. {
  25. double factor = A[i][k] / A[k][k];
  26. B[i] -= factor * B[k];
  27. for (int j = k; j < N; j++)
  28. A[i][j] -= factor * A[k][j];
  29. }
  30. }
  31.  
  32. /** Print row echelon form **/
  33. // printRowEchelonForm(A, B);
  34.  
  35. /** back substitution **/
  36. double[] solution = new double[N];
  37. for (int i = N - 1; i >= 0; i--)
  38. {
  39. double sum = 0.0;
  40. for (int j = i + 1; j < N; j++)
  41. sum += A[i][j] * solution[j];
  42. solution[i] = (B[i] - sum) / A[i][i];
  43. }
  44. return solution;
  45. /** Print solution **/
  46. // printSolution(solution);
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement