Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- int main() {
- const int x_max=20 ,y_max=20;
- const float y_max_v=1.0, mu=1.0;
- int counter=1;
- float V[x_max][y_max];
- for (int i=0; i<=y_max-1; i++) {
- V[0][i]=0.0;
- V[x_max-1][i]=1.0;
- }
- for (int i=0; i<=x_max-1;i++){
- V[i][0]=(y_max_v/(y_max-1))*i;
- V[i][y_max-1]=(y_max_v/(y_max-1))*i;
- }
- for(int i=1;i<=x_max-2;i++) {
- for(int j=1;j<=y_max-2;j++) {V[i][j]=mu;}
- }
- while(2>1){
- bool converged = false;
- int c_x, c_y;
- counter=counter+1;
- for(int i=1;i<=x_max-2;i++) {
- for(int j=1;j<=y_max-2;j++) {
- 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) {
- V[i][j]=(V[i+1][j]+V[i-1][j]+V[i][j+1]+V[i][j-1])/4.0;
- }
- else {
- converged=true; c_x=i; c_y=j;
- }
- //cout<<"The solution is "<<V[i][j]; break;}
- }
- }
- if(converged) { cout<<"The solution is "<<V[x_max/2][y_max/2]<<" and it took "<<counter<<" iterations to complete"<<endl;
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement