I love the following example of solving a 1D Poisson equation with DiffEqOperators.

However , I struggle to do the analog 3D example. Can somebody indicate me the right steps?

**The 1D formulation is:**

using DiffEqOperators, Plots

#Poisson Eq in 1D

#
`Δu = f`

with boundary conditions `u(0) = 0`

and `u(1) = 0`

Δx = 0.1

x = Δx:Δx:1-Δx # Solve only for the interior: the endpoints are known to be zero!

N = length(x)

f = sin.(2π*x)

Δ = CenteredDifference(2, 2, Δx, N)

bc = Dirichlet0BC(Float64)

u = (Δ*bc) \ f

u_analytic(x) = -sin(2π*x)/4(π^2)

plot(x, u)

plot!(x, u_analytic.(x))

**An equivalent 1D formulation, which works well, is:**

Δx = 0.1

x = Δx:Δx:1-Δx # Solve only for the interior: the endpoints are known to be zero!

N = length(x)

f = sin.(2π*x)
Δ = CenteredDifference(2, 2, Δx, N)
M = zeros(N,N)
for i in 1:N
for j in 1:N
if i==j M[i,j] = 1.0 end
end
end
Qx = Dirichlet0BC{1}(Float64, size(M))
MwBC = Qx*M

u_analytic(x) = -sin(2π*x)/4(π^2)
u = (Δ*MwBC) \ f

plot(x, u)

plot!(x, u_analytic.(x))

**In 3D, I could define Δ as:**

Δxx = CenteredDifference{1}(2,2,Δx,N)

Δyy = CenteredDifference{2}(2,2,Δx,N)

Δzz = CenteredDifference{3}(2,2,Δx,N)

Δ = Δxx + Δyy + Δzz

Then I could define a 3D matrix M and boundary conditions as:

M = zeros(N, N, N)

for i in 1:N

for j in 1:N

for k in 1:N

if i==j M[i, j, k] = 1.0 end

end

end

end

Qx = Dirichlet0BC{1}(Float64, size(M))

Qy = Dirichlet0BC{2}(Float64, size(M))

Qz = Dirichlet0BC{3}(Float64, size(M))

Q = compose(Qx, Qy, Qz)

MwBC = Q*M

Finally the Laplace operator would be placed in M

mult!(M, Δ, MwBC)

Is that correct ?

But then, what to do ?

Thanks in advance for any kind of answer …