5
$\begingroup$

My function is

f[a_, b_] := NIntegrate[Sqrt[(Cos[t] - a)^2 + b^2], {t, 0, Pi}]

I want to calculate g[1,1] where g[a,b] is defined as...

g[a_, b_] := Derivative [1, 0][f][a, b]

I get the error

The integrand has evaluated for non-numerical values...

Now I can easily calculate the derivative first and not get an error, but I don't want to do that for a particular reason. I can also use a finite difference formula that I create myself, but I want to use procedures already defined by Mathematica.

Is it possible to avoid this error and calculate the derivative of a numerical integral?

$\endgroup$
  • $\begingroup$ Please, go to the help centre and read more about the markdown and how to properly format your code and format it. $\endgroup$ – Sektor Sep 9 '15 at 19:14
9
$\begingroup$

This should work:

Clear[f];
f[a_?NumericQ, b_?NumericQ] := NIntegrate[Sqrt[(Cos[t] - a)^2 + b^2], {t, 0, Pi}]

and then add //N at the end of the definition of g[a,b]

g[a_, b_] := Derivative[1, 0][f][a, b]//N
g[1,1]
(*1.80525*)
$\endgroup$
  • $\begingroup$ This is a very simple solution, thank you! What exactly does the //N change here? Does it tell Mathematica to use a different procedure to calculate the derivative? $\endgroup$ – potatosoup Sep 9 '15 at 20:14
  • 3
    $\begingroup$ Yes. In particular in the documentation it is said "N[f'[x]] will give a numerical approximation to a derivative. " at the end of the Details section. $\endgroup$ – Nikolay Gromov Sep 9 '15 at 20:17
  • $\begingroup$ Interesting. I thought that 'N[ ]' only converted a result after evaluation, and didn't effect the method of evaluation. I also tried using 'Needs["NumericalCalculus`"]' together with 'ND[f[a,b],a,{1,1}]', but this doesn't work. Do you know why the Mathematica function 'ND[ ]' doesn't do the same thing as Derivative[ ]\\N? $\endgroup$ – potatosoup Sep 9 '15 at 20:26
  • $\begingroup$ As far as I understood the syntax of ND you should rather use ND[f[a, 1], a, 1] which works for me $\endgroup$ – Nikolay Gromov Sep 9 '15 at 20:52
  • $\begingroup$ Can you show me exactly what you are typing into Mathematica for ND? I am still getting an error. $\endgroup$ – potatosoup Sep 11 '15 at 19:54
6
$\begingroup$

Just ignore the error message. Use

g[a_, b_] := Quiet@Derivative[1, 0][f][a, b]
(* 1.80525 *)

This answer can be verified by

(f[1.005, 1] - f[.995, 1])/.01
(* 1.80525 *)
$\endgroup$
2
$\begingroup$

If the function can be evaluated at complex arguments, one possibility is to use the complex step derivative approximation of Squire and Trapp.

For this function, it proceeds like so:

f[a_?NumericQ, b_?NumericQ] := NIntegrate[Sqrt[(a - Cos[t])^2 + b^2], {t, 0, Pi}]

(* complex step approximation *)
With[{x = 1, y = 1, h = $MachineEpsilon}, Im[f[x + I h, y]]/h]
   1.80525

(* analytic derivative *)
With[{a = 1, b = 1},
     NIntegrate[(a - Cos[t])/Sqrt[(a - Cos[t])^2 + b^2], {t, 0, Pi}]]
   1.8052526175436538
$\endgroup$
  • $\begingroup$ and you claim not to be a mathematician. Interesting find. $\endgroup$ – rcollyer Oct 7 '15 at 15:55

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.