8
$\begingroup$

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?

$\endgroup$
6
  • 1
    $\begingroup$ 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]. $\endgroup$
    – C. E.
    Dec 9, 2013 at 18:51
  • 1
    $\begingroup$ Have a look at the package InterpolatingFunctionAnatomy. It has functions to extract datapoints from an InterpolatingFunction object. $\endgroup$ Dec 9, 2013 at 18:58
  • $\begingroup$ 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. $\endgroup$
    – Peltio
    Dec 10, 2013 at 15:41
  • $\begingroup$ @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]]. $\endgroup$
    – Tarek
    Dec 10, 2013 at 16:54
  • $\begingroup$ Should I transfer the edit into a separate question? $\endgroup$
    – Tarek
    Dec 11, 2013 at 10:19

2 Answers 2

7
$\begingroup$

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[0] == 1, x'[0] == .4}, x[t], {t, 0, 10}]
   InterpolatingFunction[{{0,10}},<>][t]

Define your function in this way

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

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]
$\endgroup$
3
  • $\begingroup$ Hmmmmm... this is embarrassing :) Who would have thought of this... $\endgroup$ Dec 9, 2013 at 21:30
  • $\begingroup$ 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. $\endgroup$
    – Peltio
    Dec 9, 2013 at 21:46
  • $\begingroup$ @Peltio Thanks Peltio. Please note the edit in the question. $\endgroup$
    – Tarek
    Dec 10, 2013 at 11:25
6
$\begingroup$
Needs@"DifferentialEquations`InterpolatingFunctionAnatomy`";

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

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

{
 ListPlot@Transpose@{dataX, dataY},
 ListPlot@Transpose@{dataX, dataY^3}
 }
 InterpolatingFunction[{{0., 30.}}, <>]

Mathematica graphics

$\endgroup$
1
  • 1
    $\begingroup$ 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]. $\endgroup$
    – ybeltukov
    Dec 10, 2013 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.