# Implementing periodic boundary conditions in NDSolve [duplicate]

I am wanting to solve the KdV equation using NDSolve. The exercise asks that I impose periodic boundary conditions on u and its derivatives. My approach is as follows:

a = 30;

eq = {D[u[t, x], {t, 1}] + D[u[t, x], {x, 3}] ==
6 u[t, x] D[u[t, x], {x, 1}], u[0, x] == -2 Sech[x] ^2,
u[t, -a] == u[t, a]};

sol = NDSolve[eq, u, {t, 0, 30}, {x, -a, a}, MaxStepSize -> 0.07];

data = Flatten[Table[{t, x, u[t, x]} /. sol, {t, 0, 30}, {x, -a, a}],
2];

ListPlot3D[data, Mesh -> None, ColorFunction -> "Rainbow",
PlotRange -> {{0, 30}, {-30, 30}, {-2, 2}}, Lighting -> "Automatic",
AxesLabel -> {"t", "x"}]


I am not really sure what it means to impose periodic boundary conditions on the derivatives, but in the other posts I have seen on this site, periodic boundary conditions are what I have implemented above.

How would I implement periodic boundary conditions on the derivatives?

I have tried adding the code:

D[u[t, -a], {x, 1}] = D[u[t, a], {x, 1}],D[u[t, -a], {t, 1}] = D[u[t, a], {tx, 1}]


In the code above in the 'eq' part of the code, but there was an error.

Does the Boundary Condition:

u[t, -a] == u[t, a]


Also take care of the derivatives?

• Try this Derivative[0, 1][u][t, -a] == Derivative[0, 1][u][t, a] and this Derivative[1, 0][u][t, -a] == Derivative[1, 0][u][t, a] – zhk Aug 12 '17 at 15:27
• @zhk what do you mean? – Gr Eg Aug 12 '17 at 15:44
• If the solution is periodic with the period equal to the size of the domain one uses, it has either maximums, or minimums on the boundary of this domain. This means that not only u[t, -a] == u[t, a], but also its spatial derivative is zero Derivative[0, 1][u][t,a] == 0; Derivative[0, 1][u][t,-a] == 0. – Alexei Boulbitch Aug 14 '17 at 7:13

The code in the question is correct.

a = 30;
eq = {D[u[t, x], {t, 1}] + D[u[t, x], {x, 3}] == 6 u[t, x] D[u[t, x], {x, 1}],
u[0, x] == -2 Sech[x]^2, u[t, -a] == u[t, a]};
sol = Flatten@NDSolve[eq, u, {t, 0, 30}, {x, -a, a}, MaxStepSize -> 0.07]


and yields

Plot3D[u[t, x] /. sol, {x, -a, a}, {t, 0, 30}, PlotRange -> All, PlotPoints -> 100,
BoxRatios -> {2, 1, 1/2},, AxesLabel -> {x, t, u}], ImageSize -> Large,
LabelStyle -> Directive[12, Bold, Black]


Although the PDE certainly supports two-dimensional solutions, the result here is very nearly one-dimensional, which allows a highly accurate symbolic expression to be derived. Assume u[t, x] == v[y], where y == x - c t.

eq1d = Unevaluated[D[u[t, x], {t, 1}] + D[u[t, x], {x, 3}] ==
6 u[t, x] D[u[t, x], {x, 1}]] /. u[t, x] -> v[x - c t] /. x -> c t + y
(* -(c*v'y]) + v'''y] == 6*v[y]*v''y] *)


Now, insert the initial condition from the question.

Simplify[eq1d /. v[y] -> -2 Sech[y]^2 /. v'[y] -> D[-2 Sech[y]^2, y]
/. v'''[y] -> D[-2 Sech[y]^2, {y, 3}]]
(* (-4 + c) Sinh[y] == 0 *)


So, c == 4, and the symbolic solution is given by

u[t, x] == -2 Sech[x - 4 t]^2


for t < a/4, after which the soliton propagates beyond x == a and does not reenter at x == -a, because this expression for u[t, x] is not periodic in x. However, an approximate periodic solution is obtained easily by summing such solutions:

u[t, x] == -2 Sum[Sech[x - 4 (t - 15 n)]^2, {n, 0, Infinity}]
(* (-120 + QPolyGamma[1, -(1/60) Log[-I E^(4 t - x)], E^60] +
QPolyGamma[1, -(1/60) Log[I E^(4 t - x)], E^60])/1800 *)


Although this expression does not precisely satisfy the initial condition or even the PDE itself, the relative error is of order Exp[-2 a]. Indeed, plotting this last expression gives a figure indistinguishable from that plotted above. We have, therefor, both symbolic and numerical solutions, and they are essentially identical, as one would hope.

• FWIW, I have confirmation from the NDSolve develoeprs that when using classical integration methods, NDSolve should differentiate the BCs and automatically generate the ones for derivatives based on the ones for values. (This is not the case when using FEM. In that case you need to use PeriodicBoundaryCondition.) – Itai Seggev Aug 14 '17 at 19:33