# Rule for derivatives

I have a differential equation of the form

$$f'(x)=g(x,f(x))$$

(where $g$ is has a known explicit form, e.g. $x f(x)$) and I have an expression which contains several derivatives of $f$. I want to use the differential equation to substitute all the derivatives in order to leave the expression as a function of $f(x)$ and $x$ only. For this purpose I have written the following rule:

ruleDerF = Derivative[n_][f][x_] -> D[x f[x], {x, n - 1}];


This works ok, e.g.,

f''[x] /. ruleDerF

(*f[x] + x f'[x]*)


and

f''[x] //. ruleDerF

(*f[x] + x^2 f[x]*)


The problem appears when I want to apply this rule when the function is evaluated for a value of $x$. If I try naively to apply this rule on $f(1)$ it gives an obvious error:

f''[1] /. ruleDerF

General::ivar: 1 is not a valid variable. >>

(* \!$$\*SubscriptBox[\(\[PartialD]$$, $${1, 1}$$]$$f[1]$$\) *)


This is obvious because you cannot derive wrt 1. So I tried to redefine the rule in the following way,

ruleDerF = Derivative[n_][f][x_] -> (D[y f[y], {y, n - 1}]/.y:>x);


believing that the delayed rule would cause to first perform the derivation wrt the symbolic variable y and then evaluate it to the introduced value, but it doesn't work this way (I get the same result as before).

Any idea that could help me?

• Perhaps this should be marked as a duplicate of (22917) or one of the many questions it is linked to? – Mr.Wizard Jul 21 '16 at 11:17
• You might be interested in DifferentialRoot[]: f = DifferentialRoot[Function[{y, x}, {y'[x] == x y[x], y[0] == 0}]] and then look at f'' for example. This will only work if you have given initial conditions and your DEs are linear. – J. M. is away Jul 21 '16 at 11:58
• Are you trying to prevent the derivative D[y f[y], {y,n-1}] from being carried out every time the rule is applied? One can imagine applying this rule thousands of times for a fairly complicated f[x], and wishing for the derivative to only be carried out once, and stored thereafter. This is how I interpreted in intent behind -> rather than :>, but perhaps it's simply a mistake that I'm overthinking. – jjc385 Aug 6 '16 at 1:56

## 1 Answer

Define your rule as a function:

ruleDerF = (# /. Module[{x},
Derivative[n_][f][var_] :> (D[x f[x], {x, n - 1}] /. x -> var)
])&

f''[x] // ruleDerF

f[x] + x Derivative[1][f][x]

 FixedPoint[ruleDerF, f''[x]]

f[x] + x^2 f[x]

FixedPoint[ruleDerF, f''[1]]

2 f[1]


(havent tested heavily)

• Shouldn't you localize x? And wouldn't mentioning RuleDelayed be useful to the OP? – Mr.Wizard Jul 21 '16 at 11:04
• Maybe I'm crazy but at first blush it looks like he is using :> and -> backward, or at least haphazardly. Certainly you changed -> to :> following Derivative[n_][f][x_]. – Mr.Wizard Jul 21 '16 at 11:08
• Trying to understand why this solution works I realized that defining the rule as Derivative[n_][f][x_] :> (D[y f[y], {y, n - 1}]/.y->x) does the job. The only thing left, as mentioned, is to localize the dummy variable y. – dpravos Jul 21 '16 at 11:11
• Finally I have opted for defining the rule as Derivative[n_][f][var_] :> Module[{x}, (D[x f[x], {x, n - 1}] /. x -> var)]. I will mark the answer as right because it's equivalent. Thank you for your insights. – dpravos Jul 21 '16 at 11:15
• @DavidPravos no need to accept so quickly, I will add more explanations later. Some things are needed and some may be confusing, like using FixedPoint, which isn't necessary. – Kuba Jul 21 '16 at 11:17