In the absence of charge, the Poisson equation reduces to the Laplace equation .
The Laplace equation has the nice property that the solution is the average of the surrounding points. This is applicable for the case of one, two, and three dimensions. The reason for this is that the average of the potential on a sphere is given by where q is the charge inside the sphere. Since the region we are considering does not have any volume charge density, this is zero. Thus, the average field is just the field at the center of the sphere. The one dimensional case is not very interesting: it simply states that the function that satisfies the Laplace equation is linear, of the form ax + b. The two dimensional case is more interesting. Usually, the voltage at the boundary is specified, and this value can be used to calculate the value of the voltage everywhere. The program starts with the known values of voltage, and iteratively builds the voltage at the interior points. The averaging scheme used minimizes errors. Of course, I could simply have taken the values above, below, left and right, and that would’ve given the answer too (eventually), but this can be shown mathematically to be the one with least error. Note that this can also be used in the case where the charge density inside is non zero, but now the equation will have to be modified, as it will not be a simple average.
The problem that is considered here can be solved using separation of variables, so that an analytical solution exists. However, it can be used in cases where such methods are not that easily applied.
#include
int main(int argc, char** argv){
float pot[50][50];
float fc, fs;
int i, j, k;
for (i = 0; i < 50; i++) for (j = 0; j < 50; j++) pot[i][j] = 0; for (i = 0;i < 50; i++) { pot[0][i] = 5; pot[49][i] = 5; } for (k = 0; k < 10000; k++) for (j = 1; j < 49; j++) for (i = 1; i < 49; i++) { fc = 0.25 * (pot[i - 1][j] + pot[i + 1][j] + pot[i][j - 1] + pot[i][j + 1]); fs = 0.25 * (pot[i + 1][j + 1] + pot[i + 1][j - 1] + pot[i - 1][j - 1] + pot[i - 1][j + 1]); pot[i][j] = 0.8 * fc + 0.2 * fs; } for (i = 0; i < 50; i++) for (j = 0; j < 50; j++) printf("%d %d %f\n", i, j, pot[i][j]); return 0; } [/sourcecode]