Consider some dataset corresponding to the grid x1, x2, function[x1,x2]
:
function[x1_, x2_] = Exp[-x2/25]*Cos[20*x1]^2*Sin[x1];
dataset =
Flatten[Table[{x1, x2, function[x1, x2]}, {x1,
Table[10^
x, {x, -5., Log10[1/20. Pi/2], (Log10[1/20. Pi/2] + 5.)/
20}]}, {x2,
Table[10^
x, {x, Log10[5.3], Log10[200.], (Log10[200.] - Log10[5.3])/
20}]}], {1, 2}];
{x1min, x1max} = MinMax[dataset[[All, 1]]]
{x2min, x2max} = MinMax[dataset[[All, 2]]]
I interpolate it in two ways:
int1[x1_, x2_] = 10^(Interpolation[{Log10[#[[1]]], Log10[#[[2]]], Log10[#[[3]] + 10^-90]}&/@dataset, InterpolationOrder -> 1][Log10[x1], Log10[x2]]);
int2[x1_, x2_] = Interpolation[{#[[1]], #[[2]], #[[3]]}&/@dataset, InterpolationOrder -> 1][x1, x2];
The integration of the int1
is much slower than int2
:
testfunction[x1_, x2_] = Sqrt[(1 - Cos[x1])]*Exp[5/x2];
NIntegrate[int1[x1, x2], {x1, x1min, x1max}, {x2, x2min, x2max},
Method -> "InterpolationPointsSubdivision"] // AbsoluteTiming
NIntegrate[int2[x1, x2], {x1, x1min, x1max}, {x2, x2min, x2max},
Method -> "InterpolationPointsSubdivision"] // AbsoluteTiming
NIntegrate[
int1[x1, x2]*testfunction[x1, x2], {x1, x1min, x1max}, {x2, x2min,
x2max}, Method ->
"InterpolationPointsSubdivision"] // AbsoluteTiming
NIntegrate[
int2[x1, x2]*testfunction[x1, x2], {x1, x1min, x1max}, {x2, x2min,
x2max}, Method ->
"InterpolationPointsSubdivision"] // AbsoluteTiming
{0.960714, 0.0146339}
{0.0011156, 0.0189933}
{1.01974, 0.000414336}
{0.0718938, 0.000659965}
On the other hand, int1
has better behavior in case if function
changes fast between the neighboring values of x1
, x2
in the grid.
Switching to "AdaptiveMonteCarlo" integration method somehow improves the situation, but non-negligible difference in timing (up to a few times) remains (it's funny that it slows down much the integration of int2
without testfunction
).
Could you please tell me how to speed up the integration with int1
?
int1
have to do withint2
? $\endgroup$int1
is slower: In addition to evaluating an interpolation function, it has also to evaluate10^...
andLog10
. $\endgroup$x1
andx2
), keeping also good performance. $\endgroup$