Suppose I have a function f[x]
which is very complicated, together with a function g[f[x]]+h[x]
to integrate. That is:
NIntegrate[{f[x], g[f[x]]+h[x]}, {x,0,1}]
I suppose mathematica will calculate f[x]
and g[f[x]]+h[x]
seperately, thus calculated f[x]
twice. How can I speed up the calculation by telling mathematica calculate f[x]
only once?
A concrete example suggested by the comments:
ClearAll["Global`*"]
f[x_, y_, z_] := Exp[Sin[x]] + Cos[y + z];
NIntegrate[{f[x, y, z], Sqrt[f[x, y, z]] + x}, {x, 0, 10}, {y, 0, 10}, {z, 0, 10},
Method -> {"LocalAdaptive", "SymbolicProcessing" -> 0},
PrecisionGoal -> 7] // Timing
{8.85938, {1429.54, 6081.95 + 59.0571 I}}
g[x_, y_, z_] := g[x, y, z] = Exp[Sin[x]] + Cos[y + z];
NIntegrate[{g[x, y, z], Sqrt[g[x, y, z]] + x}, {x, 0, 10}, {y, 0, 10}, {z, 0, 10},
Method -> {"LocalAdaptive", "SymbolicProcessing" -> 0},
PrecisionGoal -> 7] // Timing
{8.90625, {1429.54, 6081.95 + 59.0571 I}}
In this example the memoization seems not speed up the calcuation...
Comments
Post a Comment