1
$\begingroup$

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?

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

3 Answers 3

4
$\begingroup$

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
*)
$\endgroup$
1
$\begingroup$

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.

$\endgroup$
7
  • $\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
    Commented Jun 16, 2014 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
    Commented Jun 16, 2014 at 14:32
  • $\begingroup$ but you can see from the link what they do... $\endgroup$
    – acl
    Commented Jun 16, 2014 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
    Commented Jun 16, 2014 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
    Commented Jun 16, 2014 at 15:08
1
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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