Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
share|improve this question
2  
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 at 19:55
    
After answering the question I guess its worth pointing out this example can be directly evaluated with Integrate ( !..:-) .. –  george2079 Feb 25 at 21:03
    
There is a very long expression and use Integrate is very slow. thanks :) –  hesam Feb 25 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 at 21:28

2 Answers 2

up vote 5 down vote accepted

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
*)
share|improve this answer
    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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.