2
$\begingroup$

In mathematica, how would I use parallel computing (or paralleltable) to compute the integrals of $x^2$, $x$, $3x$ from $x=0$ to $10$ just like in the first answer of https://superuser.com/questions/315337/how-to-make-commands-in-mathematica-8-use-all-cores? After entering the three functions into a table, I'm not sure how to tell Mathematica to integrate the functions. I understand that parallel computing is not the most efficient method, but once I realize how to do this, I can extend it to a much more complicated series of functions.

The motivation of this question is that I have a complicated function (see below) that takes a very, very long time to integrate (after two hours, the function still had not integrated). I'm hoping to break this function up into a group of slightly less complicated functions and utilize parallel computing to compute the integrals.

If I can't do this, is there a way to let mathematica use all four cores on my computer to compute the integral, but not using parallel computing?

Thank you.

(Sin[q - x] ((0.476497 - 0.401956 I) - (0.476497 + 0.598044 I) Tanh[
        0.933024 (q - x)]) + 
   Cos[q - x] ((0.401956 + 0.504489 I) + (0.598044 + 0.504489 I) Tanh[
        1.06598 (q - x)])) (Sin[
     q + x] ((0.476497 - 0.401956 I) - (0.476497 - 0.598044 I) Tanh[
        0.933024 (q + x)]) + 
   Cos[q + x] ((0.401956 - 0.504489 I) + (0.598044 + 0.504489 I) Tanh[
        1.06598 (q + x)]))

Note: I plan to integrate x between -10 and 10 where p and q are arbitrary variables so I can then plot the result as a contour map.

$\endgroup$
3
  • $\begingroup$ maybe a duplicate with mathematica.stackexchange.com/questions/1620/… $\endgroup$
    – Stefan
    Commented Jul 8, 2013 at 18:24
  • $\begingroup$ Can I do numerical integration when p and q are arbitrary variables? $\endgroup$
    – user85503
    Commented Jul 8, 2013 at 19:49
  • $\begingroup$ Where is p? I only see q. $\endgroup$ Commented Jul 9, 2013 at 21:04

2 Answers 2

7
$\begingroup$

For NIntegrate, you can get a significant speed-up using the method option

Method -> {Automatic, "SymbolicProcessing" -> 0}

If you add this to your calculation, the integrand can be used as written in your question without modification, and it can be dropped right into the Plot as well:

Plot[Re[
      NIntegrate[
       (Sin[q - x] ((0.476497 - 
            0.401956 I) - (0.476497 + 0.598044 I) Tanh[
            0.933024 (q - x)]) + 
       Cos[q - x] ((0.401956 + 
            0.504489 I) + (0.598044 + 0.504489 I) Tanh[
            1.06598 (q - x)])) (Sin[
         q + x] ((0.476497 - 
            0.401956 I) - (0.476497 - 0.598044 I) Tanh[
            0.933024 (q + x)]) + 
       Cos[q + x] ((0.401956 - 
            0.504489 I) + (0.598044 + 0.504489 I) Tanh[
            1.06598 (q + x)])),
    {x, -10., 10.},
    Method -> {Automatic, "SymbolicProcessing" -> 0}]
   ],
  {q, -10., 10.},
  PlotPoints -> 10
  ] // AbsoluteTiming

timing of plot

This timing is for the integration and the Plot, and it's less than a second.

$\endgroup$
3
  • $\begingroup$ always something i preach...that's why i appreciate you write about it (+1) $\endgroup$
    – Stefan
    Commented Jul 9, 2013 at 22:53
  • $\begingroup$ I didn't know about this "SymbolicProcessing" -> 0 trick ... works brilliantly for complicated examples where we don't want symbolic output. Any downsides that we should be aware of? $\endgroup$
    – wolfies
    Commented Jul 10, 2013 at 5:21
  • $\begingroup$ Regarding downsides, I'd say you'll be safe using this option, and I would only leave it out if NIntegrate throws warnings or errors, because then symbolic processing might help massage the integrand to avoid potential numerical problems. $\endgroup$
    – Jens
    Commented Jul 11, 2013 at 3:57
1
$\begingroup$

I do not think that you can integrate your function analytically. However, if you just want to get a plot, the standard way to do so is:

int[qq_?NumberQ] :=
  NIntegrate[(Sin[
        q - x] ((0.476497 - 
           0.401956 I) - (0.476497 + 0.598044 I) Tanh[
           0.933024 (q - x)]) + 
      Cos[q - x] ((0.401956 + 
           0.504489 I) + (0.598044 + 0.504489 I) Tanh[
           1.06598 (q - x)])) (Sin[
        q + x] ((0.476497 - 
           0.401956 I) - (0.476497 - 0.598044 I) Tanh[
           0.933024 (q + x)]) + 
      Cos[q + x] ((0.401956 - 
           0.504489 I) + (0.598044 + 0.504489 I) Tanh[
           1.06598 (q + x)])), {x, -10., 10.}];
Plot[Re[int[q]], {q, -10., 10.}, PlotPoints -> 10] // AbsoluteTiming

This needs about 5 seconds.

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