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
Answer
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
*)
Comments
Post a Comment