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've been working on a problem where I need to integrate a function numerically. The function is quite complex and it takes almost an hour for Mathematica to make a plot. To speed the process up I thought that I could implement a parallel NIntegrate function using ParallelTable[]. I believe I successfully accomplished that for single numerical values for the limits of the integration but there's something wrong since when I try to make a plot I got a bunch of errors. Here's my code, where instead of my horrible integrand I used the function $f(x)=x$

ParalellNIntegrate[imin_?NumericQ, imax_?NumericQ] :=
  (step = (imax - imin)/4;
   Total[
    ParallelTable[
     NIntegrate[u, {u, i, i+step}], {i, imin, imax - step, step}]
    ]);
Plot[ParalellNIntegrate[0, x], {x, -1, 1}]

(kernel 2) NIntegrate::nlim: u = 0.25 x is not a valid limit of integration.

(kernel 1) NIntegrate::nlim: u = 0.75 x is not a valid limit of integration.

(kernel 2) NIntegrate::nlim: u = 0.5 x is not a valid limit of integration.

(kernel 1) NIntegrate::nlim: u = x is not a valid limit of integration.

(kernel 2) NIntegrate::nlim: u = i+0.25 x is not a valid limit of integration.

(kernel 1) NIntegrate::nlim: u = i+0.25 x is not a valid limit of integration.

(kernel 1) General::stop: Further output of NIntegrate::nlim will be suppressed during this calculation.

(kernel 2) General::stop: Further output of NIntegrate::nlim will be suppressed during this calculation.

Can somebody help me?

PS: I also have this doubt about the step and I think I could use this post to clear it (I do realize that I shouldn't post more than one question per post but it's somehow related and it seems silly to open a post just for this). I divided the interval of integration in 4 regions since my cpu only works with 4 threads could somebody tell me if I'm thinking correctly?

share|improve this question

closed as off-topic by Michael E2, Karsten 7., bobthechemist, RunnyKine, Simon Woods Sep 15 at 21:15

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Michael E2, Karsten 7., bobthechemist, RunnyKine, Simon Woods
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Before trying to parallelize NIntegrate, check if it is already making use of all your CPU cores internally. If it is, trying to parallelize manually will just slow things down. I noticed that when using the default "GlobalAdaptive" strategy, NIntegrate tends to already make use of all cores (depending on the integrand). –  Szabolcs Feb 24 at 21:02
1  
@Szabolcs No, in my case it only uses one core. I had previously checked that when the integral doesn't have analytical solution my implementation is indeed faster, although weirdly it oscillates between 4% and 100% faster depending on the limits of the integration... –  PML Feb 24 at 22:08
1  
It seems to me that plotting in that way is going to make a lot of redundant calculations. If you want the points at x = 0.5 and x = 1, you have to integrate from 0 to 0.5, and then integrate again from 0 to 1. But really to get that second point, we only need to add on the integral from 0.5 to 1. I'd be tempted to build up some points manually and then use a ListPlot. Also, your code works fine for me (apart from the extraneous comma). –  wxffles Feb 24 at 22:15
    
@wxffles Ups, just deleted the comma. Now that's weird, for me mathematica just throws those errors. –  PML Feb 24 at 22:38
2  
Your code works as is in V10/V9.0.1. –  Michael E2 Sep 14 at 4:01

1 Answer 1

As I mentioned in a comment, the code works fine the way it is.

Here's a faster way:

Clear[f];
f[x_] := x;
integral = NDSolveValue[{y'[x] == f[x], y[0] == 0}, y, {x, -1, 1}];
Plot[integral[x], {x, -1, 1}]
share|improve this answer

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