0
$\begingroup$

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)?

$\endgroup$
7
  • $\begingroup$ Instead of defining the function, define a table: 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$
    – march
    Commented Jan 8, 2019 at 17:03
  • $\begingroup$ Ok, if I go for the first method, how to I plot it afterwards from say x=1 to y=1000? $\endgroup$
    – hal
    Commented Jan 8, 2019 at 17:10
  • $\begingroup$ I don't understand what you mean by "plot it from x = 1 to y = 1000", because those are two different variables. $\endgroup$
    – march
    Commented Jan 8, 2019 at 17:11
  • $\begingroup$ Yeah my mistake I didn't explain it properly. By defining the table essentially I have a function ints[a,b] and I want to be able to do plots like Plot[ints[1,r],{r,1,1000}] and Plot[ints[r,1],{r,1,1000}]. Is something like that possible? Thank you $\endgroup$
    – hal
    Commented Jan 8, 2019 at 17:19
  • 1
    $\begingroup$ Use 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$
    – Bob Hanlon
    Commented Jan 8, 2019 at 18:03

1 Answer 1

1
$\begingroup$

NDSolve will compute a numeric antiderivative.

Example:

ClearAll[f];
f[t_] := Re@Zeta[1/2 + Sqrt[t] I];

Plot[f[t], {t, 1, 1000}]

enter image description here

The antiderivative:

ListLinePlot@NDSolveValue[{y'[t] == f[t], y[1] == 0}, y, {t, 1, 1000}]

enter image description here

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}]

enter image description here

For greater accuracy, use

NDSolveValue[{y'[t] == f[t], y[1] == 0}, y, {t, 1, 1000}, InterpolatingOrder -> All]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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