I have an integral that I want to evaluate it once and store its values in a table so that I don't run the integration each time that I want to plot it.
Its form is int[x_,y_]:=NIntegrate[f[z],{z,x,y}]
with the functional form of f[z]
being irrelevant for the time being and x,y
being in the range of [1,1000]
and specified each time that I plot it. How do I do a table that essentially is a function of x,y
(and takes the values from the table rather than re-integrating everything each time)?
$\begingroup$
$\endgroup$
7
1 Answer
$\begingroup$
$\endgroup$
NDSolve
will compute a numeric antiderivative.
Example:
ClearAll[f];
f[t_] := Re@Zeta[1/2 + Sqrt[t] I];
Plot[f[t], {t, 1, 1000}]
The antiderivative:
ListLinePlot@NDSolveValue[{y'[t] == f[t], y[1] == 0}, y, {t, 1, 1000}]
Here's the definite integral int[x, y]
:
ClearAll[int];
With[{F = NDSolveValue[{y'[t] == f[t], y[1] == 0}, y, {t, 1, 1000}]},
int[x_, y_] := Subtract @@ F[{y, x}]
];
Plot3D[int[x, y], {x, 1, 1000}, {y, 1, 1000}]
For greater accuracy, use
NDSolveValue[{y'[t] == f[t], y[1] == 0}, y, {t, 1, 1000}, InterpolatingOrder -> All]
ints = Table[NIntegrate[f[x], {z, x, y}], {x, 1, 2}, {y, 3, 6}]
. Either that, or memoize:int[x_, y_] := int[x, y] = NIntegrate[f[z],{z,x,y}]
, but I recommend the first. $\endgroup$x=1
toy=1000
? $\endgroup$x = 1
toy = 1000
", because those are two different variables. $\endgroup$ints[a,b]
and I want to be able to do plots likePlot[ints[1,r],{r,1,1000}]
andPlot[ints[r,1],{r,1,1000}]
. Is something like that possible? Thank you $\endgroup$Interpolation
:data = Table[{{x, y}, NIntegrate[f[z], {z, x, y}]}, {x, 1, 1000, 999/99}, {y, 1, 1000, 999/99}]; ints = Interpolation[data];
$\endgroup$