Rule for derivatives - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-08-19T09:12:34Z https://mathematica.stackexchange.com/feeds/question/121171 http://www.creativecommons.org/licenses/by-sa/3.0/rdf https://mathematica.stackexchange.com/q/121171 3 Rule for derivatives dpravos https://mathematica.stackexchange.com/users/30287 2016-07-21T10:48:21Z 2016-07-21T11:55:31Z <p>I have a differential equation of the form</p> <p>\$\$f'(x)=g(x,f(x))\$\$</p> <p>(where \$g\$ is has a known explicit form, <em>e.g.</em> \$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:</p> <pre><code>ruleDerF = Derivative[n_][f][x_] -&gt; D[x f[x], {x, n - 1}]; </code></pre> <p>This works ok, e.g.,</p> <pre><code>f''[x] /. ruleDerF (*f[x] + x f'[x]*) </code></pre> <p>and</p> <pre><code>f''[x] //. ruleDerF (*f[x] + x^2 f[x]*) </code></pre> <p>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:</p> <pre><code>f'' /. ruleDerF General::ivar: 1 is not a valid variable. &gt;&gt; (* \!\(\*SubscriptBox[\(\[PartialD]\), \({1, 1}\)]\(f\)\) *) </code></pre> <p>This is obvious because you cannot derive wrt 1. So I tried to redefine the rule in the following way, </p> <pre><code>ruleDerF = Derivative[n_][f][x_] -&gt; (D[y f[y], {y, n - 1}]/.y:&gt;x); </code></pre> <p>believing that the delayed rule would cause to first perform the derivation wrt the symbolic variable <code>y</code> and then evaluate it to the introduced value, but it doesn't work this way (I get the same result as before).</p> <p>Any idea that could help me?</p> https://mathematica.stackexchange.com/questions/121171/-/121172#121172 2 Answer by Kuba for Rule for derivatives Kuba https://mathematica.stackexchange.com/users/5478 2016-07-21T11:02:07Z 2016-07-21T11:08:24Z <p>Define your rule as a function:</p> <pre><code>ruleDerF = (# /. Module[{x}, Derivative[n_][f][var_] :&gt; (D[x f[x], {x, n - 1}] /. x -&gt; var) ])&amp; f''[x] // ruleDerF </code></pre> <blockquote> <pre><code>f[x] + x Derivative[f][x] </code></pre> </blockquote> <pre><code> FixedPoint[ruleDerF, f''[x]] </code></pre> <blockquote> <pre><code>f[x] + x^2 f[x] </code></pre> </blockquote> <pre><code>FixedPoint[ruleDerF, f''] </code></pre> <blockquote> <pre><code>2 f </code></pre> </blockquote> <p>(<em>havent tested heavily</em>)</p>