I'm having some difficulties implementing a minimization problem were the objective function involves a numeric integration. This simplified problem highlights one of the issues I'm currently having: how to find the location of the minimum average value of a function. For this problem assume the function can be described by
exactfun[x_?NumericQ, y_?NumericQ] := (x - .5)^4 + (y - .5)^4
I want to minimize the average value of the function over a disk region centered at $\left(a,b\right)$ with radius $r$. The exact value of $f_{avg} \left(a,b,r\right)$ can be determined explicitly for this particular function, though in general (and in my application), it is not possible. Here, $f_{avg} \left(a,b,r\right)$ is
FullSimplify[
Integrate[(x - 1/2)^4 + (y - 1/2)^4, {x, y} \[Element]
Disk[{a, b}, r],
Assumptions -> {{a, b} \[Element] Reals && r > 0}]/
Area@Disk[{a, b}, r]]
(*1/8 (1 + 4 (-1 + b) b (1 + 2 (-1 + b) b) + 6 r^2 +
2 (2 (-1 + a) a (1 + 2 (-1 + a) a) +
6 ((-1 + a) a + (-1 + b) b) r^2 + r^4))*)
Minimizing with respect to the disk center yields
Solve[{D[1/
8 (1 + 4 (-1 + b) b (1 + 2 (-1 + b) b) + 6 r^2 +
2 (2 (-1 + a) a (1 + 2 (-1 + a) a) +
6 ((-1 + a) a + (-1 + b) b) r^2 + r^4)), a] == 0,
D[1/8 (1 + 4 (-1 + b) b (1 + 2 (-1 + b) b) + 6 r^2 +
2 (2 (-1 + a) a (1 + 2 (-1 + a) a) +
6 ((-1 + a) a + (-1 + b) b) r^2 + r^4)), b] == 0}, {a,
b}, Reals]
(*{{a -> 1/2, b -> 1/2}}*)
I'm interested in doing this same problem numerically. However, in the evaluation of NMinimize there is an error regarding integration limits that I can't sort out. I tried constructing the optimization function two ways, and each failed.
numericint[a_?NumericQ, b_?NumericQ, r_?NumericQ] :=
NIntegrate[exactfun[x, y], {x, y} \[Element] Disk[{a, b}, r]]/
Area@Disk[{a, b}, r]
numericint2[a_?NumericQ, b_?NumericQ, r_?NumericQ] :=
Module[{disk = Disk[{a, b}, r]},
NIntegrate[exactfun[x, y], {x, y} \[Element] disk,
Method -> {Automatic, "SymbolicProcessing" -> False}]/Area@disk]
Running the optimization for a particular $r$ and in a certain region fails
NMinimize[
numericint[x, y, .05], {x, y} \[Element] Rectangle[{0, 0}, {1, 1}]]
(*NIntegrate::ilim: Invalid integration variable or limit(s) in True.*)
NMinimize[
numericint2[x, y, .05], {x, y} \[Element] Rectangle[{0, 0}, {1, 1}]]
(*NIntegrate::ilim: Invalid integration variable or limit(s) in True.*)
Each of the functions appear to evaluate fine outside of the NMinimize environment, but are crashing and burning during the minimization. For reference:
numericint[.25, .25, .05]
(*0.00828281*)
numericint2[.25, .25, .05]
(*0.00828281*)
1/8 (1 + 4 (-1 + b) b (1 + 2 (-1 + b) b) + 6 r^2 +
2 (2 (-1 + a) a (1 + 2 (-1 + a) a) +
6 ((-1 + a) a + (-1 + b) b) r^2 + r^4)) /. {a -> .25, b -> .25,
r -> .05}
(*0.00828281*)