2
$\begingroup$

The expression

Integrate[x^2, Flatten[{{x},{1,2}}]]

evaluates properly, to $\frac{7}{3}$. However,

NIntegrate[x^2, Flatten[{{x},{1,2}}]]

returns

NIntegrate::vars: Integration range specification Flatten[{{x},{1,2}}] is not of the form {x, xmin, ..., xmax}. >>

An explicit Evaluate is required on the second parameter to make this work. So I assume that NIntegrate has a HoldAll on its second parameter.

Did I have any way of knowing this before hand? That is, is this behavior documented? And, if it is (or even if it is not), why do the two functions behave differently?

$\endgroup$
3
  • 1
    $\begingroup$ Such information can be found at the end of the "Details and Options" section in the documentation for NIntegrate (and for other functions, too). Try also executing Attributes[NIntegrate]. $\endgroup$
    – Michael E2
    Commented Nov 12, 2013 at 2:48
  • $\begingroup$ @MichaelE2 Thanks. So do you also know why the two functions (should) behave differently? Also, if you want to post this as an answer, I'll accept it. $\endgroup$
    – rogerl
    Commented Nov 12, 2013 at 21:01
  • $\begingroup$ "…NIntegrate has a HoldAll on its second parameter…" - well, HoldAll does mean what it says; all inputs given are held. $\endgroup$ Commented May 26, 2015 at 23:49

1 Answer 1

2
$\begingroup$

The reason that it works for Integrate and not for NIntegrate is that NIntegrate has the attribute HoldAll and Integrate does not. Attributes, if any, are listed at the end of the "Details and Options" section of the documentation page for a function. They may also be inspected with the Attributes command:

Attributes@Integrate
(* {Protected,ReadProtected} *)

Attributes@NIntegrate
(* {HoldAll,Protected} *)

Aside from the difference point out by the OP, there is also this one if x has a value:

x = 4
(* 4 *)

NIntegrate[x^2, {x, 1, 2}]
(* 2.33333 *)

Integrate[x^2, {x, 1, 2}]

Integrate::ilim: Invalid integration variable or limit(s) in {4,1,2}. >>

I do not know why the developers have chosen to treat these similar functions differently.

$\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.