Facebook
From ja, 5 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 253
  1. #include <iostream>
  2. #include <math.h>
  3. #include <string>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. double n = 5.0;
  9. double epsilon = 0.0001;
  10. double mli = 30.0;
  11.  
  12. double data[] = { 10, 1, 1, 1, 2, 15, 3, 20, 4, 2, 1, 30, 5, 1, 40, 9, 5, 60, 3, 0, 1, 10, 1, 15, 6, 2, 1, 1, 20, 30 };
  13. //double data[] = { 10, -1, 1, 1, 2, 13, -3, 20, 4, 2, 1, 24, 5, -1, 40, 9, 5, 58, 3, 0, -1, 10, 1, 13, 6, -2, 1, 1, 20, 26 };
  14.  
  15. double A[5][5];
  16. double Alfa[5][5];
  17. double wektor[5];
  18. double Beta[5];
  19. double x_nowy[5], x_stary[5];
  20. int x=0;
  21. void funkcja_b()
  22. {
  23.         std::cout << "Macierz alfa"<<endl;
  24.         //tworzymy alfa
  25.         for (int i = 0; i < n; i++)
  26.         {
  27.                 for (int j = 0; j < n;j++)
  28.                 {
  29.                         if (i == j){           
  30.                                 Alfa[i][j] = 0;
  31.                         }
  32.                         else{
  33.                                 if (A[i][i] != 0){
  34.                                         Alfa[i][j] = -(A[i][j] / A[i][i]);
  35.                                 }
  36.                         }      
  37.                 }
  38.         }
  39.  
  40.         //tworzymy beta
  41.  
  42.         for (int i = 0; i < n; i++){
  43.                 Beta[i] = wektor[i] / A[i][i];
  44.                
  45.         }
  46.  
  47. }
  48.  
  49. void funkcja_c()
  50. {      
  51.  
  52.         double norma2 = 0.0;
  53.         int licznik = 0;
  54.  
  55.  
  56.         for (int i = 0; i < n; i++){
  57.                 x_nowy[i] = Beta[i];
  58.         }
  59.  
  60.         do{
  61.                 for (int i = 0; i < n; i++)
  62.                 {
  63.                         x_stary[i] = x_nowy[i];
  64.                 }
  65.  
  66.                 licznik++;
  67.  
  68.                 for (int i = 0; i <= n; i++){
  69.  
  70.                         for (int j = 0; j <= i - 1;j++){
  71.                                 x_nowy[i] += Alfa[i][j] * x_nowy[j];
  72.                         }
  73.                         for (int j = i + 1; j < n; j++){
  74.                                 x_nowy[i] += Alfa[i][j] * x_stary[j];
  75.                                 //cout << x_nowy<<endl;
  76.                         }
  77.                         x_nowy[i] += Beta[i];
  78.                 }
  79.                 norma2 = 0;
  80.                 for (int i = 0; i < n; i++){
  81.                                 norma2 += abs(x_nowy[i]-x_stary[i]);
  82.                                 //cout << norma2<<endl;
  83.                 }
  84.                 norma2 = norma2/n;
  85.         } while (norma2 > epsilon && licznik < mli);
  86.  
  87.         cout << endl <<"Licznik iteracji " << licznik << endl << " norma2 " << norma2 << endl;
  88. }
  89.  
  90. int main()
  91. {
  92.         for (int i = 0; i < n; i++)
  93.         {
  94.                 for (int j = 0; j < n; j++)
  95.                 {
  96.                         A[i][j] = data[x];
  97.                         x++;
  98.                 }
  99.                 wektor[i] = data[x];
  100.                 x++;
  101.         }
  102.  
  103.  
  104.         funkcja_b();
  105.  
  106.         cout << "Macierz alfa" << endl;
  107.         for (int i = 0; i < n; i++){
  108.                 for (int j = 0; j < n; j++){
  109.                         cout << Alfa[i][j] << "\t";
  110.                 }
  111.                 cout << endl;
  112.         }
  113.  
  114.         cout << endl<< "Wektor beta" << endl;
  115.         for (int i = 0; i < n; i++){
  116.                 cout << Beta[i] << endl;
  117.         }
  118.  
  119.         funkcja_c();
  120.  
  121.         return 0;
  122. }