Let's say I have a function which returns the resulting function of a differential equation solved by NDSolve:
solveDiffEq[a_] := {
sol = NDSolve[
{f''[t] == a*f[t], f[0] == 1, f'[0] == 1},
f,
{t, 0, 1}];
{AbsoluteTime[], f[t] /. sol[[1]]}
}
The time is included to check when the function is called. For example,
solveDiffEq[1][[1]] /. t -> 0.2
{3.644828015580337*10^9, 1.2214}
Now I display this output inside a manipulate as follows:
Manipulate[
solveDiffEq[a0][[1]] /. t -> t0
,
{t0, 0, 1},
{a0, 1, 2}
]
The problem is that this re-evaluates the solveDiffEq function both when a0 is changed or when t0 is changed (you can observe when the solveDiffEq function is called by observing the changing time).
Obviously, when the slider a0 is changed, the function should re-solve the diff eq. However, when t0 is changed, there is no need to resolve the diff eq. - only to evaluate the returned interpolating function at a different point. I would like for this to be the behavior.
There is probably a simple trick; I suspect this has something to do with
/. t-> t0
Thanks in advance!
Manipulate[ s = solveDiffEq[a0]; Manipulate[s /. t -> t0, {t0, 0, 1}] , {a0, 1, 2}]
$\endgroup$Manipulate[ s = solveDiffEq[a0]; Dynamic[s /. t -> t0], {t0, 0, 1}, {a0, 1, 2}]
$\endgroup$