# Wave equation with piecewise initial condition

I am trying to solve numerically the wave equation with the following initial and boundary conditions:

NDSolve[{D[u[t, x], t, t] == D[u[t, x], x, x],
u[0, x] == 1 - Abs[x], (D[u[t, x], t] /. t -> 0 ) == 0,
u[t, 1] == 0, u[t, -1] == 0}, u, {t, 0, 2}, {x, -1, 1}]


This gives a warning

NDSolve::mxsst: Using maximum number of grid points 10000 allowed by the
MaxPoints or MinStepSize options for independent variable x. >>


How can I resolve this problem? Thank you!

• Have you searched what the problem means ? I hear Mathematica has a decent documentation. – Sektor Sep 9 '15 at 18:20
• Yes, they suggest not to use the option "MaxPoints"... which I am not using. – user33988 Sep 9 '15 at 18:22
• When we take out the initial condition $\partial_t u (0,x)=0$ then the code works. – user33988 Sep 9 '15 at 18:40

## 2 Answers

uif = NDSolveValue[{D[u[t, x], t, t] == D[u[t, x], x, x],
u[0, x] == 1 - Abs[x], (D[u[t, x], t] /. t -> 0) == 0,
u[t, 1] == 0, u[t, -1] == 0}, u, {t, 0, 2}, {x, -1, 1},
Method -> {"MethodOfLines",
"SpatialDiscretization" -> "FiniteElement"}];
Plot3D[uif[t, x], {t, 0, 2}, {x, -1, 1}] • The MethodOfLines always seemed me to have a low precision and to be awfully slow. Am I wrong? Is it the only way to solve time dependent problems? – Alexei Boulbitch Sep 10 '15 at 7:37
• What about icreasing accuracy/precision? (NDSolveValue::icfail: Unable to find initial conditions that ...) – mmal Sep 10 '15 at 8:00
• @AlexeiBoulbitch, since the spatial discretization of FEM is second order accurate there is not much point in a higher order time integration. But I believe MOL uses a higher order. This example though does not seem to be slow. If you can provide an example where you think it should be faster let me know. Sometime the DAE initialization can take some time. And, yes, for FEM MOL is the only way to time integrate. – user21 Sep 10 '15 at 9:45
• @mmal, it's always good to mention the input you used. I am not quite sure what you want to do. – user21 Sep 10 '15 at 9:47
• @AlexeiBoulbitch, what you can do though is specify a different time integrator: Method -> {"MethodOfLines", Method -> {"Adams"}, "SpatialDiscretization"->...} or "BDF" and I think that's documented somewhere. Probably here – user21 Sep 10 '15 at 10:17

As far as I can tell, NDSolve spits out the warning probably because the exact solution is not smooth while NDSolve assumes it to be smooth so too many grid points are used for getting a accurate enough solution around the cusp, in other words, NDSolve is trying too hard by default. As showed by user21, using FEM in v10 is a solution for this problem (because FEM is good at finding weak solution?), but if you're not yet in v10, you can just fix the space step size:

mol[n_, o_:"Pseudospectral"] := {"MethodOfLines",
"SpatialDiscretization" -> {"TensorProductGrid", "MaxPoints" -> n,
"MinPoints" -> n, "DifferenceOrder" -> o}}

{sol4, solp} =
Quiet@NDSolveValue[{D[u[t, x], t, t] == D[u[t, x], x, x],
u[0, x] == 1 - Abs[x], (D[u[t, x], t] /. t -> 0) == 0,
u[t, 1] == 0, u[t, -1] == 0}, u, {t, 0, 2}, {x, -1, 1},
Method -> mol[##]] & @@@ {{33, 4}, {33}}


Without Quiet, the generation of sol4 will be accompanied by some warnings complaining the solution isn't accurate enough, but it doesn't matter. Let's compare the solution with the analytic one:

half[t_, x_] =
Piecewise[{{(1 - Abs[x]), Abs@x > t && 0 <= t < 1}, {-(1 - Abs[x]),
2 - Abs@x < t && 1 <= t < 2}}, 1 - Abs@t];
(* The following is the analytic solution for one period,
I haven't yet figure out how to get the analytic solution with Mathematica*)
one[t_, x_] =
PiecewiseExpand@Piecewise[{{half[t, x], 0 < t < 2}, {-half[t - 2, x], 2 < t < 4}}];

Animate[Plot[{sol4[t, x] - one[t, x], solp[t, x] - one[t, x]}, {x, -1, 1},
PlotRange -> 0.02, Frame -> True, PlotLabel -> Style["Error", 14]], {t, 0, 2}]
` As you can see, the error is quite small.