# Problem with a plot for 1D wave equation solution using NDSolve [closed]

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:

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;

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

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"}]


sol = NDSolveValue[{D[WaveEq[x, t], t, t] == 20*D[WaveEq[x, t], x, x],

• 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. – m_goldberg Dec 28 '15 at 14:27
• @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. – bbgodfrey Dec 28 '15 at 14:46
• 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. – m_goldberg Dec 28 '15 at 15:16