Message Boards Message Boards


How can I do NIntegrate of derivative of the function?

Posted 4 days ago
2 Replies
3 Total Likes

Hi everyone,

I define the function and define new function using the derivative and NIntegrate on it. As an example, I made some simple case.

f[x_?NumericQ, a_?NumericQ] := Sin[a x]
d[x_?NumericQ] := NIntegrate[D[f[x, a], x], {a, 0, 1}]

And try to get the result at some x.

With[{x = 2}, Evaluate@d[x]]

But it does not work...And I got error message

General::ivar: 2 is not a valid variable.

NIntegrate::inumr: The integrand \!(*SubscriptBox[([PartialD]), (2)](f[2, a])) has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,1}}.

How can I fix this?

2 Replies

Those ?NumericQ in the definition of f interfere with symbolic computation. Also, if you calculate the derivative with D[f[x, a], x], the variable x will become numerical too soon, before the symbolic derivative is calculated. One way to solve the problem is to use Derivative, which does not use a symbolic variable name:

f[x_, a_] := Sin[a x]
d[x_?NumericQ] := NIntegrate[Derivative[1, 0][f][x, a], {a, 0, 1}]

Another way, which localizes the symbolic variable:

f[x_, a_] := Sin[a x]
d[x_?NumericQ] :=
  NIntegrate[D[f[y, a], y] /. y -> x, {a, 0, 1}]]
Posted 2 days ago

Aha! Thank you!!

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract