I have an algorithm (second derivative) to solve poisson equaition. Why the commented algorithm don't work? I have tried very fine grid and small dt and still not work, so there must be something wrong with it
The commented algorithm is an attempt to solve the Poisson equation using a discretization of the second derivative, which is a common approach for this type of elliptic partial differential equation. The Poisson equation in two dimensions is given by:
∇²u(x,y) = f(x,y)
where ∇² is the Laplacian operator (the sum of second partial derivatives) and f(x,y) is a known function.
The commented algorithm seems to be using an incorrect discretization formula. Let's analyze the differences between the commented and uncommented parts to see what might be going wrong:
The commented algorithm uses fourth-order finite difference stencils for both x and y directions, which are given by:
The coefficients 16.0, -1, and -1 are typically used in fourth-order accurate discretizations for the second derivative. However, the denominator 30.0 does not match the expected denominator for combining these finite difference stencils. The denominator should be a sum of the coefficients multiplying the central points, which should be 30.0, but the correct formula should also divide by the square of the grid spacing (dx² or dy²), which is not reflected in the commented code.
The commented algorithm also includes a term - 12.0 * b[i][j] * (dx * dx * dy * dy). The factor 12.0 seems arbitrary and does not match the discretization of the Poisson equation. The correct discretization of the source term should be scaled by the area of the grid cell (dx * dy), but not multiplied by 12.0.
Now, let's look at the uncommented algorithm:
The uncommented algorithm uses a second-order central difference approximation for both x and y directions:
This is a standard and correct way to approximate the second derivative using a second-order accurate method. The denominator 2.0 * (dx * dx + dy * dy) seems to be an attempt to account for non-uniform grid spacing, but for a uniform grid, it should simply be 2.0 * (dx * dx) or 2.0 * (dy * dy), depending on whether you are approximating the second derivative in x or y direction.
To correct the commented algorithm, you should:
Use the proper coefficients for the fourth-order accurate finite difference scheme.
Make sure to divide by the square of the grid spacing for each direction.
Correct the scaling of the source term.
The corrected commented algorithm might look something like this:
Note that in this corrected version, I've assumed that b[i][j] already represents the source term f(x,y) scaled by the area of the grid cell (dx * dy), so no additional factors are needed. If b[i][j] is not scaled by the grid area, you would need to multiply it by dx * dy before adding it to the equation.