0

I would like to see how the graph of the function en[B, i, j, k, p] changes depending on i, j, k, p in real time. In my case, the graph is not shown inside the loop at all.

How could it be done?

ClearAll["Global`*"]

en[B_, i_, j_, k_, p_] := B2(i + j/k) + Exp[-p*i]

For[i = 1, i < 5, i++, For[j = 1, j < 4, j++, For[k = 1, k < 3, k++, For[p = 1, p < 2, p++, TE[i_, j_, k_, p_] := Table[{B, en[B, i, j, k, p]}, {B, 0, 6, 1}]; Pause[0.2]; ListPlot[ Table[{TE[i, j, k, p][[l, 1]], TE[i, j, k, p][[l, 2]]}, {l, 1, 7}], PlotRange -> All]]]]]

xzczd
  • 65,995
  • 9
  • 163
  • 468
Mam Mam
  • 1,843
  • 2
  • 9
  • 3
    Why not use Manipulate? From the documentation for For "Unless an explicit Return is used, the value returned by For is Null." Try Print@ListPlot[...]. – Rohit Namjoshi Jun 10 '23 at 20:12
  • @Rohit Namjoshi, thanks for the comment! When using this construction Print@ListPlot[...], all graphs are displayed on the screen. I would like the graphs to change each other, i.e. the second graph changed the first graph on the screen, after which the third graph replaced the second one, and so on. I would like to have one card on the screen, the content of which changes depending on the changes in the i, j, k, p. – Mam Mam Jun 10 '23 at 20:37
  • 1
    Try to define plots= {0} and then Dynamic[plots][[-1]] before the loop. When you then run the For loop with the AppendTo in Rohit's answer, the line with Dynamic should update and show you the latest plot. Can you try it? I'm away from my Mathematica machine for a few days. – Marius Ladegård Meyer Jun 11 '23 at 19:54

2 Answers2

2

I won't be surprised if this is a duplicate, but it's easier for me to write an answer.

Solution using Dynamic:

Dynamic@plot

en[B_, i_, j_, k_, p_] := B 2 (i + j/k) + Exp[-p i]

plotlst = Table[Pause[0.2]; plot = ListPlot[Table[en[B, i, j, k, p], {B, 0, 6}], PlotRange -> All, DataRange -> {0, 6}, PlotLabel -> Row[{{"i, j, k, p"}, " = ", {i, j, k, p}}]], {i, 4}, {j, 3}, {k, 2}, {p, 1}];

Solution using Monitor:

en[B_, i_, j_, k_, p_] := B 2 (i + j/k) + Exp[-p i]

Monitor[ plotlst = Table[Pause[0.2]; plot = ListPlot[Table[en[B, i, j, k, p], {B, 0, 6}], PlotRange -> All, DataRange -> {0, 6}, PlotLabel -> Row[{{"i, j, k, p"}, " = ", {i, j, k, p}}]], {i, 4}, {j, 3}, {k, 2}, {p, 1}];, plot]

As to the reason why the graph is not shown inside the loop at all, please read this.

xzczd
  • 65,995
  • 9
  • 163
  • 468
  • Thanks a lot! It seems to be possible to use this Dynamic@plot construct with the loops from my code. – Mam Mam Jun 13 '23 at 17:37
  • @MamMam Please, at least avoid For…: https://mathematica.stackexchange.com/q/134609/1871 – xzczd Jun 13 '23 at 18:46
1

If you insist on using For then

en[B_, i_, j_, k_, p_] := B*2*(i + j/k) + Exp[-p*i]

plots = {}; For[i = 1, i < 5, i++, For[j = 1, j < 4, j++, For[k = 1, k < 3, k++, For[p = 1, p < 2, p++, TE[i_, j_, k_, p_] := Table[{B, en[B, i, j, k, p]}, {B, 0, 6, 1}]; AppendTo[plots, ListPlot[ Table[{TE[i, j, k, p][[l, 1]], TE[i, j, k, p][[l, 2]]}, {l, 1, 7}], PlotRange -> 75]]]]]]

ListAnimate[plots, 2]

Rohit Namjoshi
  • 10,212
  • 6
  • 16
  • 67
  • Thanks! In your code, the graphs are displayed on the screen after all the functions have already been calculated. Is it possible to make it so that as soon as the function is calculated, it is immediately displayed on the screen? After that, as soon as the next function is calculated, would it replace the previous one on the screen? – Mam Mam Jun 11 '23 at 08:59
  • Why do you need it to be plotted as soon as it is calculated? Isn't the end result the same? What is the real problem you are trying to solve? – Rohit Namjoshi Jun 12 '23 at 02:57
  • In real case each graph takes quite a long time, so I would like to watch each graph as it is calculated. – Mam Mam Jun 12 '23 at 03:36
  • 1
    Perhaps you can use Monitor. Example from the documentation values = {}; Monitor[NIntegrate[Sqrt[x (1 - x)], {x, 0, 1}, EvaluationMonitor :> (Pause[0.025]; AppendTo[values, x];)], Quiet@ListPlot[values]] – Rohit Namjoshi Jun 12 '23 at 14:53