Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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!!

share

migrated from stackoverflow.com May 31 '12 at 3:19

This question came from our site for professional and enthusiast programmers.

    
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. –  Big_bodySmall_heart May 30 '12 at 5:23
    
A small side note, you should use Plot[Evaluate[p/.tsol],{t,0,timeduration}]; this should give better performance. –  user21 May 31 '12 at 9:42

3 Answers 3

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.

share
    
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. –  Big_bodySmall_heart May 30 '12 at 15:28
    
@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). –  Szabolcs May 30 '12 at 15:31

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.

share

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.

share

This site is currently not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .