Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.  
  8.         const int x_max=20 ,y_max=20;
  9.         const float y_max_v=1.0, mu=1.0;
  10.         int counter=1;
  11.        
  12.  
  13.         float V[x_max][y_max];
  14.        
  15.         for (int i=0; i<=y_max-1; i++) {
  16.                         V[0][i]=0.0;
  17.                         V[x_max-1][i]=1.0;
  18.                                    }
  19.                                    
  20.         for (int i=0; i<=x_max-1;i++){
  21.                         V[i][0]=(y_max_v/(y_max-1))*i;
  22.                         V[i][y_max-1]=(y_max_v/(y_max-1))*i;
  23.                                   }
  24.    
  25.        
  26.        
  27.         for(int i=1;i<=x_max-2;i++) {
  28.                             for(int j=1;j<=y_max-2;j++) {V[i][j]=mu;}
  29.                                      }
  30.                                      
  31.                                      
  32.        
  33.         while(2>1){
  34.        
  35.         bool converged = false;
  36.         int c_x, c_y;
  37.         counter=counter+1;
  38.        
  39.        
  40.         for(int i=1;i<=x_max-2;i++) {
  41.        
  42.                            
  43.                            
  44.                             for(int j=1;j<=y_max-2;j++) {
  45.                                                     if(fabs(((V[i+1][j]+V[i-1][j]+V[i][j+1]+V[i][j-1])/4.0)-V[i][j])>=0.000001) {
  46.                                                    
  47.                                                                                              V[i][j]=(V[i+1][j]+V[i-1][j]+V[i][j+1]+V[i][j-1])/4.0;
  48.                                                                                              
  49.                                                                                              
  50.                                                                                                                            }
  51.                                                     else {
  52.                                                             converged=true; c_x=i; c_y=j;
  53.                                                           }
  54.                                                    
  55.                                                            //cout<<"The solution is "<<V[i][j]; break;}
  56.                                                
  57.                                                          }
  58.                            
  59.                                      }
  60.                                      
  61.                             if(converged) { cout<<"The solution is "<<V[x_max/2][y_max/2]<<" and it took "<<counter<<" iterations to complete"<<endl;
  62.                                             break;
  63.                                            }
  64.                     }                
  65.            
  66.             return 0;
  67.            
  68.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement