Mathematica: mean of 2d function

In summary, the average value of a 2d function f(x,y) over the domain (-1.5,1.5) is sqrt((-(7/6) + x)/(2 \[Pi] ((-(7/6) + x)^2 + (-2 + y)^2)) - x/(\[Pi] (x^2 + (-2 + y)^2)) + (7/6 + x)/(
  • #1
Sue Laplace
11
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
  • #2
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
Thanks for the reply!

However, I'm afraid my function is too large for integrating, it just doesn't stop running..
 
  • #4
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
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
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
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.
 
  • #8
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..
 

Related to Mathematica: mean of 2d function

1. What is the purpose of calculating the mean of a 2d function in Mathematica?

The mean of a 2d function in Mathematica allows you to determine the average value of the function over a specified region, providing valuable insights into the overall behavior of the function.

2. How do I calculate the mean of a 2d function in Mathematica?

To calculate the mean of a 2d function in Mathematica, you can use the built-in Mean function and specify the function and region of interest as parameters. Alternatively, you can use the Integrate function to find the integral of the function over the specified region and then divide by the area of the region.

3. Can I calculate the mean of a 2d function with multiple variables in Mathematica?

Yes, you can calculate the mean of a 2d function with multiple variables in Mathematica. Simply specify all the variables in the function and the region of interest when using the Mean or Integrate functions.

4. What is the difference between the mean and average of a 2d function in Mathematica?

In Mathematica, the mean and average of a 2d function are essentially the same thing. They both refer to the average value of the function over a specified region. However, the Mean function in Mathematica specifically calculates the mathematical mean, while the Average function calculates the arithmetic mean.

5. Can I visualize the mean of a 2d function in Mathematica?

Yes, you can visualize the mean of a 2d function in Mathematica using the Plot3D function. This will create a 3D plot of the function over the specified region, with the mean value represented as a plane or surface in the plot.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
434
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
158
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
778
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
914
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
908
Back
Top