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.

Let's assume that Sol is the solution of NDSolve, given as an InterpolatingFunction


If I want to plot it, for a fixed x, it is sufficient to write


Why, if I want to define the partial derivative of f, I cannot use the following code?

GradTheta[t] = D[f[x, t], x]

ReplaceAll::reps: {InterpolatingFunction[{{0.,10.},{0.,100.}},{3,10,1,{25,103},{6,4},0,0,0,0},<<1>>,{Developer`PackedArrayForm,{<<1>>},{<<1>>}},{Automatic,Automatic}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>
share|improve this question

closed as off-topic by bobthechemist, Michael E2, m_goldberg, rm -rf Feb 20 at 23:32

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – Michael E2, m_goldberg, rm -rf
If this question can be reworded to fit the rules in the help center, please edit the question.

Hard to tell based on the limited information you provided (i.e. it works for me), but your function definition is flawed: you have to use t_, not t. If you still have trouble after this, please make a complete minimal example that reproduces the problem. Try to keep it below 5 lines of code, which should be sufficient. –  Szabolcs Feb 20 at 16:34

1 Answer 1

up vote 3 down vote accepted

As mentioned in the comments, it's hard to know exactly what you are looking for. Taking an example from the documentation for NDSolve, here is an Interpolation function from NDSolve:

s = NDSolve[{D[u[t, x], t] == D[u[t, x], x, x], u[0, x] == 0, 
   u[t, 0] == Sin[t], u[t, 5] == 0}, u, {t, 0, 10}, {x, 0, 5}]
f = u /. First@s

To define a function that is the derivative with respect to x, we can do this:

g = Derivative[1, 0][f]

In your example, you don't specify at which point in x that this derivative is being taken, so I don't know if you want a general solution or not. I'll give a general solution.

Plot[Evaluate[g[x, t] /. {x -> { 0.25, 0.5, 0.75}}], {t, 0, 5}]

Mathematica graphics

Additionally, as mentioned in the comments, NDSolve can return the derivative as part of its solution:

s = NDSolve[{D[u[t, x], t] == D[u[t, x], x, x], u[0, x] == 0, 
   u[t, 0] == Sin[t], u[t, 5] == 0}, {Derivative[1, 0][u], u}, {t, 0, 
   10}, {x, 0, 5}]
share|improve this answer
I would use g = Derivative[1, 0][f] instead, to avoid recalculating the derivative every time g is evaluated. Mathematica is able to calculate the derivatives and integrals (Derviative[-1]) of interpolating functions and give a result in the form of another interpolating function. –  Szabolcs Feb 20 at 19:29
You can also ask NDSolve to return the derivative: s = NDSolve[{D[u[t, x], t] == D[u[t, x], x, x], u[0, x] == 0, u[t, 0] == Sin[t], u[t, 5] == 0}, {Derivative[1, 0][u], u}, {t, 0, 10}, {x, 0, 5}]. –  Michael E2 Feb 20 at 20:14
@Szabolcs point noted. Unless the OP needs further clarification, I don't think there's anything here that can't be found in the documentation. –  bobthechemist Feb 20 at 21:36

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