4
$\begingroup$

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
$\endgroup$
4
  • 2
    $\begingroup$ 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. $\endgroup$
    – Steve
    Commented Feb 25, 2014 at 19:55
  • $\begingroup$ After answering the question I guess its worth pointing out this example can be directly evaluated with Integrate ( !..:-) .. $\endgroup$
    – george2079
    Commented Feb 25, 2014 at 21:03
  • $\begingroup$ There is a very long expression and use Integrate is very slow. thanks :) $\endgroup$
    – hesamaero
    Commented Feb 25, 2014 at 21:11
  • $\begingroup$ 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. $\endgroup$
    – george2079
    Commented Feb 25, 2014 at 21:28

2 Answers 2

6
$\begingroup$

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
*)
$\endgroup$
3
$\begingroup$
    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

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