I have the following code:

weq = NDSolve[{
  D[u[x, t], {t, 2}] == D[u[x, t], {x, 2}],
  u[0, t] == 0, u[2 Pi, t] == 0,
  u[x, 0] == Sin[x] + 1/3 Sin[5 x] + 1/9 Sin[25 x]},
  u, {x, 0, 2 Pi}, {t, 0, 2 Pi}, MaxStepFraction -> 1/500][[1, 1, 2]]
Plot[Sin[x] + 1/3 Sin[5 x] + 1/9 Sin[25 x], {x, 0, 2 Pi}]
Plot[weq[x, 0], {x, 0, 2 Pi}]

This produces the following output:

enter image description here enter image description here

These two plots should match, but NDSolve isn't sampling the boundary condition with a sufficiently high resolution for them to match. How can I tell NDSolve to sample the boundary condition with more points?


1 Answer 1


By adding Method->{FiniteElement,MeshOptions->MaxCellMeasure->0.01} to NDSolve, the initial condition (i.c.) will match, but are you sure you want to solve your equation under these constraints? To solve a wave equation numerically, we usually need to set it as an initial-boundary value problem (IBVP), in your case you need one more i.c. e.g. D[u[x, t], t]==0 /. t -> 0. Actually, NDSolve will spit out ivone in v9.0.1, because it can only deal with IBVP of wave equation at that time.

NDSolve gives a result in v10 or higher, because "FiniteElement" method is added since then and NDSolve will try to solve your problem as a boundary value problem (BVP). According to the Details of NeumannValue:

…not specifying a boundary condition at all is equivalent to specifying a Neumann 0 condition.

So when you only provide 3 conditions to solve the wave equation, NDSolve will think you've implicitly set the 4th b.c. as $\frac{\partial u(x,t)}{\partial t}\Bigl{|}_{t=2\pi}=0$ i.e. your code is equivalent to

weq = NDSolve[{D[u[x, t], {t, 2}] == D[u[x, t], {x, 2}] + NeumannValue[0, t == 2 Pi], 
    u[0, t] == 0, u[2 Pi, t] == 0, u[x, 0] == Sin[x] + 1/3 Sin[5 x] + 1/9 Sin[25 x]}, 
   u, {x, 0, 2 Pi}, {t, 0, 2 Pi},Method->FiniteElement][[1, 1, 2]]

NDSolve manages to find a solution under these conditions, but remember BVP for wave equation is a well known ill-posed problem. If it's not what you're trying to deal with, adding the missing i.c. and solve the problem as an IBVP is the correct way to go.

BTW, the MaxStepFraction -> 1/500 doesn't have any effect in your case, it's an option for adjusting time step.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.