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?
DifferentialRoot[]
:f = DifferentialRoot[Function[{y, x}, {y'[x] == x y[x], y[0] == 0}]]
and then look atf''
for example. This will only work if you have given initial conditions and your DEs are linear. $\endgroup$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 complicatedf[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. $\endgroup$