# Find region of given value for linear Interpolation function

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.

• RegionMeasure@DiscretizeGraphics@RegionPlot[.5<=f[x,y]<=.7, {x, 1,10},{y,1,10}]?
– kglr
Sep 21, 2018 at 16:40

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


• 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)
– user1722
Sep 21, 2018 at 17:41
• @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. Sep 21, 2018 at 18:02
• @barrycarter If I understand right, this seems related: mathematica.stackexchange.com/a/105804/4999 Sep 22, 2018 at 13:42
• Fantastic! Yes, that's exactly what I was asking!
– user1722
Sep 22, 2018 at 15:58