Advertisement
Archangelpl

Untitled

Apr 24th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. // MO LAB 5 LU.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4.  
  5. #include "stdafx.h"
  6. #include<iostream>
  7. #include <cmath>
  8. #include <fstream>
  9. #include <cstdlib>
  10. #define N 4
  11. using namespace std;
  12.  
  13. double **stworzMacierz(int n);
  14. void usunMacierz(double **matrix, int n);
  15. void uzupelnijMacierz(double **matrix, int n);
  16. void pokarzMacierz(double **matrix, int n);
  17. int wybor_elementu_podstawowego(double **matrix, int n, int j, int *index);
  18. void gauss(double **matrix, int *index);
  19. void macierzL(double**l, double *b, int *index, int n);
  20. void macierzU(double**u, double *b, int *index, int n);
  21. void pokarzWektor(double *b, int n);
  22.  
  23. int main()
  24. {
  25.  
  26. double** matrix = stworzMacierz(N);
  27. double b[4] = { 0.0, 114.0, -5.0, 177.0 };
  28. int index[4] = { 0, 1, 2, 3 };
  29.  
  30. uzupelnijMacierz(matrix, N);
  31. cout << endl << "Macierz A: " << endl;
  32. pokarzMacierz(matrix, N);
  33.  
  34. cout << endl << "Wektor B: " << endl;
  35. pokarzWektor(b, N);
  36.  
  37. gauss(matrix, index);
  38. cout << endl <<endl<< "Macierz po eliminacji Gaussa: " << endl;
  39. pokarzMacierz(matrix, N);
  40.  
  41. macierzL(matrix, b, index, N-1);
  42. cout << endl << endl << "Wektor Y" << endl;;
  43.  
  44. pokarzWektor(b, N);
  45. cout << endl << endl;
  46. macierzU(matrix, b, index, N-1);
  47.  
  48. cout << "Wektor X " << endl;
  49. pokarzWektor(b, N);
  50. usunMacierz(matrix, N);
  51. cout << endl;
  52.  
  53. return 0;
  54. }
  55.  
  56.  
  57. double **stworzMacierz(int n) {
  58. double **matrix;
  59. matrix = new double *[n];
  60. for (int i = 0; i < n; i++)
  61. matrix[i] = new double[n];
  62. return matrix;
  63. }
  64.  
  65. void uzupelnijMacierz(double **matrix, int n) {
  66. double tmp[4][4] = { { 1.0, 20.0, -30.0, -4.0 },
  67. { 4.0, 20.0,-6.0, 50.0 },
  68. { 9.0, -18.0, 12.0, -11.0 },
  69. { 16.0, -15.0, 14.0, 130.0 } };
  70. for (int i = 0; i < n; i++)
  71. for (int j = 0; j < n; j++)
  72. matrix[i][j] = tmp[i][j];
  73. }
  74.  
  75. void usunMacierz(double **matrix, int n) {
  76. for (int i = n - 1; i >= 0; i--)
  77. delete[]matrix[i];
  78. delete[]matrix;
  79. }
  80. void pokarzWektor(double *b, int n)
  81. {
  82. for (int i = 0; i < n;i++)
  83. cout << b[i] << endl;
  84. }
  85. void pokarzMacierz(double **matrix, int n) {
  86. //cout.setf(ios::basefield);
  87. //cout.precision(3);
  88. for (int i = 0; i < n; i++) {
  89. for (int j = 0; j < n; j++)
  90. cout <<matrix[i][j] << "\t";
  91. cout << endl;
  92. }
  93. }
  94.  
  95. // częściowy wybór elementu podstawowego w przypadku, gdy dana wartość na przekątnej macierzy jest równa 0
  96. int wybor_elementu_podstawowego(double **matrix, int n, int j, int *index) {
  97. int row;
  98. for (int i = n; i < j; i++) {
  99. if (fabs(matrix[index[i]][n]) < fabs(matrix[index[i + 1]][n]))
  100. row = index[i + 1];
  101. else
  102. row = index[i];
  103. }
  104. return row;
  105. }
  106.  
  107. void gauss(double **matrix, int *index) {
  108. int row;
  109. double v;
  110. for (int k = 0; k < 3; k++) {
  111. if (matrix[index[k]][index[k]] == 0.0) {
  112. row = wybor_elementu_podstawowego(matrix, index[k], 3, index);
  113. index[row] = index[k]; // zapisuje w tablicy i podmieniam indeksy
  114. index[k] = row;
  115. }
  116. for (int i = k + 1; i < 4; i++) {
  117. v = matrix[index[i]][k];
  118. for (int j = k + 1; j < 4; j++)
  119. matrix[index[i]][j] = matrix[index[i]][j] - matrix[index[k]][j] * (v / matrix[index[k]][k]); //wyliczanie wartości macierzy U z eliminacji Gaussa
  120. matrix[index[i]][k] = v / (matrix[index[k]][k]); // Zapisanie L w tej samej macierzy co U
  121. } // L macierz współczynników przez które mnożymy kolejne wiersze (a[i][k]/a[k][k])
  122. }
  123. }
  124.  
  125. // Ly=b , wyznaczenie y
  126. void macierzL(double **l, double *b, int *index, int n) {
  127. double sum = 0;
  128. for (int i = 0; i <= n; i++) {
  129. for (int j = 0; j < i; j++)
  130. sum = sum + l[index[i]][j] * b[index[j]];
  131. b[index[i]] = (b[index[i]] - sum) / 1.0; //macierz L posiada jedynki na głownej przekątnej
  132. sum = 0;
  133. }
  134. }
  135.  
  136. // Ux=y , wyznaczenie x
  137. void macierzU(double **u, double *b, int *index, int n) {
  138. double sum = 0;
  139. for (int i = n; i >= 0; i--) {
  140. for (int j = i + 1; j <= n; j++)
  141. sum = sum + u[index[i]][j] * b[index[j]];
  142. b[index[i]] = (b[index[i]] - sum) / (u[index[i]][i]);
  143. sum = 0;
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement