Mathematica Mathematica: mean of 2d function

AI Thread Summary
The discussion revolves around calculating the average value of a complex 2D function in Mathematica over a specified domain. Initial suggestions include integrating the function over the domain and dividing by the area, but the user faces challenges with long computation times and errors when using the Integrate function. Alternatives like NIntegrate are proposed, but the user encounters convergence issues and singularities in the function, particularly at the point (0,0). Monte Carlo methods are suggested as a practical approach, yielding average values around 1.0. The conversation highlights the difficulties of averaging functions with singularities and the importance of understanding the behavior of the function within the chosen domain. The final consensus indicates that while obtaining an average is challenging due to the singular point, Monte Carlo methods may provide a reasonable estimate.
Sue Laplace
Messages
10
Reaction score
0
Dear all,

I wonder if there is a function implemented in Mathematica to find the average value of a 2d function f(x,y). So averaged over a specified domain.

Thanks in advance for any help!
Sue
 
Physics news on Phys.org
If your function is fairly well behaved then perhaps you could integrate over the domain and then divide by the area of the domain.

If you include an example with your posts that is fairly simple, but which when answered would almost certainly be enough to let you understand and solve your actual problem that seems like it usually helps.
 
Thanks for the reply!

However, I'm afraid my function is too large for integrating, it just doesn't stop running..
 
Depending on your function, perhaps you could try using NIntegrate instead of Integrate for your average calculation.

If that doesn't work and your function fairly well behaved and you want to find the average over a fairly simple domain and probably finite domain and not wildly computationally expensive to evaluate then find the mean of the function at 10^3 or 10^6 or 10^8 randomly selected points in the domain.
 
NIntegrate indeed does give me an answer, though I get two errors, NIntegrate::slwcon: and NIntegrate::ncvb: so apperently my function is not well behaved.
So I do not think I can use the result as I keep getting the ncvb error also for large nr of recursion..

I don't know how interested you are in my problem..., but here is my function. I need the average value on a small domain [x,-1.5,1.5][y,-.5,.5]

\[Sqrt](((-(7/6) + x)/(2 \[Pi] ((-(7/6) + x)^2 + (-2 + y)^2)) -
x/(\[Pi] (x^2 + (-2 + y)^2)) + (7/6 + x)/(
2 \[Pi] ((7/6 + x)^2 + (-2 + y)^2)) + (-(7/6) + x)/(
2 \[Pi] ((-(7/6) + x)^2 + (-1 + y)^2)) -
x/(\[Pi] (x^2 + (-1 + y)^2)) + (7/6 + x)/(
2 \[Pi] ((7/6 + x)^2 + (-1 + y)^2)) + (-(47/6) + x)/(
2 \[Pi] ((-(47/6) + x)^2 + y^2)) + (-(43/6) + x)/(
2 \[Pi] ((-(43/6) + x)^2 + y^2)) - (-6 +
x)/(\[Pi] ((-6 + x)^2 + y^2)) + (-(29/6) + x)/(
2 \[Pi] ((-(29/6) + x)^2 + y^2)) + (-(25/6) + x)/(
2 \[Pi] ((-(25/6) + x)^2 + y^2)) - (-3 +
x)/(\[Pi] ((-3 + x)^2 + y^2)) + (-(11/6) + x)/(
2 \[Pi] ((-(11/6) + x)^2 + y^2)) + (-(7/6) + x)/(
2 \[Pi] ((-(7/6) + x)^2 + y^2)) - x/(\[Pi] (x^2 + y^2)) + (
7/6 + x)/(2 \[Pi] ((7/6 + x)^2 + y^2)) + (11/6 + x)/(
2 \[Pi] ((11/6 + x)^2 + y^2)) - (
3 + x)/(\[Pi] ((3 + x)^2 + y^2)) + (25/6 + x)/(
2 \[Pi] ((25/6 + x)^2 + y^2)) + (29/6 + x)/(
2 \[Pi] ((29/6 + x)^2 + y^2)) - (
6 + x)/(\[Pi] ((6 + x)^2 + y^2)) + (43/6 + x)/(
2 \[Pi] ((43/6 + x)^2 + y^2)) + (47/6 + x)/(
2 \[Pi] ((47/6 + x)^2 + y^2)) + (-(7/6) + x)/(
2 \[Pi] ((-(7/6) + x)^2 + (1 + y)^2)) -
x/(\[Pi] (x^2 + (1 + y)^2)) + (7/6 + x)/(
2 \[Pi] ((7/6 + x)^2 + (1 + y)^2)) + (-(7/6) + x)/(
2 \[Pi] ((-(7/6) + x)^2 + (2 + y)^2)) -
x/(\[Pi] (x^2 + (2 + y)^2)) + (7/6 + x)/(
2 \[Pi] ((7/6 + x)^2 + (2 + y)^2)))^2 + ((-2 + y)/(
2 \[Pi] ((-(7/6) + x)^2 + (-2 + y)^2)) - (-2 +
y)/(\[Pi] (x^2 + (-2 + y)^2)) + (-2 + y)/(
2 \[Pi] ((7/6 + x)^2 + (-2 + y)^2)) + (-1 + y)/(
2 \[Pi] ((-(7/6) + x)^2 + (-1 + y)^2)) - (-1 +
y)/(\[Pi] (x^2 + (-1 + y)^2)) + (-1 + y)/(
2 \[Pi] ((7/6 + x)^2 + (-1 + y)^2)) + y/(
2 \[Pi] ((-(47/6) + x)^2 + y^2)) + y/(
2 \[Pi] ((-(43/6) + x)^2 + y^2)) - y/(\[Pi] ((-6 + x)^2 + y^2)) +
y/(2 \[Pi] ((-(29/6) + x)^2 + y^2)) + y/(
2 \[Pi] ((-(25/6) + x)^2 + y^2)) - y/(\[Pi] ((-3 + x)^2 + y^2)) +
y/(2 \[Pi] ((-(11/6) + x)^2 + y^2)) + y/(
2 \[Pi] ((-(7/6) + x)^2 + y^2)) - y/(\[Pi] (x^2 + y^2)) + y/(
2 \[Pi] ((7/6 + x)^2 + y^2)) + y/(2 \[Pi] ((11/6 + x)^2 + y^2)) -
y/(\[Pi] ((3 + x)^2 + y^2)) + y/(2 \[Pi] ((25/6 + x)^2 + y^2)) +
y/(2 \[Pi] ((29/6 + x)^2 + y^2)) - y/(\[Pi] ((6 + x)^2 + y^2)) +
y/(2 \[Pi] ((43/6 + x)^2 + y^2)) + y/(
2 \[Pi] ((47/6 + x)^2 + y^2)) + (1 + y)/(
2 \[Pi] ((-(7/6) + x)^2 + (1 + y)^2)) - (
1 + y)/(\[Pi] (x^2 + (1 + y)^2)) + (1 + y)/(
2 \[Pi] ((7/6 + x)^2 + (1 + y)^2)) + (2 + y)/(
2 \[Pi] ((-(7/6) + x)^2 + (2 + y)^2)) - (
2 + y)/(\[Pi] (x^2 + (2 + y)^2)) + (2 + y)/(
2 \[Pi] ((7/6 + x)^2 + (2 + y)^2)))^2)
 
Plot3D[yourfunction,{x, -1.5, 1.5}, {y, -.5, .5}]
seems to be fairly well behaved, until you notice that the automated plot range code appears to have clipped off a peak around (0,0).
Plot3D[yourfunction,{x, -1.5, 1.5}, {y, -.5, .5},PlotRange->All]
is amusing and shows a single spike surrounded by a nearly flat plane that indicates you have denominators that go to zero and thus your function goes to infinity. That is going to make an average calculation a little questionable.

Monte Carlo random generation of {x,y} points in your domain and averaging the results over 10^3, 10^4 and 10^5 points gives values of approximately 1.0.

I think that is about the best you are going to do with this
 
I'm not sure if its RIGHT, but with NIntegrate you can specify singularities. I believe yours is at 0,0, so:

AVG = NIntegrate[FF, {x, -3/2, 0, 3/2}, {y, -1/2, 0, 1/2}]/NIntegrate[1, {x, -3/2, 0, 3/2}, {y, -1/2, 0, 1/2}]
I get:
Integrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 18 recursive bisections in x near {x,y} = {1.16666,1.3406*10^-7}. NIntegrate obtained 3.0301 and 6.55317*10^-6 for the integral and error estimates. >>

Which isn't bad, but I don't know if it blows up at higher WorkingPrecision, though I don't think it does. So 3.0301/3 might be your answer? 1.01003.
 
Thank you both!

I suppose indeed with a singular point it is difficult to talk about an average value. It is indeed at [0,0].
Why do you divide the integral by NIntegrate[1,{x,-1.5,0,1.5},{y,-0.5,0,0.5}]?
I suppose you’re right as you both obtain an average value of 1..
 

Similar threads

Replies
1
Views
2K
Replies
13
Views
2K
Replies
4
Views
3K
Replies
5
Views
3K
Replies
0
Views
2K
Replies
4
Views
1K
Back
Top