# How to use NIntegrate when there are symbolic constant coefficients

I would like to numerically integrate an equation such as the one below in which there are symbolic constant coefficients. I used a very simple code but it doesn't work in general, that tried to deal with constant coefficients with patterns. Is there any general approach to NIntegrate where symbolic constant coefficients exist?

11.94 a[1, 1]^2 Cos[x]^2 Cos[θ]^2 +
21.31 c[1, 1]^2 Cos[x]^2 Cos[θ]^2 +
0.14702 a[1, 1] b[1, 1] Cos[x] Cos[θ]^2 Sin[x] - (
1.395 b[1, 1]^2 Cos[x] Cos[θ]^2 Sin[x])/(1 + x/2)^3 +
0.4669 b[1, 1]^2 Cos[θ]^2 Sin[x]^2 + (
1.395 b[1, 1]^2 Cos[θ]^2 Sin[x]^2)/(1 + x/
2)^4 /.
{b[a1_, a2_] b[a3_, a4_] g_ :> b[a1, a2] b[a3, a4] NIntegrate[g, {θ, 0, 2}, {x, 0, 1}],
a[a1_, a2_] a[a1_, a2_] g_ :> a[a1, a2] a[a1, a2] NIntegrate[g, {θ, 0, 2}, {x, 0, 1},
Method -> {Automatic, "SymbolicProcessing" -> 0}],
a[a1_, a2_] a[a3_, a4_] g_ :> a[a1, a2] a[a3, a4] NIntegrate[g, {θ, 0, 2}, {x, 0, 1},
Method -> {Automatic, "SymbolicProcessing" -> 0}],
b[a1_, a2_] b[a1_, a2_] g_ :> b[a1, a2] b[a1, a2] NIntegrate[g, {θ, 0, 2}, {x, 0, 1},
Method -> {Automatic, "SymbolicProcessing" -> 0}],
c[a1_, a2_] c[a1_, a2_] g_ :> c[a1, a2] c[a1, a2] NIntegrate[g, {θ, 0, 2}, {x, 0, 1},
Method -> {Automatic, "SymbolicProcessing" -> 0}],
a[a1_, a2_] b[a3_, a4_] g_ :> a[a1, a2] b[a3, a4] NIntegrate[g, {θ, 0, 2}, {x, 0, 1},
Method -> {Automatic, "SymbolicProcessing" -> 0}],
a[a1_, a2_] c[a3_, a4_] g_ :> a[a1, a2] c[a3, a4] NIntegrate[g, {θ, 0, 2}, {x, 0, 1},
Method -> {Automatic, "SymbolicProcessing" -> 0}],
b[a1_, a2_] c[a3_, a4_] g_ :> a[a1, a2] a[a3, a4] NIntegrate[g, {θ, 0, 2}, {x, 0, 1},
Method -> {Automatic, "SymbolicProcessing" -> 0}],
c[a1_, a2_] c[a3_, a4_] g_ :> c[a1, a2] c[a3, a4] NIntegrate[g, {θ, 0, 2}, {x, 0, 1},
Method -> {Automatic, "SymbolicProcessing" -> 0}]
} // Timing
-
Replacing [theta] with a simple roman variable, shortening the seemingly random real numbers involved, and reducing the code to a much shorter working example would make it easier for people here to help you. – Steve Feb 25 '14 at 19:55
After answering the question I guess its worth pointing out this example can be directly evaluated with Integrate ( !..:-) .. – george2079 Feb 25 '14 at 21:03
There is a very long expression and use Integrate is very slow. thanks :) – hesamaero Feb 25 '14 at 21:11
This is actually quite a good question, I hope you take the time to make the changes suggested by Steve. An example that can not be integrated analytically would be better as well. – george2079 Feb 25 '14 at 21:28

You have to be able to reduce the integrand to a sum of terms in which the symbolic constants are factors of the term. Then you can separate the terms and their factors. Gather the factors that are constants, and numerically integrate the (product of the) function-factors of each term.

expr = 11.94 a[1, 1]^2 Cos[x]^2 Cos[θ]^2 +
21.31 c[1, 1]^2 Cos[x]^2 Cos[θ]^2 +
0.14702 a[1, 1] b[1, 1] Cos[x] Cos[θ]^2 Sin[x] -
(1.395 b[1, 1]^2 Cos[x] Cos[θ]^2 Sin[x]) / (1 + x/2)^3 +
0.4669 b[1, 1]^2 Cos[θ]^2 Sin[x]^2 +
(1.395 b[1, 1]^2 Cos[θ]^2 Sin[x]^2) / (1 + x/2)^4;
terms = List @@@ List @@ expr
(*
{{11.94, a[1,1]^2, Cos[x]^2, Cos[θ]^2},
{21.31, c[1,1]^2, Cos[x]^2, Cos[θ]^2},
{0.14702, a[1,1], b[1,1], Cos[x], Cos[θ]^2, Sin[x]},
{-1.395, 1/(1+x/2)^3, b[1,1]^2, Cos[x], Cos[θ]^2, Sin[x]},
{0.4669, b[1,1]^2, Cos[θ]^2, Sin[x]^2},
{1.395, 1/(1+x/2)^4, b[1,1]^2, Cos[θ]^2, Sin[x]^2}}
*)

integrals = GatherBy[#, MemberQ[#, x | θ, Infinity] &] & /@ terms;
(Times @@@ integrals[[All, 1]]) *
(NIntegrate[Times @@ #,
{θ, 0, 2}, {x, 0, 1},
Method -> {Automatic, "SymbolicProcessing" -> 0}] & /@
integrals[[All, 2]]) // Total
(*
7.04119 a[1, 1]^2 + 0.0422025 a[1, 1] b[1, 1] +
0.00809433 b[1, 1]^2 + 12.5668 c[1, 1]^2
*)
-
fnintegrate[ exp_ , lim__ ] := Module[{f, vars},
vars = #[[1]] & /@ List[lim];
Distribute[f[exp]] //.
f[a_ b_ /; And @@ ((D[a, #] == 0) & /@ vars)  ] :> a f[b]
/. f[y_] :> NIntegrate[y, lim] ]

fnintegrate[
11.94` a[1, 1]^2 Cos[x]^2 Cos[θ]^2 +
(etc ) , {θ, 0, 2}, {x, 0, 1}]

7.04119 a[1, 1]^2 + 0.0422025 a[1, 1] b[1, 1] + 0.00809433 b[1, 1]^2 + 12.5668 c[1, 1]^2

-