6
$\begingroup$

Let's assume I have a function $f(x)=\exp(10x^2)$, i.e. f[x_]:= Exp[10*x^2]. When I differentiate $f$ by evaluating the expression D[f[x],x], I get the result:

20 E^(10 x^2) x

which is OK, but I would like to see

20 f[x] x

instead.

Is there any way to force Mathematica to do this substitution?

I do not necessarily expect Mathematica to output this right away, but at least is there any command to replace expressions with predefined functions where possible?

This is just a simple example, but clearly you get the point. If then one works with much more complicated expressions, this can save a lot of space and supports nice analytical expressions.

$\endgroup$
0

5 Answers 5

5
$\begingroup$
f[x_]:= Exp[10*x^2]

D[f[x], x] /. f[x] :> HoldForm@f[x]

enter image description here

Update

B[t_, T_] := 1/a*(1 - E^(-a*(T - t)));

A[t_, T_] := 
 1/a^2*(a*b - 1/2*sigma^2)*(B[t, T] - T + t) - (sigma^2*(B[t, T])^2)/(4*a);

P[t_, T_, r_] := Exp[A[t, T] - B[t, T]*r];

D[P[t, T, r], {r, 1}] /. B[t, T] -> HoldForm[B[t, T]] /. P[t, T, r] -> HoldForm[P[t, T, r]]

gives the desired

enter image description here

$\endgroup$
7
  • $\begingroup$ Alternatively, ReplaceRepeated can be used for nested functions, for instance, D[P[t, T, r], {r, 1}] //. {B[t, T] -> HoldForm[B[t, T]], P[t, T, r] -> HoldForm[P[t, T, r]]}. $\endgroup$
    – bbgodfrey
    Jan 10, 2016 at 16:15
  • $\begingroup$ Thanks both, this is wonderful! $\endgroup$ Jan 10, 2016 at 16:58
  • 1
    $\begingroup$ @user2743931 try it with f[x_]:= 2 Exp[10*x^2] and you'll see that this method works with the examples given through sheer coincidence. I'm afraid it is not an easy problem to solve properly. $\endgroup$ Jan 10, 2016 at 19:01
  • 1
    $\begingroup$ @user2743931 it is nothing to do with HoldForm. It is because the approach seeks to find instances where the function in question appears verbatim in the output. For most possible functions and outputs this will never happen. The answer/comments of AccidentalFourierTransform illustrate very well the problems with this idea, and I think make a strong case that at the very least you need to supply an ansatz. $\endgroup$ Jan 10, 2016 at 19:24
  • 1
    $\begingroup$ @user2743931 it should be obvious according to the counterexample I mentioned. f[x_]:= 2 Exp[10*x^2]; D[f[x], x] produces 40 Exp[10 x^2] x. There is no 2 Exp[10 x^2] to be found in that, and so the literal search for the value of f[x] fails. $\endgroup$ Jan 10, 2016 at 19:53
3
$\begingroup$

Here's a pretty snazzy method:

DifferentialRootReduce[Exp[10 x^2], {x, 0}]
  (* -> DifferentialRoot[Function[{y, x}, {-20 x y[x] + y'[x] == 0, y[0] == 1}]][x] *)

(The actual output uses \[FormalY] and \[FormalX], which I replaced with the corresponding normal letters for clarity.)

Though as noted in another answer, one can conjure any number of ODEs that have a given analytic function as a solution, one can usually derive a unique linear ODE; DifferentialRootReduce[] is the function intended for finding that linear ODE, if there is one.

$\endgroup$
1
  • $\begingroup$ thank you for introducing me to DifferentialRoot[] :) $\endgroup$
    – ubpdqn
    Apr 18, 2016 at 12:07
2
$\begingroup$

Note that any function satisfies an infinite number of ODE's, so the answer to your question is non-unique. The following code will do what you want for some specific cases (it works for your example):

f[x_] := Exp[10 x^2]
ansatz = Inactivate[D[f[x], x] + a x f[x] + b f[x] + c, f];
Solve[Activate[#==0&/@(D[ansatz,{x,#}]&/@Range[0,2]/.x->0),f],{a,b,c}]
ansatz == 0/.%

If you want to use a more general ansatz, with $n$ unknowns (in my code, $n=3$ for $a,b,c$), you just have to modify ansatz, and change Range[0,2] to Range[0,n-1].

Of course, any suggestion to my code will be highly well-recieved!

$\endgroup$
5
  • $\begingroup$ It doesn't necessarily need to be related to differenation. I can just simply have an expression: 20 E^(10 x^2) x and replace it with 20 f[x] x, where f[x_]:= Exp[10*x^2] $\endgroup$ Jan 10, 2016 at 14:49
  • 1
    $\begingroup$ in that case its not easy to see how should we handle other examples: if $f(x)=x$, then what is the expected output for $3x^2$? is it $3xf(x)$? or $3f(x)^2$? $\endgroup$ Jan 10, 2016 at 15:01
  • $\begingroup$ The third option, is what I would expect. Replace all $x$ by $f(x)$... $\endgroup$ Jan 10, 2016 at 15:12
  • 1
    $\begingroup$ I still think the answer is highly non-unique. Maybe the example $f(x)=x$ was too simple to illustrate my point. Take $f(x)=\sqrt{1-x^2}$. Then $x^3\to x(1-f(x)^2)$? or $x^3\to (1-f(x)^2)^{3/2}$? both seem very valid to me... If you dont expect any $x$ in the output, then $20x\mathrm e^{10x^2}\to 2\sqrt{10\log(f(x))}f(x)$, and not what you wrote.$$$$(I hope Im not being annoying: if eldo's answer address your concerns then its pointless for us to keep on arguing abot this; if his answer is not enough for you, then I wont be able to help unless I know precisely what the expected output is) $\endgroup$ Jan 10, 2016 at 15:30
  • $\begingroup$ I got your point, but maybe that is overly complicated. Any of these expressions would do. The same problem could occur if I set, say, $f(x) = 2x$ and $g(x)= 10x$. Then an expression $z(x)=20x$ could be any combination of the two (e.g. $z(x)=2g(x)$ or $z(x)=g(x)+5f(x)$, etc...). eldo's suggestion works great for me. $\endgroup$ Jan 10, 2016 at 17:12
1
$\begingroup$

You can achieve this by giving a definition for the derivative only. For example:

f' ^= 20 # f[#]&

Then:

f[x]
f'[x]
f''[x]
f'''[x]

f[x]

20 x f[x]

20 f[x] + 400 x^2 f[x]

1200 x f[x] + 8000 x^3 f[x]

$\endgroup$
0
$\begingroup$

Not really an answer, but some thoughts:

  • For something like this, you don't need :=. You can do f[x_] = Exp[10*x^2]

  • This probably isn't the answer you're looking for, but:

D[f[x],x] /. f[x] -> "f[x]"

gives you what you want. Of course, the second f[x] here is a string, not a function.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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