# How to plot partial derivative of an integral vs. another variable?

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?

• Yes. Is the D function only works for symbol equation? – user14613 Jun 14 '14 at 16:27
• I'm sorry, you need to Evaluate: see here. – Öskå Jun 14 '14 at 16:31
• Can't take derivative w.r.t a number. – Nasser Jun 14 '14 at 16:32
• 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 – Nasser Jun 14 '14 at 16:36
• See here. – Ö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];


Check:

?dT


?T


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


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
*)


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.

• 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 1: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