2
$\begingroup$

Summary: How do I find the (possibly disconnected) region(s) where a linear interpolating function is between two specified values?

Example:

(* this creates a 10x10 grid of random real numbers and a function to
linearly interpolate them *)

rand = Table[{i, j, RandomReal[]}, {i, 1, 10}, {j, 1, 10}];

f = Interpolation[Flatten[rand,1], InterpolationOrder -> 1]

(*

I want the region(s) where f is between 0.5 and 0.7, for example.

If it's not easy to find the regions, I'd like to get the area of
where the function is between those two values.

However, because I plan to treat the region in a non-Euclidean way, I
actually need the region more than I need the area

*)

Goal: I have the "distance from coastline" data for a grid of latitude/longitude points. I want to find the total area where the distance from coastline rounds to, say, 492km (ie, is between 491.5km and 492.5km). Since I only have data for a finite number of points, I'm interpolating to find values for all points. I realize this interpolation is inaccurate, potentially very inaccurate. I'm OK with treating the Earth as a sphere even though it's actually an ellipsoid.

I realize I could generalize this question to ask "how do I find the [area of] the region where a given function is in a given range", but I think/hope the fact the function is both an interpolation and is linear might make the problem significantly easier.

$\endgroup$
  • 1
    $\begingroup$ RegionMeasure@DiscretizeGraphics@RegionPlot[.5<=f[x,y]<=.7, {x, 1,10},{y,1,10}]? $\endgroup$ – kglr Sep 21 '18 at 16:40
4
$\begingroup$

Use ImplicitRegion to describe the region of interest and DiscretizeRegion to get a discrete numerical version thereof:

SeedRandom[42];
rand = Table[{i, j, RandomReal[]}, {i, 1, 10}, {j, 1, 10}];
f = Interpolation[Flatten[rand, 1], InterpolationOrder -> 1];
reg = ImplicitRegion[
   0.5 <= f[x, y] <= 0.7, {{x, 1, 10}, {y, 1, 10}}];
DiscretizeRegion@reg

enter image description here

$\endgroup$
  • $\begingroup$ This is really nice, thanks! If I want to know where f is between .7 and .9, .1 and .3, and several other nonoverlapping ranges, is there a faster way or should I just repeat the technique you gave for each value? I realize I can coerce ContourPlot into drawing this, but that's not quite the same thing (although I could use the generated image as a Raster... hmm) $\endgroup$ – barrycarter Sep 21 '18 at 17:41
  • $\begingroup$ @barrycarter - if you want them as separate regions then I don't know a way to do so without repeating this procedure. If you want them combined as one region, you could make the first argument to ImplicitRegion be an Or statement. $\endgroup$ – Jason B. Sep 21 '18 at 18:02
  • 1
    $\begingroup$ @barrycarter If I understand right, this seems related: mathematica.stackexchange.com/a/105804/4999 $\endgroup$ – Michael E2 Sep 22 '18 at 13:42
  • $\begingroup$ Fantastic! Yes, that's exactly what I was asking! $\endgroup$ – barrycarter Sep 22 '18 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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