# Using the result of NDSolve to make animation

I have solved a system of PDEs and made density plots using the following script

  L = 5; del = 0.6; g = -0.1; a = 4; s = 0.1; tmax = 3;

ux = s (1 + 0.1   Exp[-(2.5)^2] - m[x, t]);
eqs = {2 s  k[x, t]  m[x, t] -  D[D[k[x, t], {x, 2}], t] +
2  s  del D[m[x, t], {x, 2}] ==
0, -D[m[x, t], t] +  D[m[x, t], {x, 2}] + g  m[x, t]^3 +
m[x, t]  (a - ux + del  D[k[x, t], t]) == 0}; bc = {k[0, t] == 0,
k[L, t] == 0, m[0, t] == 1 + 0.1  Exp[-(2.5)^2],
m[L, t] == 1 + 0.1  Exp[-(2.5)^2]}; ic = {m[x, 0] ==
1 + 0.1  Exp[-(x - 2.5)^2]};

sol = NDSolveValue[{eqs, ic, bc, k[x, 0] == 0}, {m[x, t],
k[x, t]}, {x, 0, L}, {t, 0, tmax},
Method -> {"MethodOfLines",
"SpatialDiscretization" -> {"TensorProductGrid",
"MaxPoints" -> 401, "MinPoints" -> 401,
"DifferenceOrder" -> 2}}];


Then I tried to make an animation out of it using Animate as follows

Animate[Plot[sol[[2]], {x, 0, L}, PlotRange -> All,
ImagePadding -> 20, PerformanceGoal -> "Quality"], {t, 0, tmax}]


However, this gives me a white plot without any graph. Could someone please help me how to solve this?

Edit: I have managed to plot this using the comments. But I realized I need to plot $$H$$ where $$\partial_x^2 H(x,t)=sol[[2]][x,t]$$ where the animation dynamics is given by $$t$$.

I tried to do this using

solH = NDSolveValue[{D[H[x, t], {x, 2}] == solb[[2]][x, t],
H[L, t] == 0, H[0, t] == 0}, {H}, {x, 0, L}, {t, 0, tmax},
Method -> {"MethodOfLines",
"SpatialDiscretization" -> {"TensorProductGrid",
"MaxPoints" -> 401, "MinPoints" -> 401,
"DifferenceOrder" -> 2}}];
Animate[Plot[solH[[1]][x, t], {x, 0, L}, PlotRange -> All,
ImagePadding -> 20, PerformanceGoal -> "Quality"], {t, 0, tmax}]


But I get an error that I don't understand:

NDSolveValue::ivone: Boundary values may only be specified for one independent variable. Initial values may only be specified at one value of the other independent variable.

• Please post the full code or another Minimal Working Example. Commented May 17 at 14:05
• @cvgmt added a code Commented May 17 at 14:18
• One way is Animate[Plot[sol[[2]] /. t -> s, {x, 0, L}, PlotRange -> All, ImagePadding -> 20, PerformanceGoal -> "Quality"], {s, 0, tmax}] or Animate[Plot[With[{t = t}, sol[[2]]], {x, 0, L}, PlotRange -> All, ImagePadding -> 20, PerformanceGoal -> "Quality"], {t, 0, tmax}], but I don't know the reasion. Commented May 17 at 14:30
• This explains the cause (localization) of the problem: mathematica.stackexchange.com/questions/9985/… . Note Animate constructs a special Manipulate. Commented May 17 at 14:38
• This fixes in this case is (1)remove variables in output of NDSolveValue: sol = NDSolveValue[{eqs, ic, bc, k[x, 0] == 0}, {m, k},...]; and (2) to put the variables in the plot: Animate[Plot[sol[[2]][x, t],...]. Commented May 17 at 14:41

Here is the fix from my comment, which moves the variables x and t from being global and external to Animate to be local and internal:

L = 5; del = 0.6; g = -0.1; a = 4; s = 0.1; tmax = 3;

ux = s  (1 + 0.1    Exp[-(2.5)^2] - m[x, t]);
eqs = {2  s   k[x, t]   m[x, t] - D[D[k[x, t], {x, 2}], t] +
2   s   del  D[m[x, t], {x, 2}] ==
0, -D[m[x, t], t] + D[m[x, t], {x, 2}] + g   m[x, t]^3 +
m[x, t]   (a - ux + del   D[k[x, t], t]) == 0}; bc = {k[0, t] ==
0, k[L, t] == 0, m[0, t] == 1 + 0.1   Exp[-(2.5)^2],
m[L, t] == 1 + 0.1   Exp[-(2.5)^2]}; ic = {m[x, 0] ==
1 + 0.1   Exp[-(x - 2.5)^2]};

sol = NDSolveValue[{eqs, ic, bc, k[x, 0] == 0}, {m, k}, {x, 0, L}, {t,
0, tmax},
Method -> {"MethodOfLines",
"SpatialDiscretization" -> {"TensorProductGrid",
"MaxPoints" -> 401, "MinPoints" -> 401, "DifferenceOrder" -> 2}}]

Animate[Plot[sol[[2]][x, t], {x, 0, L}, PlotRange -> {All, {0, 0.5}},
ImagePadding -> 20, PerformanceGoal -> "Quality"], {t, 0, tmax}]


I think the updated question about H[x, t] should probably be a second question. The issue with H seems to be an incompletely formulated PDE system. (NDSolve complains about boundary and initial conditions.) The issues with it seem to have nothing to do with Animate or plotting, at least until NDSolve is working. Possibly, the following is the answer (assuming solb is the same as sol, since no solb appears in the OP):

solH = Head@Integrate[sol[[2]][x, t], x, x];
solH = Function[{x, t}, Evaluate[solH[x, t] - solH[5, t] x/5]]

Animate[Plot[solH[x, t], {x, 0, L}, PlotRange -> {All, {-0.65, 0.02}},
ImagePadding -> 20, PerformanceGoal -> "Quality"], {t, 0, tmax}]

• Thanks. The solution to H seems to be incorrect as it is not symmetric about the center of the box Commented May 17 at 18:01
• @questionerno8 It occurred to me that if one just thinks about it for a minute, it's easy to adjust to the BCs. See updated answer. Commented May 17 at 21:30
• aw, nice! @goofy Commented May 17 at 23:27

The problem is the localization of t inside Manipulate. The "t" in sol is not the same "t" in Manipulate. Therefore Manipulate does NOT replace the "t" of sol by a number. You may sees this by:

fun := (Print[t]; sol[[2]]);
Manipulate[Plot[fun, {x, 0, L}], {t, 0, tmax}]


This prints "y" many times.

To fix this you may either inject the "t" from Manipulate into sol[[2]] using "With":

Animate[Plot[With[{t = t}, sol[[2]]], {x, 0, L}, PlotRange -> All,
ImagePadding -> 20, PerformanceGoal -> "Quality"], {t, 0, tmax}]


Or you may define a new function that will do the same:

fun[x_, t_] = sol[[2]];
Animate[Plot[fun[x, t], {x, 0, L}, PlotRange -> All,
ImagePadding -> 20, PerformanceGoal -> "Quality"], {t, 0, tmax}]