# Mathematica: mean of 2d function

1. Jun 7, 2012

### Sue Laplace

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

2. Jun 7, 2012

### Bill Simpson

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.

3. Jun 8, 2012

### Sue Laplace

However, I'm afraid my function is too large for integrating, it just doesn't stop running..

4. Jun 8, 2012

### Bill Simpson

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.

5. Jun 8, 2012

### Sue Laplace

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)

6. Jun 8, 2012

### Bill Simpson

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

7. Jun 8, 2012

### Hepth

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 isnt bad, but I dont know if it blows up at higher WorkingPrecision, though I dont think it does. So 3.0301/3 might be your answer? 1.01003.

8. Jun 9, 2012

### Sue Laplace

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..