Message Boards Message Boards

Can you parallelize a numerically solved Integral?

Posted 2 years ago
POSTED BY: sorabella91
3 Replies
POSTED BY: Daniel Lichtblau

If functions are listable like Cos, using Range together with Total usually gets met the fastest results.

In[1]:= f[x_] := Cos[x]; (*function*)
n = 75;

I2[f_, a_, b_, n_] := (h = (b - a)/n; 
  Sum[f[a + (i - 1/2)*h]*h, {i, 1, n}])

I2R[f_, a_, b_, n_] := With[{h = (b - a)/n},
  Total[f[a + Range[0.5, n - 0.5, 1.] h] h]
  ]

{t1, v1} = RepeatedTiming@I2[f, -1.0, 1, n];
{t2, v2} = RepeatedTiming@I2R[f, -1.0, 1, n];
{t1/t2, v1 === v2}

Out[6]= {16.0959, True}
POSTED BY: Martijn Froeling
Posted 2 years ago

Hello,

This method ended up being a bit slower. Perhaps, Cos[x] was not the best choice of function for describing my situation. It more closely resembles Cos[x*t] where the end result of the integration will not be a singular number but, rather, a function of t. This integration method is being used to create another function that I can plot as a function of t.

POSTED BY: Updating Name
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract