# Manipulation of InterpolatingFunction

Suppose that $q[t]$ is obtained by NDSolve as an InterpolatingFunction, and I want to define $Q[t]$ to be some function of $q[t]$, say $\sqrt{q[t]}$. How can I define it in such a way that I become able to plot its time derivative for example?

• If ifun is your interpolating function, you could for example write Sqrt[ifun[#]] &@t to get Q[t] and Sqrt[ifun[#]] &'@t to get Q'[t]. – C. E. Dec 9 '13 at 18:51
• Have a look at the package InterpolatingFunctionAnatomy. It has functions to extract datapoints from an InterpolatingFunction object. – István Zachar Dec 9 '13 at 18:58
• If I understood your new question, I believe you'll be better off in using G[p,Q] as a function of unevaluated p and Q, compute its partial derivatives wrt p and Q and only then substitute the InterpolatingFunctions for their values. – Peltio Dec 10 '13 at 15:41
• @Peltio The problem is that if I do that, even without taking the partial derivative, I can't take the time derivative as I can for G[p[t], Q[t]]. – Tarek Dec 10 '13 at 16:54
• Should I transfer the edit into a separate question? – Tarek Dec 11 '13 at 10:19

I might be grossly mistaken, but what is preventing you to compute a function of an interpolating function?

 NDSolve[{x''[t] + .2x'[t] + x[t] == 0, x == 1, x' == .4}, x[t], {t, 0, 10}]

   InterpolatingFunction[{{0,10}},<>][t]


Define your function in this way

f[t_] = x[t] /. %[];


Then you can compute functions of it, like it was another function

g[t_] = Sqrt[f[t]]

   Sqrt[ InterpolatingFunction[{{0,10}},<>][t] ]


Computing the derivative is using the composition rule

g'[t]

   InterpolatingFunction[{{0,10}},<>][t]/(2 Sqrt[InterpolatingFunction[{{0,10}},<>][t])


and like other functions you might have warnings when you try to plot imaginary values... So might be forced to use Abs or Re or Im.

Plot[{Abs[g[t]], Abs[g'[t]]}, {t, 0, 10}, Frame -> True]

• Hmmmmm... this is embarrassing :) Who would have thought of this... – István Zachar Dec 9 '13 at 21:30
• A long time ago, in a galaxy far, far away, Mathematica developers used to write articulated... articles in The Mathematica Journal about the new features introduced with new versions. But that was, as I've said, a long time ago. These days there is much less... articulation :-). Anyway, your approach shows an easy way to extract data points from interpolating functions. The OP will certainly appreciate it. – Peltio Dec 9 '13 at 21:46
• @Peltio Thanks Peltio. Please note the edit in the question. – Tarek Dec 10 '13 at 11:25
Needs@"DifferentialEquationsInterpolatingFunctionAnatomy";

if = y /. First@NDSolve[{y'[x] == y[x] Cos[x + y[x]], y == 1}, y, {x, 0, 30}]

dataY = InterpolatingFunctionValuesOnGrid@if;
dataX = Flatten@InterpolatingFunctionGrid@if;

{
ListPlot@Transpose@{dataX, dataY},
ListPlot@Transpose@{dataX, dataY^3}
}

 InterpolatingFunction[{{0., 30.}}, <>] • I have never hear about InterpolatingFunctionAnatomy +1! However there is simpler method: if["Coordinates"] and if["ValuesOnGrid"] make the job. A list of all possible options: PropertyList[if]. – ybeltukov Dec 10 '13 at 18:37