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_] := 
         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}],
     max = Max[Liste]
  AppendTo[Liste2, {\[Omega], max}]}]
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}]
  • 5
    $\begingroup$ 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. $\endgroup$ Jan 12, 2022 at 8:58
  • $\begingroup$ I add the code now, maybe you can help me. $\endgroup$ Jan 12, 2022 at 10:33
  • $\begingroup$ @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. $\endgroup$
    – Rom38
    Jan 12, 2022 at 11:11
  • $\begingroup$ @Rom38 i change my syntax to For[[Omega] = 0.3, [Omega] <= 1, [Omega], {Liste = {};..... $\endgroup$ Jan 12, 2022 at 11:34
  • $\begingroup$ 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. $\endgroup$
    – bbgodfrey
    Jan 13, 2022 at 22:21

2 Answers 2


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];
  • $\begingroup$ Okay, thanks for the tip, unfortunately it did not help. $\endgroup$ Jan 12, 2022 at 11:29
  • $\begingroup$ 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... $\endgroup$ Jan 12, 2022 at 12:00
  • $\begingroup$ This really should be a comment. I guess that requires some minimum reputation level? If so, maybe a moderator can convert this. $\endgroup$ Jan 12, 2022 at 14:11
  • $\begingroup$ @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. $\endgroup$ Jan 12, 2022 at 14:40
  • $\begingroup$ 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. $\endgroup$ Jan 12, 2022 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

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

enter image description here


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 ω.

  • $\begingroup$ Hello, first of all thank you for the effort, unfortunately it stopped with the same error again. Do you also use a MacBook? $\endgroup$ Jan 12, 2022 at 19:40
  • $\begingroup$ @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. $\endgroup$
    – bbgodfrey
    Jan 13, 2022 at 2:52
  • $\begingroup$ I can confirm that the above code works on a PC. I get the same results with a roughly similar runtime. $\endgroup$ Jan 13, 2022 at 12:12
  • $\begingroup$ Okay thank you guys $\endgroup$ Jan 13, 2022 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.