I'm performing a definite integral on a sum of 66 fairly complicated terms. Sample term + integral (a[2] is a constant):

temp1733[n_, lat_] = 
   Sec[0.005782961777094692 - 0.4001419318234436*Cos[0.017167172970436028*n] - 
      0.0060922154967620835*Cos[0.034334345940872056*n] - 
      0.002387468786938206*Cos[0.05150151891130809*n] + 
      0.0711242550022214*Sin[0.017167172970436028*n] + 
      0.0005863132618294766*Sin[0.034334345940872056*n] + 
      0.0013462049383894524*Sin[0.05150151891130809*n]] - 
   1.*Tan[lat]*Tan[0.005782961777094692 - 0.4001419318234436*
       Cos[0.017167172970436028*n] - 0.0060922154967620835*
       Cos[0.034334345940872056*n] - 0.002387468786938206*
       Cos[0.05150151891130809*n] + 0.0711242550022214*
       Sin[0.017167172970436028*n] + 0.0005863132618294766*
       Sin[0.034334345940872056*n] + 0.0013462049383894524*

temp1734 = Integrate[temp1733[n,lat], {n, 0.5, 366.5}, {lat,
 -60*Degree, 60*Degree}]

On my machine, the definite integral above times out. Since almost all my values are numerical, I'd like to use NIntegrate, but can't, because a[2] isn't a numerical value. Of course, in this case, I can simply do:

a[2]*NIntegrate[temp1733[n,lat]/a[2], {n, 0.5, 366.5}, {lat, 
 -60*Degree, 60*Degree}] 

to get the answer (it happens to be -18229.40312917879*a[2] in this case).

However, I don't want to have to look at each of my terms to factor out the constants.

Is there any way I can tell NIntegrate to use linearity of integration for constants? I understand why NIntegrate can't handle more deeply nested constants (see Using NIntegrate with constants), but it seems constants used in a purely linear way should work.

I did try things like Coefficient and CoefficientList, but they won't work in my case because the function I'm using isn't a polynomial in my constants. Even if I can't coerce NIntegrate to handle my functions, there must be a way to separate out and then rejoin the constant parts?

This question is a followup of sorts to the answer https://mathematica.stackexchange.com/a/165937/1722 which shows a more complicated (in my opinion) way to solve a similar problem.


1 Answer 1


You could use Collect:


-18229.4 a[2]

  • $\begingroup$ That's not bad, but some of my constants are in the form a[i]*a[j] or a[i]^2. My fault for not being more specific. $\endgroup$
    – user1722
    Feb 22, 2018 at 1:25
  • $\begingroup$ @barrycarter If all your constants are of the form a[i], then just use Collect[expr, _a, NIntegrate[...]&] instead. $\endgroup$
    – Carl Woll
    Feb 22, 2018 at 3:05
  • $\begingroup$ That didn't do quite what I expected: I had a[1]..a[9], and the Collect returned 19 terms (I expected at least 9 choose 2 = 36, one for each a[i],a[j] pair), but it did work, thanks! $\endgroup$
    – user1722
    Feb 22, 2018 at 3:28

Your Answer

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