# NDSolve much slower when using InterpolatingFunctions inside

I am solving an ODE system with Mathematica. Let's say the system is of the form (to simplify).

sol = NDSolve[{x'[t] == f[t] x[t] + g[t], x[0] == 0}, x, {t, 0, 1}]


The functions f[t] and g[t] are obtained from a different program. I either use a simple fit for f[t] and g[t] (say polynomial) or define them as InterpolationFunctions from data tables I have, i.e.

f = Interpolation[data]


In the latter case (which is more desirable as more accurate), the system takes 10 times longer to solve!

In detail, I actually loop over NDSolve for several additional parameters, and my ODE is with complex coefficients and 6-dimensional. So the system is already kind of long to solve, and 10 times longer is killing me...

Anyone has any advice on how to speed this up? Interpolating functions should be super fast, shouldn't they?

-
To actually try to speed up your code people need to see it - what I am saying is that it is useful to post a minimum working example that illustrates the problem :) –  Sektor Sep 13 '13 at 23:18

## 1 Answer

The InterpolatingFunction[] calc time is highly dependent on the number of points you use:

f[t_] := 1 + t + t^2;
f1[d_] := Interpolation[Table[{t, f[t]}, {t, 0, 1, d}],  InterpolationOrder -> 2];
ListLinePlot@ Table[First@Timing[f1[d] /@ Range[0, 1, 10^-4]], {d, Array[10^-# &, 5]}]


-
Here is a working example illustrating the issue: Do[NDSolve[{x'[a] + Sqrt[a^3/(1. + a^3)] x[a] == 1, x[0] == 1}, x, {a, 0, 1}], {i, 1, 100}] // Timing Out[311]= {0.078638, Null} cube = Interpolation[Table[{(i - 1.)/999., ((i - 1.)/999.)^3}, {i, 1, 1000}]]; Do[NDSolve[{x'[a] + Sqrt[cube[a]/(1. + cube[a])] x[a] == 0, x[0] == 1}, x, {a, 0, 1}], {i, 1, 100}] // Timing Out[312]= {0.218771, Null} In this simple case it only takes a factor of 3 longer when using the interpolating function, but when having more than one ODE it gets worse. I use only 1000 points for interpolation Thanks! –  Yacine Sep 14 '13 at 15:05