Define a function via numerical integration in Mathematica

In summary: Yes, I have tried to tabulate it. However, the function is highly oscillatory, and the tabulation becomes inaccurate quite quickly.
  • #1
muppet
608
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
  • #2
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}]
 
  • #3
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...
 
  • #5
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
[tex]f(x)=\int_{0}^{\infty} dq e^{q^2x^2-q}=e^{-1/(4x^2)}\frac{\sqrt{\pi}}{2x}erfc(1/2x)[/tex]
-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!)
 
  • #6
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:
  • #7
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
 

What is numerical integration?

Numerical integration is a method for approximating the value of a definite integral using numerical techniques, such as the trapezoidal rule or Simpson's rule. It is commonly used in mathematics and science to solve problems that do not have analytical solutions.

How do you define a function via numerical integration in Mathematica?

To define a function via numerical integration in Mathematica, you can use the NIntegrate function. This function takes the function you want to integrate and the limits of integration as its arguments, and returns an approximate value of the integral. For example, if you want to define a function f(x) that is the integral of x^2 from 0 to x, you could use the code f[x_] := NIntegrate[x^2, {t, 0, x}].

What are the benefits of using numerical integration in Mathematica?

There are several benefits of using numerical integration in Mathematica. Firstly, it allows for the evaluation of integrals that do not have analytical solutions. Additionally, it provides accurate results for complicated integrals that would be difficult to solve by hand. It also allows for the integration of functions with a large number of variables.

What are the limitations of numerical integration in Mathematica?

One limitation of numerical integration in Mathematica is that it can only provide an approximation of the integral, not an exact solution. The accuracy of the approximation also depends on the method used and the number of intervals used in the calculation. Additionally, some integrals may be too complicated for Mathematica to handle, resulting in an error or an incorrect result.

Are there any tips for using numerical integration in Mathematica effectively?

One tip for using numerical integration in Mathematica effectively is to choose the appropriate integration method for your specific problem. Different methods may have varying levels of accuracy and may be more suitable for different types of functions. It is also important to adjust the number of intervals used in the calculation to achieve a desired level of accuracy. Additionally, it can be helpful to check the results of the integration by comparing with a known solution or using a different integration method.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
27
Views
3K
Back
Top