3
$\begingroup$

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 Python Evolution Result

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Chris K Apr 8 at 19:58
4
$\begingroup$

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

Mathematica graphics

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

Mathematica graphics

| improve this answer | |
$\endgroup$
  • $\begingroup$ 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 $\endgroup$ – MavisMoss Apr 8 at 19:54
  • 1
    $\begingroup$ 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. $\endgroup$ – Chris K Apr 8 at 19:56
  • $\begingroup$ Ah OK that makes sense, the plotting algorithm used sounds interesting. Thanks for your comments $\endgroup$ – MavisMoss Apr 8 at 20:00
  • $\begingroup$ @ChrisK you can also do PlotRange->All to just show everything $\endgroup$ – b3m2a1 Apr 9 at 7:23
  • $\begingroup$ @b3m2a1 you’re right, I guess the zero is just my own default preference for plots $\endgroup$ – Chris K Apr 9 at 13:09
5
$\begingroup$

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

Mathematica graphics

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]

To add Manipulate:

enter image description here

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}

 ]
| improve this answer | |
$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – MavisMoss Apr 8 at 19:48
  • 1
    $\begingroup$ @MavisMoss you want to see the propagating wavefront? $\endgroup$ – CA Trevillian Apr 9 at 4:07
  • $\begingroup$ @CATrevillian, yes this is what I would like to see as I progress in the problem. Do you know how could I achieve this ? $\endgroup$ – MavisMoss Apr 9 at 7:46
  • 1
    $\begingroup$ @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. $\endgroup$ – CA Trevillian Apr 9 at 8:44
  • 1
    $\begingroup$ @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 $\endgroup$ – CA Trevillian Apr 9 at 8:46

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.