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?

jscoot
  • 2,019
  • 3
  • 24
  • 27
  • 2
    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)`. – Kerrek SB Nov 26 '11 at 12:30

4 Answers4

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]
*) 
Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
Daniel Lichtblau
  • 6,854
  • 1
  • 23
  • 30
  • 3
    +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. – Sjoerd C. de Vries Nov 27 '11 at 13:25
  • Seems it does not work for _any_ analytic fun. See what happens with (for example) with `f[t_]:= Sin@Sin@t` – Dr. belisarius Nov 27 '11 at 13:49
  • Thanks! The same question about SeriesCoefficient's full symbolic capability in comparison with D's limited symbolic functionality. – jscoot Nov 27 '11 at 14:54
  • 1
    @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. – Daniel Lichtblau Nov 27 '11 at 16:05
  • 2
    @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). – Daniel Lichtblau Nov 27 '11 at 16:11
  • 1
    @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. – Daniel Lichtblau Nov 27 '11 at 16:15
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]
*)
Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
  • 1
    I slightly disagree with mma's output format, I'd write that as `((1~Divide~(2 - t))~Power~#1 1~Pochhammer~-1 + #1) &` :) – acl Nov 26 '11 at 13:05
  • @acl Let's keep this civilized :) – Dr. belisarius Nov 26 '11 at 13:17
  • @acl I'll get you back, you just wait. ;^p – Mr.Wizard Nov 26 '11 at 14:59
  • 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. – jscoot Nov 27 '11 at 14:50
  • @jscoot function transformation in Mma is an art, and probably not worth the effort. – Dr. belisarius Nov 27 '11 at 15:06
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.

Vladimir Reshetnikov
  • 11,750
  • 4
  • 30
  • 51
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}
telefunkenvf14
  • 1,011
  • 7
  • 19
  • 1
    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`. – Simon Nov 28 '11 at 02:44
  • 2
    @Simon - 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. – telefunkenvf14 Nov 28 '11 at 03:33