1

Just to take an example, because my original numerical integral was too complex:

T[t_] = NIntegrate[Exp[(eta - 1)*t], {eta, 0, 4}]

But when I am using

 Plot[D[T[t],t],{t,0,1}]

It seems it doesn't work. How can I plot the derivative of the integral?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
user14613
  • 41
  • 4

3 Answers3

4

The derivative operator D will (symbolically) differentiate NIntegrate. It's tricky to keep NIntegrate from evaluating and giving error messages. If we block the evaluation of NIntegrate , then D will still differentiate it properly. To get the NIntegrate expression from the function T, we block NumericQ and redefine it to evaluate to true; then T[t] will evaluate to the expression NIntegrate[Exp[(eta - 3) * t], {eta, 0, 4}]. (I changed the OP's function slightly to make a better plot.) [Edit: Set the attribute of the blocked NIntegrate to HoldAll to keep arguments from evaluating. It makes no difference in the OP's example, but it's better this way.]

ClearAll[T, dT];
T[t_?NumericQ] := NIntegrate[Exp[(eta - 3)*t], {eta, 0, 4}];
Block[{NIntegrate, NumericQ = (True &)},
  SetAttributes[NIntegrate, HoldAll];
  dT[t_?NumericQ] = D[T[t], t];
  ];
T /: D[T[t_], t_] := dT[t];

Check:

?dT

Mathematica graphics

?T

Mathematica graphics

Plot[Evaluate[{T[t], D[T[t], t]}], {t, 0, 2}, AxesOrigin -> {0, 0}]

Mathematica graphics

It's quite a bit faster, too, than using ND or T'[t]:

Needs["NumericalCalculus`"];  (* from Szabolcs' answer *)
ndT[tt_] := Block[{t}, ND[T[t], t, tt]];

Plot[Evaluate[D[T[t], t]], {t, 0, 2}] // AbsoluteTiming // First
Plot[ndT[t], {t, 0, 2}] // AbsoluteTiming // First
Plot[T'[t], {t, 0, 2}] // AbsoluteTiming // First

(*
  0.287848
  1.878337
  4.054600
*)
Michael E2
  • 235,386
  • 17
  • 334
  • 747
1

Another possibility is to use ParametricNDSolveValue:

T = ParametricNDSolveValue[
    {int'[eta] == Exp[(eta-3) t], int[0]==0},
    int[4],
    {eta, 0, 4},
    t
]

where I used @Michael's variation of the integral for comparison purposes. Then:

Plot[{T[t], T'[t]}, {t, 0, 2}]

enter image description here

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
1

You need to use := when defining T in conjunction with NumericQ:

Clear[T]
T[t_?NumericQ] := NIntegrate[Exp[(eta - 1)*t], {eta, 0, 4}]

See here for why.

Use Derivative in Plot, or use Evaluate on D:

Plot[T'[t], {t, 0, 1}]

or

Plot[D[T[t],t]//Evaluate, {t,0,1}]

//Evaluate simply ensures that D[T[t],t] will evaluate T'[t] before a number gets substituted for t. Plot tries to get this right automatically, but it doesn't always manage. For this reason I prefer to use Evaluate explicitly.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • Why do you say ND is more accurate? @Belisarius and I found out here that one (D) is a centred approximation, the other (ND) only uses values to the right of x to find the derivative at x but there's no other real difference. – acl Jun 16 '14 at 01:10
  • @acl Thanks for the link. I don't remember exact examples. I seem to recall that on several counts ND gave a good result while Derivative didn't. – Szabolcs Jun 16 '14 at 14:32
  • but you can see from the link what they do... – acl Jun 16 '14 at 14:38
  • @acl Yes, I meant that I wrote this based on a vague memory. It seems I was not correct. I already updated the post. – Szabolcs Jun 16 '14 at 14:39
  • @acl, Szabolcs I think the advantage of ND is that it can be adjusted to the how fast the function is varying. For example, f[t_?NumericQ] := NIntegrate[Exp[(eta - 3)/t], {eta, 0, 4}]. Then f'[0.2] is way off, ND[f[t], t, 0.2] is ok, and Apply[ND, {f[t], t, t0, Scale -> t0^2} /. t0 -> 0.2] agrees with the exact value up to about 9 digits. (I picked Scale -> t0^2 because it known to be proportional to 1/f'[t0] in this special case.) – Michael E2 Jun 16 '14 at 15:08
  • @MichaelE2 Yes, you can control the parameters in ND easily (see @belisarius' addition to the answer I linked to above, it clarifies evertyhing). By the way, you can read the code for ND: it's in /AddOns/Packages/NumericalCalculus/NLimit.m – acl Jun 16 '14 at 15:13
  • @MichaelE2 Exactly. It's a pity Derivative won't let us do that. ND also has Method -> NIntegrate. – Szabolcs Jun 16 '14 at 15:14