Bug introduced in 9.0 and persisting through 11.0.1

I have noted that in Mathematica 9 my code, which involves a lot of calls to NDSolve, slows down considerably after some time.

Apparently, the problem is NDSolve itself and it seems to be related to Mathematica 9, as the following example shows:

RunNDSolve :=  Timing[Do[NDSolve[{f''[x] == -f[x], f[0] == 1, f'[0] == 0},
  {f[x]}, {x, 0, 1}], {1000}]][[1]]

RunNDSolve integrates a simple differential equation 1000 times using NDSolve and returns the time needed to carry out the integrations.

In Mathematica 9, RunNDSolve takes longer and longer times as it is called again, as the following example shows:

Table[{j, RunNDSolve}, {j, 50}]

In Mathematica 9 (extract of the full result):

j, RunNDSolve
1, 0.712
10, 2.649000
20, 4.869000
30, 7.326000
50, 13.372000

In Mathematica 8 (extract of the full result):

j, RunNDSolve
1, 0.5880000000000001`
10, 0.593`
20, 0.5719999999999992`
30, 0.5670000000000002`
40, 0.5770000000000017`
50, 0.5850000000000009`

Does anyone have an idea what the problem could be and how to fix it?

  • $\begingroup$ Using your code I can reproduce the increasing timing pattern in version 9, and the constant timings in versions 7 and 8. I haven't thought yet about why this might be though. $\endgroup$ – tom Aug 15 '13 at 10:32
  • $\begingroup$ I've run into similar issues with an earlier version of Mathematica, perhaps version 6. It showed up when solving very large systems of ODEs with NDSolve. $\endgroup$ – Pillsy Aug 15 '13 at 12:21
  • $\begingroup$ @Pillsy, what makes you believe it was the same cause? Could you send an example? $\endgroup$ – user21 Aug 15 '13 at 13:31
  • 1
    $\begingroup$ I have filed this as a bug. Thanks for bringing it up. $\endgroup$ – user21 Aug 15 '13 at 13:36
  • $\begingroup$ I have found the same problem on INTEL i7 and XEON machines. This needs to be fixed by Wolfram. gamma3142, what computer system did you use to obtain your Mathematica 8 results? Its seems to be very powerful. $\endgroup$ – user13754 Apr 16 '14 at 10:29

I also can confirm this (Windows7, Version 9 only), it looks like a nasty bug to me that seems to be related to using the second derivative. As a workaround you can do a classical reformulation (spoiling much of the advantages of using Mathematica, though) to avoid second derivatives:

RunNDSolve := Timing[Do[
  NDSolve[{f'[x] == fp[x], fp'[x] == -f[x], f[0] == 1, 
  fp[0] == 0}, {fp[x], f[x]}, {x, 0, 1}], 

which seems to not have the problem and gives the same result. Another workaround which will be more useful in practical applications seems to be to localize your dependent variables, e.g.:

RunNDSolve := Module[{f}, Timing[Do[
 NDSolve[{f''[x] == -f[x], f[0] == 1, f'[0] == 0}, {f[x]}, {x, 0, 
   1}], {1000}]][[1]]]

Block will work just as well, but be aware of the differences between the two. Actually I was very surprised that I haven't suffered from this problem as that's what I've everywhere in my codes but there I usually localize the dependent variables which seems to explain why I have never had problems with this...

It's probably worth noting that using the new names reserved for formal variables as e.g. \[FormalF] which could be used as an alternative for localized variables do -- not surprising -- also show the performance problem.

I suggest you send a bug report to wolfram support.

  • $\begingroup$ Hi Albert, I filed this as a bug. Thanks for the hints about possible causes. $\endgroup$ – user21 Aug 15 '13 at 13:37
  • 1
    $\begingroup$ @user21 Did you get any clarity in this matter? The problem appears to still exist as of V10.0.0.0? $\endgroup$ – C. E. Jul 21 '14 at 21:34
  • $\begingroup$ just tried it and it looks like the problem still exists in version 10 $\endgroup$ – Albert Retey Jul 29 '14 at 10:14
  • 1
    $\begingroup$ @AlbertRetey Problem persists for "10.4.1 for Microsoft Windows (64-bit) (April 11, 2016)". Timing grows more or less linearly. ClearSystemCache[] does not help. Neither does closing and reopening the notebook. Of course, restarting Mathematica does. $\endgroup$ – bbgodfrey May 3 '16 at 18:36
  • $\begingroup$ @bbgodfrey: that is sad to hear (with user21 having filed a bug I think WRI is aware of it). Have you tried the workarounds? I could imagine that they consider that to only be a minor problem when using localized variables solves it... $\endgroup$ – Albert Retey May 3 '16 at 18:42

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.