7
$\begingroup$

While analyzing a large system of ODE's, I defined a particular ratio p, which contains some variables that are represented by InterpolatingFunctions by NDSolve.

p = Sum[(c[i][t] + ac[i][t] + ct[i][t] + bmct[i][t] + 
 ambmct[i][t]) i, {i, 0, 6}]/(6*0.58)

To plot p, I put p with a transformation rule inside of a Plot[], like so:

Plot[p/.tsol,{t, 0, timeduration}]

where tsol stores NDSolve's output.

Plotting of p works fine and yields a nicely oscillating plot. How do I extract values of this new InterpolatingFunction p for certain time points t?

Ironically enough, I do manage to calculate the derivative of this function, along with the points where the slope is 0. This more trivial looking problem, however, is giving me headaches...

Thanks!!

$\endgroup$
2
  • $\begingroup$ I did manage to do this for just one InterpolatingFunction, like this (c[0] /. tsol)[1] for the variable c[0]. The problem with p is that it is a combination of InterpolatingFunctions and somehow this syntax won't work. $\endgroup$
    – Big_bodySmall_heart
    May 30, 2012 at 5:23
  • $\begingroup$ A small side note, you should use Plot[Evaluate[p/.tsol],{t,0,timeduration}]; this should give better performance. $\endgroup$
    – user21
    May 31, 2012 at 9:42

4 Answers 4

6
$\begingroup$

You can call an interpolating function like any other function. Here's an example with NDSolve:

sol = NDSolve[{y'[x] == -y[x], y[0] == 1}, y, {x, 0, 10}]

(* ==> {{y->InterpolatingFunction[{{0.,10.}},<>]}} *)

fun = y /. First[sol]

(* ==> InterpolatingFunction[{{0.,10.}},<>] *)

{fun[0], fun[1], fun[2]}

(* ==> {1., 0.367879, 0.135335} *)

It sounds like the difficulty for you was extracting the actual function from the rule list returned by NDSolve. My example above should help you with this.

$\endgroup$
3
  • $\begingroup$ Thanks! As I added in the comment below my question, I already succeeded in retrieving values for single InterpolationFunction(s). What I couldn't do was combining multiple ones into a new variable name/function (p) and then retrieve values for p. $\endgroup$
    – Big_bodySmall_heart
    May 30, 2012 at 15:28
  • $\begingroup$ @Big_bodySmall_heart Can you give a small but complete and working example which we can use to illustrate a solution? Your example is not complete (has lots of symbols that you didn't show a definition for). $\endgroup$
    – Szabolcs
    May 30, 2012 at 15:31
  • $\begingroup$ Extending the answer of Szabolcs, for multiple interpolating functions generated from NDsolve just replace y in the expression fun = y /. First[sol] by the interpolating function which you want to evaluate $\endgroup$
    – subhash k
    Jul 27, 2015 at 12:22
3
$\begingroup$

Thank you very much Szabolcs and Heike for taking some time with my question.

I just came up with this, before reading your answers, which is the most useful solution for the particular problem I'm trying to solve (very similar to Heike's solution in that it also defines a function):

p[sol_, parameters_, t_] := (Sum[(c[i][t] + ac[i][t] + ct[i][t] + 
bmct[i][t] + ambmct[i][t]) i, {i, 0, 6}] /. sol)/(6 cT /. parameters);

It's nice in that I can now ask check what it maps each value t in its domain to while still being able to calculate its derivative.

I'm pretty new to Mathematica, but the fact that there's always a sexy way to solve a problem is very appealing to me.

$\endgroup$
2
$\begingroup$

You could do something like this

fun = Sum[(c[i][#] + ac[i][#] + ct[i][#] + bmct[i][#] + ambmct[i][#]) i, 
  {i, 0, 6}]/(6*0.58) & /. tsol[[1]]

Then fun is a (pure) function, so you can evaluate it in the normal way.

$\endgroup$
0
$\begingroup$

I believe for InterpolatingFunction you may use InterpolatingFunctionValuesOnGrid from DifferentialEquations`InterpolatingFunctionAnatomy`

It is also shipped as this resource function.

$\endgroup$
1
  • $\begingroup$ I don't think this answers OP's question. Though OP uses the word "extract", (s)he isn't trying to obtain the raw data inside the InterpolatingFunction according to the rest part of the question. $\endgroup$
    – xzczd
    Mar 9, 2021 at 7:13

Your Answer

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

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