# Mathematica9: NDSolve slows down after repeated calls

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?

-
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. – tom Aug 15 '13 at 10:32
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. – Pillsy Aug 15 '13 at 12:21
@Pillsy, what makes you believe it was the same cause? Could you send an example? – user21 Aug 15 '13 at 13:31
I have filed this as a bug. Thanks for bringing it up. – user21 Aug 15 '13 at 13:36
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. – 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}],
{1000}]][[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.

-
Hi Albert, I filed this as a bug. Thanks for the hints about possible causes. – user21 Aug 15 '13 at 13:37
@user21 Did you get any clarity in this matter? The problem appears to still exist as of V10.0.0.0? – Pickett Jul 21 '14 at 21:34
just tried it and it looks like the problem still exists in version 10 – Albert Retey Jul 29 '14 at 10:14