Define a function via numerical integration in Mathematica

Click For Summary

Discussion Overview

The discussion revolves around defining a function via numerical integration in Mathematica, particularly focusing on handling an integral with an arbitrary real parameter. Participants explore methods for plotting the function, storing results, and addressing issues related to oscillatory behavior in the integrand.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant seeks assistance with plotting a function defined by an integral that includes a non-numerical parameter.
  • Another participant suggests using a pattern in the function definition to ensure it only evaluates for numerical inputs.
  • A participant expresses gratitude for the advice and inquires about storing the function to avoid recalculating it due to long computation times.
  • There is a mention of a tutorial link for storing function values in Mathematica.
  • A participant raises concerns about the limitations of storing values in RAM and seeks a method for permanent storage of results for further manipulation.
  • Issues are noted regarding the accuracy of the numerical version of the integral, particularly its behavior at larger values of the variable.
  • Another participant suggests using Save or DumpSave for permanent storage and recommends increasing precision settings for better results.
  • One participant proposes tabulating the function values as a potential solution to the oscillatory nature of the integrand.

Areas of Agreement / Disagreement

Participants express varying opinions on the best methods for storing function values and addressing numerical integration challenges. No consensus is reached on the most effective approach to manage the oscillatory behavior of the integrand or the best storage solution.

Contextual Notes

Participants note issues with the accuracy of the numerical integration, particularly regarding the behavior of the function at different ranges and the handling of singularities in analytic expressions. There are also mentions of potential limitations in the numerical methods employed.

Who May Find This Useful

This discussion may be useful for Mathematica users dealing with numerical integration, particularly those working with functions defined by integrals that include non-numerical parameters or exhibit oscillatory behavior.

muppet
Messages
602
Reaction score
0
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
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 58 ·
2
Replies
58
Views
7K
  • · Replies 4 ·
Replies
4
Views
4K
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K