1
$\begingroup$

Because NIntegrate[] does not support parallel computation out of the box, I'm doing a simplified version by breaking the integration limit into pieces and using ParallelTable[]. The following code is just a minimum working example in which I used Sinc[] as the integrated function.

iser[x_] := NIntegrate[Sinc[t + x], {t, 0, 100}];

ipar[x_] := Total[ParallelTable[
             With[{xx = x}, NIntegrate[Sinc[t + xx], {t, ii*25, ii*25 + 25}]
             ], {ii, 0, 3}]
            ];

where iser[] is the usual serialize version, and ipar[] is the parallel version. Then I do DistributeDefinitions[ipar]. Passing a value to iser[] and ipar[] and both give the correct answer:

In[28]:= ipar[3]

Out[28]= -0.270322

But, when I try to run Plot[ipar[x], {x, 0, 10}] // Timing I get error messages that says

 NIntegrate::inumr: The integrand Sinc[t+x] has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,25}}.

 NIntegrate::inumr: The integrand Sinc[t+x] has evaluated to non-numerical values for all sampling points in the region with boundaries {{50,75}}.

 NIntegrate::inumr: The integrand Sinc[t+x] has evaluated to non-numerical values for all sampling points in the region with boundaries {{75,100}}.

 NIntegrate::inumr: The integrand Sinc[t+x] has evaluated to non-numerical values for all sampling points in the region with boundaries {{25,50}}.

 NIntegrate::nlim: t = 25. ii is not a valid limit of integration.


 General::stop: Further output of NIntegrate::nlim will be suppressed during this calculation.

 $Aborted

I know I can just generate a table of values and use ListPlot[] instead, but it's not easy to do adaptive sampling as Plot[] does. How can this be fixed?

$\endgroup$
7
  • $\begingroup$ ipar[x_?NumberQ] := ... $\endgroup$
    – wxffles
    Commented Nov 9, 2012 at 2:23
  • $\begingroup$ @wxffles Thanks. That solves it. Bit strange that ipar[3] returns the correct value without the query ?NumberQ $\endgroup$ Commented Nov 9, 2012 at 2:56
  • 2
    $\begingroup$ Please read this for a full explanation: support.wolfram.com/kb/3820 $\endgroup$
    – Searke
    Commented Nov 9, 2012 at 5:14
  • 2
    $\begingroup$ @davidsedai please rename your post to Table not working with Plot as the problem has nothing to do with Parallel in practice. And change the tag? $\endgroup$
    – chris
    Commented Nov 9, 2012 at 7:13
  • $\begingroup$ @Searke Thanks for the link $\endgroup$ Commented Nov 9, 2012 at 7:22

1 Answer 1

2
$\begingroup$

As mentioned in the comments, using a condition for the arguments of ipar and iser is needed.

A broader discussion of how and when to implement this solution is found in this answer.

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