# Is there a better way to approximate some graphs of integrals than interpolation?

I'm still pretty new to Mathematica so my apologies if this is a dumb question. I wanted to plot some integrals of functions for no particularly good reason, but the only decent way I could come up with to do that in Mathematica is by getting an interpolation function from a table of points generated from my original function, and then NIntegrating that interpolation function. This seems awfully fiddly. Is there a better way that I've been too dumb to notice?

Also, I wrote the "procedure" (very heavy quotes) in a block of text so I could use it later, is there a better way to write a simple program like this so that it can be called later without having to copy paste the text and run it again like I'm doing?

domainend = Input["Enter the maximum domain to calculate"]
iterator = Input["Enter the number of steps for each value of x in the domain."]
function = Input["Enter the function whose integral will be approximated."]
grdom1 = Input["Enter leftmost value to graph"]
grdom2 = Input["Enter rightmost value to graph"]
domainstart = (-domainend)

functionvar[x_] =
Interpolation[
N[Transpose[{
(Table[x, {x, domainstart, domainend, iterator}]),
(Table[function, {x, domainstart, domainend, iterator}] /. ComplexInfinity ->0)
}],
30],
x]
diff = NIntegrate[functionvar[x], {x, domainstart, 0}]
antiderivative[x_] = Integrate[functionvar[x], x]
f = Function[x, functionvar[x]]
i = Function[x, antiderivative[x]]
g = Function[x, antiderivative[x] - diff]

Plot[
{f[x], g[x]}, {x, grdom1, grdom2},
PlotStyle -> Thickness[0.0025], PlotLegends -> {function, \!$$\*SubsuperscriptBox[\(\[Integral]$$, $$0$$, $$x$$]$$function \ \[DifferentialD]x$$\)}, Filling -> {1 -> {Axis, LightBlue}}
]

-

domainend = Input["Enter the maximum domain to calculate"]
function =  Input["Enter the function whose integral will be approximated."]
grdom1 =    Input["Enter leftmost value to graph"]
grdom2 =    Input["Enter rightmost value to graph"]

Plot[{function /. x -> t, NIntegrate[function, {x, grdom1, t}]}, {t, grdom1, grdom2},
PlotStyle -> Thickness[0.0025], Filling -> {1 -> {Axis, LightBlue}}]


-

You can use NDSolve to solve the integral, which will return a function you can plot. The differential equation is the one that corresponds to an antiderivative, a'[x] == f[x], with the particular one chosen such that a[0] == 0.

Use Manipulate and InputFields for your input.

Manipulate[
With[{sol = a[x] /.
First @ NDSolve[{a'[x] == function, a[0] == 0}, a, {x, grdom1, grdom2}]},
Plot[{function, sol}, {x, grdom1, grdom2}]],
{{function, Sin[x], "function to integrate"}, InputField},
{{grdom1, -1, "left endpoint"}, InputField},
{{grdom2, 1, "right endpoint"}, InputField}
]


Update

A simple error suppression technique: Quiet. A serious error won't be caught, but many numerical functions issue warnings, that make Manipulate turn red. Look into Check for another way to handle them.

NDSolve returns an InterpolatingFunction, which you query for its domain with sol["Domain"] in this case. This one handles Tan[x]. The integration stops near π/2, and the plot will stop there, too.

Manipulate[
With[{sol = a /.
First @ Quiet @ NDSolve[{int'[x] == function, a[0] == 0}, a, {x, grdom1, grdom2}]},
Plot[{function, sol[x]},
{x, Max[grdom1, sol["Domain"][[1, 1]]], Min[grdom2, sol["Domain"][[1, 2]]]}]],
{{function, Tan[x], "function to integrate"}, InputField},
{{grdom1, -1, "left endpoint"}, InputField},
{{grdom2, 5, "right endpoint"}, InputField}
]

-
Wow, that is much better. I can say with honesty that I have no idea how to use NDSolve. One problem with doing this is that it has a heart attack if you try to integrate Tan[x] or something similar. –  Brendan Jun 25 '13 at 22:47
@Brendan Doesn't anything fail to integrate Tan[x] over an asymptote? There are ways to do error checking/catching/suppression. You will have to work on it to make it "perfect." See the update for a simple fix for Tan[x], but a truly robust program will take considerable work. –  Michael E2 Jun 26 '13 at 1:33
I lazy-ed up a solution to that with my hacked together thing up there. Because I make everything into a list before integrating, I just search the list for singularities and remove them. You end up with a decent looking graph. –  Brendan Jun 26 '13 at 3:00