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


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

  • $\begingroup$ Yes. Is the D function only works for symbol equation? $\endgroup$ – user14613 Jun 14 '14 at 16:27
  • $\begingroup$ I'm sorry, you need to Evaluate: see here. $\endgroup$ – Öskå Jun 14 '14 at 16:31
  • $\begingroup$ Can't take derivative w.r.t a number. $\endgroup$ – Nasser Jun 14 '14 at 16:32
  • $\begingroup$ Is this what you mean? diff[t_] = D[Integrate[Exp[(eta - 1)*t], {eta, 0, 4}], t]; Plot[Evaluate@diff[t], {t, 0, 1}] gives !Mathematica graphics $\endgroup$ – Nasser Jun 14 '14 at 16:36
  • $\begingroup$ See here. $\endgroup$ – Öskå Jun 14 '14 at 16:38

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];



Mathematica graphics


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


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

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}]


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.

  • $\begingroup$ 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. $\endgroup$ – acl Jun 16 '14 at 1:10
  • $\begingroup$ @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. $\endgroup$ – Szabolcs Jun 16 '14 at 14:32
  • $\begingroup$ but you can see from the link what they do... $\endgroup$ – acl Jun 16 '14 at 14:38
  • $\begingroup$ @acl Yes, I meant that I wrote this based on a vague memory. It seems I was not correct. I already updated the post. $\endgroup$ – Szabolcs Jun 16 '14 at 14:39
  • $\begingroup$ @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.) $\endgroup$ – Michael E2 Jun 16 '14 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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