I am trying to plot the ListPlot3D but mathematica quits calculations without any warning or error.

Ein=E^-(x/σ)^2 E^-(y/σ)^2;
nn = 200; 
dz = 0.5; 
σ = 60;
dx = 2 σ/(2 nn + 1);
dy = 2 σ/(2 nn + 1);
dqx = 6 2 π/p/(2 nn + 1); 
dqy = 6 2 π/p/(2 nn + 1);
dn0 = 0.001;
dn[j_, h_] = dn0 (Sin[2 π/p dx j] Sin[2 π/p dy h]);

Eq[l_, o_] =  dy dx Sum[Sum[(E^-(dx j/σ)^2 E^-(dy h/σ)^2 
E^(-I dqx l dx j)
E^(-I dqy o dy h)), {j, -nn, nn}], {h, -nn, nn}];

ListPlot3D[Table[Re[Eq[l, o]], {l, -nn, nn}, {o, -nn, nn}], 
PlotRange -> All]

The nn should be 200 for calculation purposes and if I reduce it to such as nn=15 it works fine. I wonder whether it is memory issue or something else?

  • $\begingroup$ The definition Eq[l_, o_] = = ... is wrong I think, remove one =. dqx,dqy aren't defined. $\endgroup$ – Ulrich Neumann Jul 15 '19 at 12:40
  • $\begingroup$ @UlrichNeumann it was a typo while copying the code here. $\endgroup$ – Muhammad Ali Jul 15 '19 at 12:43
  • 1
    $\begingroup$ What about dqx,dqy? $\endgroup$ – Ulrich Neumann Jul 15 '19 at 12:50
  • $\begingroup$ @UlrichNeumann Now they are there. $\endgroup$ – Muhammad Ali Jul 15 '19 at 13:25
  • 1
    $\begingroup$ Please add cross-links between this and the corresponding Wolfram Community post. $\endgroup$ – Daniel Lichtblau Jul 15 '19 at 15:56

One of the most common mistakes I see new Mathematica users make is using symbolic calculations when they are not really needed. This makes it all orders of magnitude slower and takes orders of magnitude more memory than it should.

I suppose that what actually happened is that Mathematica kernel exhausted the system RAM and quit (or maybe crashed). It might have beeped in the process (even if you didn't hear it), so try looking into menu item HelpWhy the Beep? for some bit of explanation.

Now how to make it work. If you intend your calculation to be numeric, you should remember that in Wolfram language, there's a difference between 200 and 200.0. Namely, the former is exact integer, while the latter is machine-precision number. Any arithmetic operation on exact numbers yields exact result: e.g. 200/15 will yield 40/3 (the exact fraction), while with machine arithmetic, 200.0/15 will give you 13.3333333333333, which is a machine-precision number.

So, the first thing you should do is change your numbers like nn=200 and σ=60 to machine-precision values like nn=200.0 and σ=60.0. The expression for Eq[l,o] will become much smaller (because things like Exp[exactSymbolicExpression] will reduce to a machine-precision number) and, when supplied with actual numbers, will evaluate to a machine-precision value.

Your second problem is that your calculation has the complexity $O(\operatorname{nn}^4)$. Coupled with a large value of nn this will make you wait very long even in numerical calculations mode.

To make the calculation of the plot take more reasonable times, try generating the table with some step larger than 1 — e.g. 10. The following command takes about 10 minutes on my machine instead of a dozen hours I'd get with your variant:

ListPlot3D[Table[Re[Eq[l, o]], {l, -nn, nn, 10}, {o, -nn, nn, 10}], PlotRange -> All]

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.