4
$\begingroup$

I can't properly use Manipulate for my solution of a wave equation. Can anyone help me?

I have the wave equation in the form:

D[WaveEq[x, t], t, t] == 20*D[WaveEq[x, t], x, x]

Initial conditions are:

WaveEq[x, 0] == Sin[Pi*x]
Derivative[0, 1][WaveEq][x, 0] == 0

Boundary conditions are:

WaveEq[0, t] == 0
WaveEq[1, t] == 0

It is solved on the range {x,0,1} and {t,0,1}. The result using Plot3D looks like this: Plot3D solution of a wave equation

I then try to make a 2D plot with the values of WaveEq vs values of x. I want the plot to be manipulated with respect to time t. I get a blank result for this 2D plot.

Here is the full code:

 sol1 = NDSolve[{D[WaveEq[x, t], t, t] == 20*D[WaveEq[x, t], x, x],

   WaveEq[x, 0] == Sin[Pi*x],
   Derivative[0, 1][WaveEq][x, 0] == 0,
   WaveEq[0, t] == 0,
   WaveEq[WaveDistance, t] == 0},
   WaveEq[x, t], {x, 0, 1}, {t, 0, 1}]

   Plot3D[WaveEq[x, t] /. sol1, {x, 0, 1}, {t, 0, 1}, PlotPoints -> 25, 
   AxesLabel -> {x, t}]

   Manipulate[
   Plot[WaveEq[x, t] /. sol1, {x, 0, 1}, 
   PlotRange -> {{0, 1}, {-1, 1}}], {t, 0, 1, Appearance -> "Labeled"}]

One correction: the code must also have this line:

WaveDistance = 1;
$\endgroup$
  • 2
    $\begingroup$ Here is the full code when I copied and pasted the full code into my notebook, I get syntax errors. $\endgroup$ – Nasser Dec 27 '15 at 12:06
  • 1
    $\begingroup$ Sorry, I missed a line that assigns the value of 1 to WaveDistance that shows up in boundary conditions. Thanks. $\endgroup$ – space bobcat Dec 27 '15 at 17:49
4
$\begingroup$

The last line of the NDSolve function is missing from the question,

WaveEq[x, t], {x, 0, 1}, {t, 0, 1}]

With it included and WaveDistance set equal to 1 (to reproduce the Plot3D result), the Manipulate function can be made to work with a minor modification,

Manipulate[Plot[WaveEq[x, t] /. sol1 /. t -> t0, {x, 0, 1}, 
    PlotRange -> {{0, 1}, {-1, 1}}], {t0, 0, 1, Appearance -> "Labeled"}]

enter image description here

Addendum

Another approach is

sol = NDSolveValue[{D[WaveEq[x, t], t, t] == 20*D[WaveEq[x, t], x, x],
    WaveEq[x, 0] == Sin[Pi*x], Derivative[0, 1][WaveEq][x, 0] == 0, WaveEq[0, t] == 0, 
    WaveEq[1, t] == 0}, WaveEq, {x, 0, 1}, {t, 0, 1}];
Manipulate[Plot[s[t], {x, 0, 1}, PlotRange -> {{0, 1}, {-1, 1}}], 
    {t, 0, 1, Appearance -> "Labeled"}, Initialization :> (s[t_] := sol[x, t])]
| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you very much. This worked. $\endgroup$ – space bobcat Dec 27 '15 at 17:50
  • $\begingroup$ I suggest With[{waveEq = Head[sol[[1, 1, 2]]]}, Manipulate[ Plot[waveEq[x, t], {x, 0, 1}, PlotRange -> {{0, 1}, {-1, 1}}], {t, 0, 1, Appearance -> "Labeled"}]] as a simplified version of this answer. $\endgroup$ – m_goldberg Dec 28 '15 at 14:27
  • $\begingroup$ @m_goldberg I believe you mean either {waveEq = Head[sol1[[1, 1, 2]]]} or {waveEq = sol}. In any case very ingenious (+1). I had tried With[{t = t}, Plot ... inside Manipulate but without success. $\endgroup$ – bbgodfrey Dec 28 '15 at 14:46
  • 1
    $\begingroup$ From the point of view of your 1st answer it should be {waveEq = Head[sol1[[1, 1, 2]]]}. For your 2nd answer, I would simply write waveEq = NDSolveValue[...], and then Manipulate[Plot[waveEq[x, t], {x, 0, 1}, PlotRange -> {{0, 1}, {-1, 1}}], {t, 0, 1, Appearance -> "Labeled"}]. In this form the 2nd answer becomes very elegant. $\endgroup$ – m_goldberg Dec 28 '15 at 15:16

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