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
*)
Evaluate
: see here. $\endgroup$diff[t_] = D[Integrate[Exp[(eta - 1)*t], {eta, 0, 4}], t]; Plot[Evaluate@diff[t], {t, 0, 1}]
gives !Mathematica graphics $\endgroup$