1
$\begingroup$

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?

$\endgroup$

1 Answer 1

2
$\begingroup$

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  *)
$\endgroup$
1
  • $\begingroup$ 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! $\endgroup$
    – Shane
    Commented Jul 20, 2017 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.