# Crash when dynamically updating ListLinePlot

I was trying to solve a wave problem using numerical methods. The code below crashes each time I run it on Windows with version 10.2.

data = {ConstantArray[0, 121]};
matA[n_, alpha_, dx_] :=
Block[{result},
result =
SparseArray[
{{i_, i_} -> alpha/dx,
{i_, j_} /; i - j == 1 -> (-1-alpha)/(2dx),
{i_, j_} /; j-i == 1 -> (1 - alpha)/(2dx)}, n + 1];
result[[n + 1, -2 ;; -1]] = {-1/dx, 1/dx};
result[[1, 1 ;; 2]] = {0, 0};
result]
bc1[t_] := 1 + 0.1 Sin[4t];
dpdt[n_, mat_, source_, alpha_, dx_, x_, t_] :=
Block[{rho = x},
rho[[1, 1]] = source[t];
-(matA[n, alpha, dx].(rho*{1 + 0.5 Cos[#]& /@
Table[dx*j, {j, 0, n }]}\[Transpose])) // N];
RK4ODEs[n_, init_, mat_, source_, alpha_, dx_, dt_, time_] :=
Fold[
Block[{s, coeff1 = {0, 0.5, 0.5, 1}, coeff2 ={1/6, 1/3, 1/3, 1/6}},
s = 0;
Do[
s[i] =
dpdt[n, mat, source, alpha, dx,
#1 + coeff1[[i]]*s[i - 1]*dt, #2 + coeff1[[i]]*dt],
{i,1,4}]//N;
data = #1 + (1/6 s + 1/3 s + 1/3 s + 1/6 s)*dt//N;
data[[1, 1]] = source[#2 + dt];
data2 = data;
data2]&,
init,
Table[dt*j, {j, 0, Floor[time/dt]}]]
Dynamic[
Module[{},
ListLinePlot[data2//Transpose, PlotRange->{All,All}]],
TrackedSymbols :> data2]
Dynamic[data2 // Transpose]
sol1 =
RK4ODEs[150, {ConstantArray[1, 151]}\[Transpose], matA, bc1, 0.0, 0.09, 0.025, 30];


I noticed that if I comment out the line that dynamically updates the ListLinePlot, then the kernel will not crash, which means the problem is probably due to dynamically updating ListLinePlot. I have also tried several methods in this question, i.e., adding TrackedSymbols:>{data2}, wrapping ListLinePlot with Module, but neither works. In fact, TrackedSymbols:>{data2} can sometimes force the evaluation to continue (but you can see that something is still wrong because many frames pink and the red error box is flashing around the plot), however, the kernel will crash eventually. Besides, the crash is rather random in terms of time of occurrence, i.e., sometimes it crashes as soon as I ran the code, and sometimes it crashes after 10 sec or so. The error message popped up was "Transpose[data2] is not a list of numbers or pairs of numbers. " and An unknown box name (ToBoxes) was sent as the BoxForm for the expression. Check the format rules for the expression.

I am also wondering if there is a better way of visualizing data2, which contains information of the points at each time step. Of course, I could use FoldList to collect the information of the points at all time steps and then use ListAnimate to generate the movie. However, the problem with this is that it consumes as much as 3GB RAM since it stores the information of all points at all time steps. Besides, I have to wait until RK4ODEs finishes evaluation before I could call ListAnimate, and ListAnimate also takes a lot of time to generate the movie.

P.S: I am required to implement RK4 myself, so I am not allowed to use NDSolve.

• As an alternative: are you aware that you can configure NDSolve[] to use the classical Runge-Kutta coefficients? Nov 12, 2015 at 5:48
• @J.M. I am required to implement RK4 by myself...... Nov 12, 2015 at 5:52
• Try this Dynamic[ If[ (* some test on data2 here *), ListLinePlot[data2 // Transpose, PlotRange -> {All, All}], (* else *) Spacer ], TrackedSymbols :> {data2}] Nov 12, 2015 at 5:55
• @MikeHoneychurch It still crashes.... Nov 12, 2015 at 6:05
• what test did you apply? Nov 12, 2015 at 6:07

matA[n, alpha, dx].(rho *Transpose[{1 + 0.5 Cos[#] & /@ Table[dx*j, {j, 0, n}]}])

The other notation for Transpose did not seem to work. Also, some of the frames during the dynamic pink but that might be a different issue. It certainly did not crash for me. (Perhaps it does for a different OS - then it were good to report this to the support of Wolfram Research) - Looks nice when it runs...
• I think that TrackedSymbols can sometimes suppress the error so that the evaluation can continue, but on my computer the kernel still will crash after some time. My thought is that perhaps data2 is updating so fast that ListLinePlot does not have enough time to draw them? BTW, what do you think is the reason for the pinked frame? Nov 12, 2015 at 14:00
• On 10.1 all of the frames are pink with the error being data2 is not a valid symbol for TrackedSymbols. It appears to work fine if you just omit tracked symbols all together. (see above comment by MikeH for correct TrackedSymbols syntax , {} required) Nov 12, 2015 at 19:23
• @george2079 Removing TrackedSymbols doesn't solve the problem, it still crashes each time I run it, and the difference is just how soon it will crash. BTW, I am just wondering why it crashes even without TrackedSymbols? Nov 13, 2015 at 1:43