# Using Manipulate to plot a function's time evolution

I am trying to plot a function in Mathematica using Manipulate to check how a function evolves with time. I am able to get sensible results with Python, though in Mathematica there is an overshoot in the y axis as I evolve it with time using Manipulate.

My Mathematica code is as follows:

a = -1.0; b = 1.0; nnn = 100;
grid = Table[(i (b - a))/nnn, {i, 0, nnn}];

f[xx_, t_] = Exp[-2 (xx - t)^2]
Manipulate[ListPlot[{grid, f[grid, t]} // Transpose], {t, 0, 10}]



My Python code is:

nx = 100
xx = np.linspace(-1,1,nx)

def test(x,t):
y = np.exp(-2*(x-t)**2)

return y

for i in np.arange(0,1,0.1):
y = test(xx,i)
plt.plot(xx,y)


The latter gives

• BTW, Welcome to Mathematica.SE, Mavis! I suggest the following: 1) Take the tour and check the faqs. 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. – Chris K Apr 8 at 19:58

Not sure you know this, but if you use Plot you don't need to manually define the x-coordinates.

Plot[Evaluate[Table[f[x, t], {t, 0, 0.9, 0.1}]], {x, -1, 1}]


The Evaluate is only necessary to get the different colors for the different lines.

If you want to use Manipulate, you can force the y-axis to have the same height with PlotRange:

Manipulate[Plot[f[x, t], {x, -1, 1}, PlotRange -> {0, 1}], {t, 0, 10}]


• Thanks Chris, I am aiming towards a more complicated problem where I will need to use different variations of my grid, so this may complicate this direct implementation in 'Plot'. I have one more question do you know why my y values were being overshoot in my solution above ? Is it OK to just force as you mention in your answer? Am I losing any mathematical insight? Thanks for your time – MavisMoss Apr 8 at 19:54
• If I understand your question, I think it's because Plot and ListPlot try to zoom in on "interesting" parts of the curve by default. Giving an explicit PlotRange overrides that behavior. Unfortunately this relies on some trial-and-error as far as I know. – Chris K Apr 8 at 19:56
• Ah OK that makes sense, the plotting algorithm used sounds interesting. Thanks for your comments – MavisMoss Apr 8 at 20:00
• @ChrisK you can also do PlotRange->All to just show everything – b3m2a1 Apr 9 at 7:23
• @b3m2a1 youâ€™re right, I guess the zero is just my own default preference for plots – Chris K Apr 9 at 13:09

You got few things wrong in translating Python to Mathematica.

a = -1; b = 1; nnn = 100;
grid = Subdivide[a, b, nnn - 1];
f[xx_, t_] = Exp[-2 (xx - t)^2];
ListLinePlot[
Evaluate@Table[{grid, f[grid, t]} // Transpose, {t, 0, 0.9, 0.1}]]


First np.arange(0,1,0.1) gives array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]) and not to {t, 0, 10} and np.linspace(-1,1,nx) translates to Subdivide[-1, 1, nx - 1]

ClearAll[x,t,i,a,b];
a = -1; b = 1; nnn = 100;
grid = Subdivide[a, b, nnn - 1];
f[xx_, t_] = Exp[-2 (xx - t)^2];
Manipulate[
ListLinePlot[
Evaluate@Table[{grid, f[grid, i]} // Transpose, {i, 0, t, 0.1}]],
{{t, 0, "time"}, 0, 0.9, 0.1, Appearance -> "Labeled"},
TrackedSymbols :> {t}

]

• Thanks for your answer. Could we though produce a similar plot using Manipulate and just showing onecurve y, evolving in time instead of multiple curves? – MavisMoss Apr 8 at 19:48
• @MavisMoss you want to see the propagating wavefront? – CA Trevillian Apr 9 at 4:07
• @CATrevillian, yes this is what I would like to see as I progress in the problem. Do you know how could I achieve this ? – MavisMoss Apr 9 at 7:46
• @MavisMoss ah, this makes sense. It might be better in the future to specify this in your original question. As it is, @ChrisK has answered the question you posed. I would do the following: 1) Determine the maximum and minimum t-values you want to depict. 2) Go to the Manipulate portion of ChrisKâ€™s post and put these in for what are currently 0 and 10. 3) Determine your step size you want to depict and insert it after the 0 and 10 youâ€™ve replaced. 4) Change Manipulate to Table, wrap the whole thing in Export w/proper syntax & file format. This method has been detailed before on SE. – CA Trevillian Apr 9 at 8:44
• @MavisMoss if you require further assistance, Iâ€™d recommend asking another question, consulting the documentation, searching on SE for previous QAs, or some combination of those. I hope I helped you figure it out :D – CA Trevillian Apr 9 at 8:46