How to define a function - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-09-22T04:38:44Z https://mathematica.stackexchange.com/feeds/question/76100 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/76100 0 How to define a function ame_math https://mathematica.stackexchange.com/users/25351 2015-02-28T17:46:10Z 2015-04-01T01:16:39Z <p>How can I define a function which depends on a function of x? Something like this</p> <pre><code>f[u[x]_,u[x]']= u[x]' + a [x] u[x] +b[x] </code></pre> <p>I think I wasn't enaugh clear. If I define this function:</p> <pre><code>f[x_]=x^2 </code></pre> <p>and I put <code>x = a + b</code> I obtain:</p> <pre><code>f[a + b] // Expand </code></pre> <blockquote> <pre><code>a^2 + b^2 + 2ab </code></pre> </blockquote> <p>Now I want to define something similar for a function:</p> <pre><code>f[u[x]_, x] = D[u[x], x] + a[x]u[x] </code></pre> <p>And for I put this in input</p> <pre><code> f[z[x]u[x], x] </code></pre> <p>I want to obtain for the output</p> <blockquote> <pre><code>z[x]'u[x]+z[x]u[x]'+a[x]z[x]u[x] </code></pre> </blockquote> <p>Is it possible? Or I can only define the differential operator?:</p> <pre><code>diff1 = D[#, x] + a[x]# &amp; </code></pre> https://mathematica.stackexchange.com/questions/76100/-/76101#76101 0 Answer by TransferOrbit for How to define a function TransferOrbit https://mathematica.stackexchange.com/users/3236 2015-02-28T17:55:12Z 2015-02-28T17:55:12Z <p>On the assumption that you have defined the functions <code>u[x]</code>, <code>a[x]</code> and <code>b[x]</code> elsewhere, you can define a function as follows:</p> <pre><code>f[x_] := u'[x] + a[x] u[x] + b[x] </code></pre> <p>However, I recommend you read through the documentation on <a href="http://reference.wolfram.com/language/tutorial/DefiningFunctions.html" rel="nofollow">defining functions</a>.</p> https://mathematica.stackexchange.com/questions/76100/-/76139#76139 0 Answer by Mr.Wizard for How to define a function Mr.Wizard https://mathematica.stackexchange.com/users/121 2015-03-01T09:23:58Z 2015-03-01T09:23:58Z <p>I am still not sure of what you want but attempting to be helpful:</p> <pre><code>f[u[x] p_, x] := D[u[x] p, x] + a[x] u[x] p f[z[x] u[x], x] </code></pre> <blockquote> <pre><code>a[x] u[x] z[x] + z[x] Derivative[u][x] + u[x] Derivative[z][x] </code></pre> </blockquote> <p>Which formats as:</p> <p>\$a(x) u(x) z(x)+z(x) u'(x)+u(x) z'(x)\$</p> https://mathematica.stackexchange.com/questions/76100/-/76194#76194 1 Answer by djp for How to define a function djp https://mathematica.stackexchange.com/users/25325 2015-03-02T00:26:26Z 2015-03-02T00:26:26Z <p>This works:</p> <pre><code>f[u_, x_] := D[u, x] + a[x] u </code></pre> <p>By way of explanation, everything is an expression, and there is nothing particularly special about functions. You and I know that this definition doesn't have lot of meaning for objects "u" that aren't functions, but Mathematica doesn't need to know that u is a function.</p>