# Bug in NDSolve (Mathematica 9.0.1)? [duplicate]

The following code performs many times the same identical operation, but the time it takes increases systematically. This seems to be a problem of Mathematica 9.0.1 and above. Mathematica 9.0.0 does not have this problem. Do you know how to fix this? Perhaps using the Method option?

Clear[delta];
delta := NDSolve[{y''[n] + y'[n] - y[n] == 0, y[-3] == 1.,y'[-3] == 1.}, y, {n, -3, 0}];
Do[{
time = Timing[Do[delta, {a, 1000}]];
Print[time]
}, {i, 10}]

• or duplicate of this not sure which one is better to link to. Jul 10, 2014 at 16:02

\$Version


"9.0 for Mac OS X x86 (64-bit) (January 24, 2013)"

Clear[delta];
delta := NDSolve[{
y''[n] + y'[n] - y[n] == 0,
y[-3] == 1., y'[-3] == 1.},
y, {n, -3, 0}];
Do[
Print[
Timing[
Table[
(y /. delta[[1]])[-2], {1000}] //
Union]],
{i, 10}]


{1.743167,{2.13832}}

{2.285117,{2.13832}}

{2.801171,{2.13832}}

{3.340376,{2.13832}}

{3.878951,{2.13832}}

{4.426765,{2.13832}}

{4.997985,{2.13832}}

{5.558735,{2.13832}}

{6.112288,{2.13832}}

{6.662651,{2.13832}}

Use Set rather than SetDelayed so that you only solve the equation once.

Clear[delta];
delta = NDSolve[{
y''[n] + y'[n] - y[n] == 0,
y[-3] == 1., y'[-3] == 1.},
y, {n, -3, 0}];
Do[
Print[
Timing[
Table[
(y /. delta[[1]])[-2], {1000}] //
Union]],
{i, 10}]


{0.007109,{2.13832}}

{0.007204,{2.13832}}

{0.007366,{2.13832}}

{0.007139,{2.13832}}

{0.006914,{2.13832}}

{0.007126,{2.13832}}

{0.007045,{2.13832}}

{0.007172,{2.13832}}

{0.007201,{2.13832}}

{0.007057,{2.13832}}