Skip to content

Instantly share code, notes, and snippets.

@siddhant3s
Forked from anonymous/gist:613839
Created October 6, 2010 19:53
Show Gist options
  • Save siddhant3s/613963 to your computer and use it in GitHub Desktop.
Save siddhant3s/613963 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
const int x_max=20 ,y_max=20;
const double y_max_v=1.0, mu=1.0;
double 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;}
}
int flag=1;
do{
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.000005)
V[i][j]=(V[i+1][j]+V[i-1][j]+V[i][j+1]+V[i][j-1])/4.0;
else
flag=0;
}while(flag!=0);
//computation done, show the result:
cout<<V[x_max/2][y_max/2];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment