So, I've been attempting to design a simple solver for a problem of finding the gravitational potential of a system using Poisson's equation (let's call the potential phi, $\phi$). The goal is that I apply this potential to the system, move it forward one time step, and then repeat.
Right now, I am working with a periodic system (so assuming the system operates while being bound within a circle), and thus need to use periodic boundary conditions. Up to this point, I have been using this post to develop my method with Finite Differences.
Here's the issue. Let's say that the system is represented by a wave in the direct "center" of the circle, then the potential should by pseudo-linear from zero until the center where it drops off and returns pseudo-linearly to zero.

Now, if I was to shift this wave halfway across the circle and do this again, we should see the same potential also shifted halfway across the circle. In this case, the "center" would be 0, and pseudo-linearly towards the edges before tapering off into a peak. Instead, what I get is nearly the exact opposite, a non-zero flatline everywhere except the edges.

So, now the question is, how do I solve this so that it produces appropriate potential for a wave that sits on the "edges" of the circle? I have been coding this in Julia, so I will include the solution code below.
#M is the number of points, g is the waveform, which we get from |psi|^2
#minx = 0, maxx = 2pi
#dx is the step size, C is the FDM matrix
using SparseArrays, LinearAlgebra, LinearSolve, Plots, Printf, LaTeXStrings
#Define the FDM matrix
ctop = zeros(Float64, M-1)
cmid = zeros(Float64, M)
cbot = zeros(Float64, M-1)
@inbounds for i in 1:M-1
cmid[i] = -2/dx^2
ctop[i] = 1 /dx^2
cbot[i] = 1/dx^2
end
cmid[M] = -2/dx^2
C = spdiagm(-1=>cbot, 0=>cmid, 1=>ctop)
C[M,1] = 1/dx^2
#Set up g, the waveform, it should be noted that init_func produces a gaussian on the circle centered at pi of width sigma=1
psi= init_func(minx, maxx, M, [pi,0,1])
abspsi = abs.(psi).^2
g = zeros(Float64, M)
@inbounds for i in 1:M
g[i] = abs(psi[i])^2
#g[i] = -4 pi abspsi[i]
end
#Now solve then plot
phi = zeros(Float64, M)
phi = C\g