Guest User

Untitled

a guest
Oct 12th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4.  
  5. using namespace std;
  6.  
  7. int n = 5;
  8.  
  9.  
  10.  
  11. double get_random(int fMin, int fMax){
  12.     double f = (double)rand() / RAND_MAX;
  13.     double losowa = fMin + f * (fMax - fMin);
  14.     return losowa;
  15. }
  16.  
  17.  
  18. // fill matrix with random numbers between fmin and fmax
  19. void fill_matrix(){
  20.     double matrix_a[n][n];
  21.     for (int i=0; i<n; i++){
  22.         for (int j=0; j<n; j++){
  23.             matrix_a[i][j] = 0;        
  24.         }
  25.     }
  26.    
  27.     int fMin = 1;
  28.     int fMax = 5;
  29.    
  30.     for (int i=0; i<n; i++){
  31.         for (int j=0; j<n; j++){
  32.             double losowa = get_random(fMin,fMax);
  33.             matrix_a[i][i] = 8*losowa;
  34.             try{
  35.                 losowa = get_random(fMin,fMax);
  36.                 matrix_a[i][i-1] = losowa;
  37.                
  38.                 losowa = get_random(fMin,fMax);
  39.                 matrix_a[i][i+1] = losowa;
  40.             }
  41.             catch(...){
  42.             }
  43.         }
  44.     }
  45. }
  46.  
  47. //print matrix
  48. void print_matrix(){
  49.     double matrix_a[n][n];
  50.     for (int i=0; i<n; i++){
  51.         for (int j=0; j<n; j++){
  52.             cout << matrix_a[i][j] << "\t\t";
  53.             if (j == (n-1) ){
  54.                 cout << endl;
  55.             }
  56.         }
  57.     }
  58. }
  59.  
  60.  
  61. //fill vector
  62. void fill_vector(){
  63.     double matrix_b[n];
  64.     int fMin = 10;
  65.     int fMax = 40;
  66.     for (int w=0; w<n; w++){
  67.             double losowa = get_random(fMin,fMax);
  68.             matrix_b[w] = losowa;
  69.     }
  70. }
  71.  
  72. //print vector
  73. void print_vector(){
  74.     double matrix_b[n];
  75.     for (int i=0; i<n; i++){
  76.         cout << matrix_b[i] << endl;
  77.     }
  78. }
  79.  
  80.  
  81. //solve system equation - SOR method
  82. void solve(){
  83.     double matrix_a [n][n];
  84.     double matrix_b [n];
  85.    
  86.     double SOR_param = 1.0;
  87.     double max_residuum = 0;
  88.     double r0 = 1E-2;
  89.     bool active_comp = true;   
  90.     int iter = 0;
  91.     int maxiter = 100;
  92.    
  93.     // initialize array
  94.    
  95.     double x_old[n] = {0};
  96.     double x[n] = {0};
  97.     double residuum[n] = {0};
  98.    
  99.     //fill x[n] with zeros
  100.     for (int k=0; k<=n; k++){
  101.         x[k] = 0;
  102.         x_old[k] = 0;
  103.         residuum[k] = 0;
  104.     }
  105.    
  106.     fill_matrix();
  107.     fill_vector();
  108.     cout << endl;
  109.     print_matrix();
  110.     cout << endl;
  111.     print_vector();
  112.     cout << endl;
  113.    
  114.         cout<< "Startowe" << endl;
  115.         cout << "x[" << 0 << "]: " << x[0] << endl;
  116.         for (int l =1;l<n;l++){
  117.             cout << "x[" << l << "]: " << x[l] << endl;}
  118.         cout << "x[" << n << "]: " << x[n] << endl;
  119.         cout <<"Koniec startowych" << endl;
  120.    
  121.     while (active_comp){
  122.  
  123.        
  124.         x[0] = ( matrix_b[0] - x[1] * matrix_a[0][1] )/matrix_a[0][0];
  125.         x[n] = ( matrix_b[n] - x[n-1] * matrix_a[n][n-1] )/matrix_a[n][n];
  126.  
  127.         //solve
  128.         for (int i=1; i<n; i++){
  129.             //for (int j=1; j<n-1; j++){
  130.                 iter += 1;
  131.                 //try{
  132.                     x[i] =  ( ( matrix_b[i] - x[i-1]*matrix_a[i][i-1] - x[i+1]*matrix_a[i][i+1] )/matrix_a[i][i] );// + ( (1.0 - SOR_param) * x_old[i] ) );
  133.                        
  134.                     /* 
  135.                     residuum[i] = abs((x[i] - x_old[i])/x[i]);
  136.                    
  137.                     if (residuum[i] > max_residuum){
  138.                         max_residuum = residuum[i];
  139.                     }
  140.                     }
  141.                     */
  142.                        
  143.                     if (iter == maxiter){
  144.                         cout << "End of computation,max iter, iter: " << iter << endl;
  145.                         active_comp = false;
  146.                     }
  147.  
  148.                     /*
  149.                     if (max_residuum < r0){
  150.                         cout << "End of computation, iter: " << iter << endl;
  151.                         active_comp = false;
  152.                     }
  153.                     */
  154.                    
  155.                     else{
  156.                         x_old[i] = x[i];
  157.                         //cout << "Iter: " << iter << endl;
  158.                     }
  159.                 //}
  160.                 //catch(...){}
  161.  
  162.            
  163.             //}
  164.         }
  165.     }
  166. }
  167.  
  168.  
  169. int print_solution(){
  170.     double x[n];
  171.     cout << "Sollution: ";
  172.     for (int i=1; i<n; i++){
  173.         cout << x[i] << " ";
  174.     }
  175. }
  176.    
  177.  
  178.  
  179. int main(){
  180.  
  181.     fill_matrix();
  182.     print_matrix();
  183.    
  184.     cout << endl << endl;
  185.    
  186.     fill_vector();
  187.     print_vector();
  188.    
  189.     cout <<endl<<endl; 
  190.    
  191.     srand (time( 0 )); 
  192.     solve();
  193.     print_solution();
  194.     cout << endl;
  195.    
  196.    
  197.     return 0;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment