# Define a function via numerical integration in Mathematica

• Mathematica
muppet
Hi all,
I have a function defined by an integral containing an arbitrary real parameter q. I can't do the integral, so I wanted to plot the function by evaluating the integral numerically. However, I can't do this owing to the arbitary-non numerical parameter in my integrand! Can anyone tell me how to do this?

Simon_Tyler
You need to put a pattern on you function definition so that it only fires when given a numerical value. For example:

f[q_?NumericQ] := NIntegrate[Exp[-x^2], {x, 0, q}]
Plot[f[q], {q, -10, 10}]

muppet
This was extremely helpful, thank you!

As a further question, is it possible to "store" the function defined by my integral so that I don't have to recalculate it every time I want to do something with it? My integrand is highly oscillatory, it takes a good few hours to chew it over...

muppet
Thanks DaleSpam. It seems though as if that method only stores the value in the RAM- is there a way of permanently enshrining it? What I really want to do is work out the function (which is, incidentally, highly oscillatory ) over some range of values once, and keep a permanent record of the result, preferably in some kind of form I could manipulate as I would any other function.

I also encountered the following problem. My notebook contains three expressions for the function
$$f(x)=\int_{0}^{\infty} dq e^{q^2x^2-q}=e^{-1/(4x^2)}\frac{\sqrt{\pi}}{2x}erfc(1/2x)$$
-the left and right hand sides of the second equation, and a numerical version of the integral.

The numerical version is the one I'm most interested in, so that I can apply this method to my real problem rather than this simple one (where both Mathematica and I can do the integral exactly). When I try and plot it, it's accurate for small values of x but towards the end of whatever range of variables I'm considering it always becomes spuriously negative. The point at which it crosses the x-axis varies depending on what range of x I consider, and moreover plotting for the same range of x always produces the same graph, even after having previously calculated it for a larger region using the f[x_]:=f[x]=... prescription. Could someone please explain to me what's going on?

(As an aside, I've also encountered difficulties plotting and evaluating the two analytic expressions; the closed form solution doesn't identify that the "singularity" at x=0 is removable; whilst plotting the function defined by the integral expression yields values on the order of 10^80 for some small values of x... Mathematica is not my friend at the moment!)

Mentor
2021 Award
It seems though as if that method only stores the value in the RAM- is there a way of permanently enshrining it?
You can use Save or DumpSave

Regarding the rest, you can do things like increasing the precision or the working precision. Also there are various options that you can set, such as LocalAdaptive, that might help. There is a whole tutorial on numerical integration. I would recommend you read the entire thing:
http://reference.wolfram.com/mathematica/tutorial/NIntegrateOverview.html

Last edited:
hanciong
This was extremely helpful, thank you!

As a further question, is it possible to "store" the function defined by my integral so that I don't have to recalculate it every time I want to do something with it? My integrand is highly oscillatory, it takes a good few hours to chew it over...

have you tried to make tabulation of it? although the function is highly oscillatory, I think as long as you make fine enough tabulation, you are ok