I want to solve a PDE as:

$\frac{\partial u}{\partial t}=\frac{\partial u}{\partial x}$, with conditions: $u(x,0)=sin(\pi cos(x))$ and $u(x+2\pi,t)=u(x,t)$. I have solved it by hand and obtained: $u(x,t)=sin(\pi cos(x+t))$. I would like to solve it both numerically and analytically by Mathematica. I have written:

NDSolve[{D[u[t, x], t] == D[u[t, x], x], u[x, 0] == Sin[Pi Cos[x]], 
  u[x, t] == u[x + 2 Pi, t]}, u, {t, 0, 10}, {x, 0, 5}].

What's the problem?

  • $\begingroup$ You can get Mathematica to solve this analytically by evaluating DSolve[{D[u[x, t], t] == D[u[x, t], x], u[x, 0] == Sin[\[Pi] Cos[x]]}, u, {x, t}], which gives {{u -> Function[{x, t}, Sin[\[Pi] Cos[t + x]]]}} in version 10.3. $\endgroup$ – Stephen Luttrell Oct 22 '15 at 9:40

You had a simple mistake - getting the order of the t and x arguments mixed up in a couple of places, giving you the error

NDSolve::conarg: The arguments should be ordered consistently.

But then you should also replace the equation u[x, t] == u[x + 2 Pi, t] with u[t, 0] == u[t, 2 Pi] and it works just fine

sol = NDSolve[{D[u[t, x], t] == D[u[t, x], x], 
    u[0, x] == Sin[Pi Cos[x]], u[t, 0] == u[t, 2 Pi]}, 
   u, {t, 0, 10}, {x, 0, 5}][[1]]

enter image description here

And you can compare the numerical and analytic solutions

Grid[{{DensityPlot[u[t, x] /. sol, {t, 0, 10}, {x, 0, 5}, 
    PlotPoints -> 100, ImageSize -> 400], 
   DensityPlot[Sin[π Cos[x + t]], {t, 0, 10}, {x, 0, 5}, 
    PlotPoints -> 100, ImageSize -> 400]}}]

enter image description here

  • $\begingroup$ Thank you very much! What does [[1]] in your code? $\endgroup$ – Dave Oct 22 '15 at 8:14
  • 1
    $\begingroup$ The NDSolve returns a doubly nested list for some reason, {{u -> ...}} ans so when you type u[3,3] /. sol it returns {0.125417}, which seems ugly to me. [[1]]selects the first element of the list, so if I include it int he definition of sol then u[3, 3] /. sol just returns 0.125417. The plotting seems to work just fine without it. $\endgroup$ – Jason B. Oct 22 '15 at 8:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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