0
$\begingroup$

i am trying to use NDSolve on a set of coupled ODEs (roughly 200 equations). In each equation an interpolating function has to be evaluated many times at the same point respectively. So for example the equation would look like:

eqn={fun[t,.1] Sin[fun[t,.1]] Tanh^2[fun[t,.1]^5] a1'[t]+1/fun[t,.1] a2[t]+...=0,
fun[t,.2] Exp[fun[t,.2]] Cos[fun[t,.2]^5] a2'[t]+1/fun[t,.2]^3 a2[t]+...=0,
and so on...};

Solving for a1,a2,.. So solving this using NDSolve and an analytical expression for fun works fine. Using an interpolating function for fun is very slow, because I think Mathematica evaluates fun in each equation many times instead of just a single time as it would be necessary and using the result for the rest of the equation.

One could think of defining a new interpolating function for each prefactor but this seems a bit awkward and I hope there is a better solution.

Is there a way that Mathematica does not evaluate the same interpolating function in NDSolve at the same point multiple times? Thanks for any advice!

$\endgroup$
  • 4
    $\begingroup$ This should help: What does the construct f[x_] := f[x] = … mean? $\endgroup$ – Kuba Sep 7 '17 at 11:22
  • 1
    $\begingroup$ A sneaky way would be to define a new variable z'[t] == fun[t, .1], z[0] == 0 and use z'[t] instead of fun[t, .1]. (You could also use more directly z[t] == fun[t, .1], but you then have to use the IDA method and machine precision, which might be OK.) Conceivably this trick could affect the step size, too. You could probably diminish this effect by making z[0] comparatively large. But can't test these hypotheses without code. $\endgroup$ – Michael E2 Sep 7 '17 at 11:57
  • $\begingroup$ Kubas comment actually solved my problem. Its exactly what I wanted, thank you. $\endgroup$ – Mr Puh Sep 7 '17 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.