How to find a function's rth derivative when r is symbolic in Mathematica?
Asked Answered
C

4

15

I have a function f(t)=2/(2-t). It is not so hard to get the rth derivative at t=0 (i.e. 2^(-r)*r!) without using Mathematica. In the case of Mathematica calculation, I can get the r-th derivative when r=4 like this: D[2/(2-t), {t, 4}]. But how can I get the rth derivative at t=0 in Mathematica when r is ANY integer? I tried to use this expression, but it didn't work as expected:

Simplify[D[2/(2 - t), {t, r}], Assumptions -> Element[r, Integers]]  /. {t->0}

Is it possible to do the above math symbolically in Mathematica just as we humans do?

Corral answered 26/11, 2011 at 12:22 Comment(1)
Seems like Maple can do it: diff(x^4, x$n); results in pochhammer(5-n,n)*x^(4-n); diff(sin(x), x$n); gives sin(x+1/2*n*Pi).Kindred
T
14
f = FindSequenceFunction[Table[D[2/(2 - t), {t, n}], {n, 1, 5}], r]

(*
-> -((2 (2 - t)^-r Pochhammer[1, r])/(-2 + t))
*)
g[r_, t_] := f
FullSimplify@FindSequenceFunction[Table[g[r, t], {r, 1, 5}] /. t -> 0]

 (*
 -> 2^-#1 Pochhammer[1, #1] &
 *)

Edit

Or just

FindSequenceFunction[Table[D[2/(2 - t), {t, n}], {n, 1, 5}], r] /. t -> 0
(*
-> 2^-r Pochhammer[1, r]
*)

*Edit *

Note: While FindSequenceFunction[] works in this simple situation, don't bet on it in more general cases.

Edit

To get the result expressed in terms of the factorial function, just do:

FunctionExpand@FindSequenceFunction[Table[D[2/(2-t),{t, n}],{n,1,5}], r] /.t->0
(*
-> 2^-r Gamma[1 + r]
*)
Tilghman answered 26/11, 2011 at 12:51 Comment(5)
I slightly disagree with mma's output format, I'd write that as ((1~Divide~(2 - t))~Power~#1 1~Pochhammer~-1 + #1) & :)Antidote
@Antidote Let's keep this civilized :)Tilghman
@Antidote I'll get you back, you just wait. ;^pBeaverette
A good albeit indirect method to solve the problem! Thanks! Another small question: why didn't Mathematica show the output in the form of "2^-r r!" even if i applied your FunctionExpand with Assumptions -> Element[r, Integers]? I know Gamma[1+r] = r! for any r,but for integer, i would prefer r! due to its common use.Corral
@Corral function transformation in Mma is an art, and probably not worth the effort.Tilghman
S
16

For analytic functions you can use SeriesCoefficient.

nthDeriv[f_, x_, n_] := n!*SeriesCoefficient[f[x], {x, x, n}]

Your example:

f[t_] := 2/(t - 2)

nthDeriv[f, t, n]
(*
-> Out[39]= n!*Piecewise[{{-2*(2 - t)^(-1 - n), n >= 0}}, 0]
*) 
Scrubland answered 26/11, 2011 at 22:17 Comment(6)
+1 Indeed, the "More information" part of the SeriesCoefficient documentation reads: "In the form SeriesCoefficient[f,{x,Subscript[x, 0],n}], the order n can be symbolic. " Two questions: 1) Why is this symbolic evaluation capability reserved for SeriesCoefficient and not granted to D? 2) I'm not sure whether I understand the {x, x, n} form. The second argument is normally used for x0, the point around which the series is developed, leading to the (x-x0) terms in the expansion. But what would (x-x) mean? In Series you can't do it this way.Spenser
Seems it does not work for any analytic fun. See what happens with (for example) with f[t_]:= Sin@Sin@tTilghman
Thanks! The same question about SeriesCoefficient's full symbolic capability in comparison with D's limited symbolic functionality.Corral
@belisarius I should have stated analyticity as a necessary condition. Getting general nth terms relies on properties related to symbolic closed forms for sums, as best I recall.Scrubland
@Sjoerd (1) D makes no assumptions whereas Series and SeriesCoefficient "assume", in places, that some form of series representation actually exists. There are ways, albeit clumsy, in which this assumption is finessed. Moreover SeriesCoefficient has subsumed some general "nth term" technology borrowed from symbolic summation capabilities. I doubt it would make sense to try to retrofit any of that into D (might create more problems than it solves).Scrubland
@Sjoerd (2) In Series it would make no sense at least in the output form, because Series uses powers of (x-x0) and so they'd be zeros when x and x0 coincide. From a SeriesCoefficient view, both are formal variables and, as it happens, they can even be the same.Scrubland
T
14
f = FindSequenceFunction[Table[D[2/(2 - t), {t, n}], {n, 1, 5}], r]

(*
-> -((2 (2 - t)^-r Pochhammer[1, r])/(-2 + t))
*)
g[r_, t_] := f
FullSimplify@FindSequenceFunction[Table[g[r, t], {r, 1, 5}] /. t -> 0]

 (*
 -> 2^-#1 Pochhammer[1, #1] &
 *)

Edit

Or just

FindSequenceFunction[Table[D[2/(2 - t), {t, n}], {n, 1, 5}], r] /. t -> 0
(*
-> 2^-r Pochhammer[1, r]
*)

*Edit *

Note: While FindSequenceFunction[] works in this simple situation, don't bet on it in more general cases.

Edit

To get the result expressed in terms of the factorial function, just do:

FunctionExpand@FindSequenceFunction[Table[D[2/(2-t),{t, n}],{n,1,5}], r] /.t->0
(*
-> 2^-r Gamma[1 + r]
*)
Tilghman answered 26/11, 2011 at 12:51 Comment(5)
I slightly disagree with mma's output format, I'd write that as ((1~Divide~(2 - t))~Power~#1 1~Pochhammer~-1 + #1) & :)Antidote
@Antidote Let's keep this civilized :)Tilghman
@Antidote I'll get you back, you just wait. ;^pBeaverette
A good albeit indirect method to solve the problem! Thanks! Another small question: why didn't Mathematica show the output in the form of "2^-r r!" even if i applied your FunctionExpand with Assumptions -> Element[r, Integers]? I know Gamma[1+r] = r! for any r,but for integer, i would prefer r! due to its common use.Corral
@Corral function transformation in Mma is an art, and probably not worth the effort.Tilghman
L
3

There is another approach that sometimes works better (gives closed-form expressions rather than recurrence relations):

In[1]:= InverseFourierTransform[(-I k)^n FourierTransform[1/(1 + x^2)^Log[2], x, k] , k, x]
Out[1]= (2^(-1 + n - 1/2 Log[1/x^2])
      Abs[x]^-Log[2] ((-I)^
      n ((1 + n) x Gamma[(1 + n)/2] Gamma[
      n/2 + Log[2]] Hypergeometric2F1[(1 + n)/2, n/2 + Log[2], 1/
      2, -x^2] (n + Log[4]) - 
    2 I Gamma[1 + n/2] Gamma[
      1/2 (1 + n + Log[4])] ((1 + x^2) Hypergeometric2F1[(2 + n)/
         2, 1/2 (1 + n + Log[4]), -(1/2), -x^2] - 
       Hypergeometric2F1[(2 + n)/2, 1/2 (1 + n + Log[4]), 1/
         2, -x^2] (1 + x^2 (3 + 2 n + Log[4])))) + 
 I^n ((1 + n) x Gamma[(1 + n)/2] Gamma[
      n/2 + Log[2]] Hypergeometric2F1[(1 + n)/2, n/2 + Log[2], 1/
      2, -x^2] (n + Log[4]) + 
    2 I Gamma[1 + n/2] Gamma[
      1/2 (1 + n + Log[4])] ((1 + x^2) Hypergeometric2F1[(2 + n)/
         2, 1/2 (1 + n + Log[4]), -(1/2), -x^2] - 
       Hypergeometric2F1[(2 + n)/2, 1/2 (1 + n + Log[4]), 1/
         2, -x^2] (1 + x^2 (3 + 2 n + Log[4]))))))/((1 + n) 
       Sqrt[Pi] x Gamma[Log[2]] (n + Log[4]))

It also can be used to find repeated anti-derivatives.

Lancet answered 9/5, 2013 at 5:33 Comment(0)
F
1

The other answers make me wonder if I'm not understanding underlying question, but I think you should look at Derivative instead of D for this kind of thing.

In[1]:= Remove[f, fD]
f = 2/(2 - #) &;
fD[r_Integer, EvaluatedAt_] := Derivative[r][f][#] &[EvaluatedAt]

Now we have a function that can easily be evaluated for any r and value.

In[4]:= fD[#, 0] & /@ {1, 2, 3, 4, 5, 6}

Out[4]= {1/2, 1/2, 3/4, 3/2, 15/4, 45/4}
Furlough answered 27/11, 2011 at 20:44 Comment(2)
Your solution will only work for actual integer r, I believe that the OP was looking for a solution that would work for a symbolic integer r.Denims
@Denims - OP requested "...when r is ANY integer." Thought I'd nailed that part :), although I (think I) now understand where other answers were going with trying to find the underlying sequence function. Which makes me wonder if interpolating functions would be another approach. BTW, I think way to fast for my typing to keep up.Furlough

© 2022 - 2024 — McMap. All rights reserved.