1

I'm having some trouble with evaluation order within NIntegrate. Here's a simple example:

pts = {{-0.2, -0.2}, {0.2, 0.2}};
NIntegrate[Nearest[pts-> "Distance", {x, y}] , {x, y} \[Element] Disk[]]
(*Nearest: The default distance does not give a real numeric distance when 
applied to the point pair {x,y} and {-0.2,-0.2}.*)

Is there some way I can get the numerical integration to plug the values of x and y into Nearest rather than passing the variables themselves?

Shane
  • 1,003
  • 1
  • 6
  • 18

1 Answers1

2

This is nearly a "duplicate" of What are the most common pitfalls awaiting new users? (link is to the ?NumericQ answer) -- please see it for explanation. In this case, there are a few other things to add to make the integration work efficiently:

pts = {{-0.2, -0.2}, {0.2, 0.2}};
Block[{f, nf},
 nf = Nearest[pts -> "Distance"]; (* precompute NearestFunction *)
 f[x_?NumericQ, y_?NumericQ] :=   (* NumericQ prevents evaluation on symbolic x, y *)
   First@nf[{x, y}];              (* Nearest returns a list; extract distance with First *)
 NIntegrate[f[x, y], {x, y} ∈ Disk[]]
 ]
(*  1.69082  *)
Michael E2
  • 235,386
  • 17
  • 334
  • 747
  • I read some of that thread quite a while back. At the time I didn't understand much of it. Now I see what a great resource it is. I guess I've only just attained the sophistication of a new user. Before I was just a n00b. Anyway, many thanks! – Shane Jul 20 '17 at 18:52