# Mathematica: How can I solve the problem "The Kernel Local has quit (exited) during the course of an evaluation"

I am using a Mac Book with Monterey and 16GB RAM for a calculation with 2 nested For loops. I am relatively new to Mathematika and still trying to learn the language properly, so I hope the problem is relatively small.

Edit: The Code

Liste2 = {};
For[\[Omega] = 0.3, \[Omega] <= 1, \[Omega],
{Liste = {},
\[Omega] = \[Omega] + 0.01,
\[Lambda] = 2*Pi/\[Omega],
{For[Nz = 0, Nz <= 150, Nz++,
{F[t_] :=
Piecewise[{{2*t/\[Lambda],
0 < t <= \[Lambda]/2}, {1, \[Lambda]/2 <
t <= \[Lambda]*(Nz + 1/2)}, {-2/\[Lambda]*t +
2*(Nz + 1), \[Lambda]*(Nz + 1/2) <
t <= \[Lambda]*(Nz + 1)}}, 0],
{xsol, ysol} =
NDSolveValue[{x'[t] == -Sin[\[Omega]*t]*Exp[2*I*t]*F[t]*y[t],
y'[t] == Sin[\[Omega]*t]*F[t]*Exp[-2*I*t]*x[t], x[0] == 0,
y[0] == 1}, {x, y}, {t, 0, 10000}],
fabs2[t_] := Re[xsol[t]*Conjugate[xsol[t]]],
Wert = fabs2[10000],
AppendTo[Liste, {Wert}],
(*Print[{\[Omega],Max[Liste]}],*)
max = Max[Liste]
}]
},
AppendTo[Liste2, {\[Omega], max}]}]
(*Print[Liste2]*)
ListLogLogPlot[Liste2, Joined -> True, PlotTheme -> "Scientific",
FrameLabel -> {Style["\[Omega] [Einheiten von m]", 20],
Style["max(|f(\!$$\*SubscriptBox[\(t$$, \
$$e$$]\))\!$$\*SuperscriptBox[\(|$$, $$2$$]\))", 20]},
ImageSize -> Large, PlotStyle -> {Purple}]

• Well, there could be a bazillion of reasons. Most likely: too high memory consumption in your code or some programming error in the backend (hence out of your reach) that leads to a segmentation fault. Anyways, seeing the code that causes this error might help to narrow this down. Jan 12 at 8:58
• I add the code now, maybe you can help me. Jan 12 at 10:33
• @StealthFrosch, you code is syntactically wrong. Coma is not a command separator in MMA. It separate the operands inside functions but each function should be finished by semicolon. Jan 12 at 11:11
• @Rom38 i change my syntax to For[[Omega] = 0.3, [Omega] <= 1, [Omega], {Liste = {};..... Jan 12 at 11:34
• I should have posted earlier: Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 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, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. Jan 13 at 22:21

Look at your syntax. You are using comma instead of semicolon at the end of line. It should be like this:

Liste = {};
\[Omega] = \[Omega] + 0.01;
\[Lambda] = 2*Pi/\[Omega];
...

• Okay, thanks for the tip, unfortunately it did not help. Jan 12 at 11:29
• More improvements: Remove the unnessesary brackets { and } at the begin and end of the For-Loops, also define F[t] outside the For-Loops at the beginning of your code. But also your code takes a huge amount of time to evaluate: at my brand-new Intel-i5 rougly 30 seconds for each Omega Loop, so probably more than half an hour at the end... Jan 12 at 12:00
• This really should be a comment. I guess that requires some minimum reputation level? If so, maybe a moderator can convert this. Jan 12 at 14:11
• @ChristophT The optimisations didn't help either, but what I noticed is that the code runs through /omega and Nz with fewer passes. Here, however, also with the non-optimised code. Jan 12 at 14:40
• I can reproduce the crash on 13.0.0 for Mac OS X ARM (64-bit) (December 3, 2021). Happens after 2 - 7 iterations of the omega loop. You should report this to Wolfram Support. Jan 12 at 14:53

Rather than attempt to find the error, I rewrote the code - still using For-loops - to avoid the kernel crash and also to reduce runtime a bit. The result is

Clear[F];
For[Liste2 = {}; ω = 0.3, ω <= 1, ω = ω + .01,
For[Liste = {}; λ = 2*Pi/ω; Nz = 0, Nz <= 150, Nz++,
F[t_?NumericQ] := Piecewise[{{2*t/λ, 0 < t <= λ/2}, {1, λ/2 < t <= λ*(Nz + 1/2)},
{-2/λ*t + 2*(Nz + 1), λ*(Nz + 1/2) < t <= λ*(Nz + 1)}}, 0];
Wert = NDSolveValue[{x'[t] == -Sin[ω*t]*Exp[2*I*t]*F[t]*y[t],
y'[t] == Sin[ω*t]*F[t]*Exp[-2*I*t]*x[t], x[0] == 0, y[0] == 1},
Abs[x[10000]]^2, {t, 0, 10000}];
AppendTo[Liste, Wert]];
AppendTo[Liste2, {ω, Max[Liste]}]]


Major changes were to replace F[t_] by F[t_?NumericQ], use standard For format, with instructions separated by semi-colons rather than included as Lists, and ω incremented as the third argument of the outer For. Also, only Abs[x[10000]]^2 is requested as output from NDSolveValue to save time, and some intermediate variables eliminated. The computation took about 50 minutes, using an average of three of my computer's six processors. The resulting plot, using the code in the question, is

Further investigation shows that using F[t_?NumericQ] is the key. With it, even the original code in the question works. It is not obvious to me why, without it, various solutions to the problem run awhile and then crash the kernel at seemingly random values of ω.
• @StealthFrosch F[t_?NumericQ] solves the remaining problem. Be sure to rune Clear[F]; as shown in my answer, if you are not starting from a fresh notebook. Jan 13 at 2:52