Mathematica: mean of 2d function

Click For Summary

Discussion Overview

The discussion revolves around finding the average value of a two-dimensional function \( f(x,y) \) using Mathematica, specifically over a specified domain. Participants explore various methods for calculating this average, considering the challenges posed by the function's complexity and behavior.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant suggests integrating the function over the domain and dividing by the area, assuming the function is well-behaved.
  • Another participant proposes using NIntegrate instead of Integrate for functions that are computationally intensive, recommending random sampling of points in the domain to estimate the average if the function is not well-behaved.
  • A participant reports that NIntegrate provides results but encounters errors indicating the function is not well-behaved, particularly near singularities.
  • One participant notes that the function appears to have a spike when visualized, suggesting that the presence of singularities complicates the average calculation.
  • Another participant mentions the possibility of specifying singularities in NIntegrate and provides a formula for calculating the average, although they express uncertainty about its correctness.
  • There is a discussion about the implications of having a singular point at (0,0) and how it affects the average value calculation.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to calculate the average value, with some advocating for numerical integration methods and others highlighting the complications introduced by singularities. No consensus is reached on a definitive method or solution.

Contextual Notes

Participants note limitations related to the function's behavior, particularly the presence of singularities and computational challenges that arise during integration. The discussion reflects varying assumptions about the function's properties and the implications for averaging.

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 ·
Replies
1
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 0 ·
Replies
0
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K