I am trying to solve a simple diffusion equation in Mathematica. The problem is inspired by spin diffusion, which is why I consider an initial condition that has both positive and negative density rho[x,t].
Here is a simple mathematica code that appears to work correctly
eq1 = D[rho[x, t], t] == Dif* D[rho[x, t], {x, 2}];
a = 3;
iv = {rho[x, 0] == x*Exp[-x^2], rho[-a, t] == 0, rho[a, t] == 0};
Dif = 0.1;
sl1 = NDSolve[{eq1, iv}, {rho[x, t]}, {x, -a, a}, {t, 0, 10}]
Plot3D[rho[x, t] /. sl1, {x, -a, a}, {t, 0, 10}, PlotRange -> {-0.5, 0.5}]
but it gives a warning because the initial condition is only approximately consistent with the boundary condition.
I thought I should do better, and tried to implement periodic boundary conditions. This also works
iv2 = {rho[x, 0] == x*Exp[-x^2], rho[-a, t] == rho[a, t],
Derivative[1, 0][rho][-a, t] == Derivative[1, 0][rho][a, t]};
sl2 = NDSolve[{eq1, iv2}, {rho[x, t]}, {x, -a, a}, {t, 0, 10}]
Plot3D[rho[x, t] /. sl2, {x, -a, a}, {t, 0, 10}, PlotRange -> {-0.5, 0.5}]
but it is still not quite right, because for my problem I should implement anti-periodic boundary conditions for rho[x,t], and periodic boundary conditions for the current D[rho[x,t],x]. So this should be the best solution
iv3 = {rho[x, 0] == x*Exp[-x^2], rho[-a, t] == -rho[a, t],
Derivative[1, 0][rho][-a, t] == Derivative[1, 0][rho][a, t]};
sl3 = NDSolve[{eq1, iv3}, {rho[x, t]}, {x, -a, a}, {t, 0, 10}]
except, it does not work, it produces an error
NDSolve::bcedge: "Boundary condition rho[-3,t]==-rho[3,t] is not
specified on a single edge of the boundary of the computational domain.
which I cannot make sense of. What is the problem here?
NDSolve
reference page by selecting theTutorials
icon and selecting the tutorial. I am, however, not sure this is mentioned in the tutorial. $\endgroup$