Mathematica Define a function via numerical integration in Mathematica

Click For Summary
The discussion centers on defining a function via numerical integration in Mathematica, particularly when the integrand includes an arbitrary parameter. Users are advised to define the function with a pattern that only accepts numerical values to facilitate numerical integration. There are inquiries about storing the results of the integral to avoid recalculating, with suggestions to use Save or DumpSave for permanent storage. Issues arise with plotting the function, as it becomes inaccurate for larger values, leading to unexpected negative results. Recommendations include increasing precision settings and exploring tabulation for highly oscillatory functions to improve accuracy.
muppet
Messages
602
Reaction score
1
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?

Thanks in advance.
 
Physics news on Phys.org
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}]
 
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...
 
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 :frown:) 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!)
 
muppet said:
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:
muppet said:
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
 

Similar threads

  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
3K
Replies
5
Views
4K
  • · Replies 58 ·
2
Replies
58
Views
7K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K