# Using RegionFunction to constrain the surface shown by a 3D plot

I'm working with an equation where the portion of the surface $z=x^2+2+\sin(3x-2y)$ lies above the region inside the circle $(x-2)^2+(y+4)^2=1$.

My code is:

Clear[x, y]

eq2 = x^2 + 2 + Sin[3*x - 2*y];
eq3 = (x - 2)^2 + (y + 4)^4 == 1;
ploteq2 =
Plot3D[x^2 + 2 + Sin[3x - 2y], {x, -4, 10}, {y, -5, -4},
PlotPoints -> 50, Mesh -> None, PlotStyle ->
Purple, RegionFunction -> ((#1 - 2)^2 + (#2 + 4)^4 ≤ 1 &)]


There doesn't seem to be anything wrong with the output as my graph is being displayed, but it's only the range I'm having difficulty adjusting. No matter what I try for y range (or x range), it either shrinks or I get a blank plot. Also, when I look at the plot over the x and y ranges I have chosen, it doesn't look like the partial surface is inside the circle.

## 3 Answers

A method that is slightly less fiddly than using RegionFunction would be to construct an ImplicitRegion[], and use that in Plot3D[]:

reg = ImplicitRegion[(x - 2)^2 + (y + 4)^4 <= 1, {x, y}];
Plot3D[x^2 + 2 + Sin[3 x - 2 y], {x, y} ∈ reg] In fact, restricting to a disk can be done more compactly, if you know the center and the radius of a disk:

Plot3D[x^2 + 2 + Sin[3 x - 2 y], {x, y} ∈ Disk[{2, -4}, 1]]


This also happens to be much faster.

• This was actually how it's supposed to look, as the it is suppose to be circular. I was fiddly with the range and couldn't get the correct shape, but you got it right. – mastud89 Mar 19 '18 at 22:23
rp = RegionPlot3D[(x - 2)^2 + (y + 4)^2 <= 1, {x, -4, 10}, {y, -5, -4}, {z, -2, 100},
Mesh -> None, PlotStyle -> Opacity[.5, LightBlue], BoundaryStyle -> None];
p3doutside = Plot3D[x^2 + 2 + Sin[3 x - 2 y], {x, -4, 10}, {y, -5, -4},
PlotPoints -> 50, Mesh -> None, PlotStyle -> Opacity[.5, Orange],
RegionFunction -> ((#1 - 2)^2 + (#2 + 4)^2 >= 1 &)];
p3dinside = Plot3D[x^2 + 2 + Sin[3 x - 2 y], {x, -4, 10}, {y, -5, -4},
PlotPoints -> 50, Mesh -> None, PlotStyle -> Purple,
RegionFunction -> ((#1 - 2)^2 + (#2 + 4)^2 <= 1 &)]

Show[rp, p3doutside, p3dinside  ,
PlotRange -> {{-2, 4}, {-5, -4}, {0, 30}}, Lighting -> "Neutral"] Show[p3dinside, PlotRange -> {{0, 4}, {-5, -4}, {0, 30}}, Lighting -> "Neutral"] • please, correct your code related with the equation of the circle bounding the plot ;)) – José Antonio Díaz Navas Feb 22 '18 at 10:37
• Thank you @Jose. Done. – kglr Feb 22 '18 at 17:55

I think that basically, you have to play with the BoxRatios to scale the axis range proportionally. The problem here is the high ratio between them. I mean, one axis has a high range that minimises the length of the others, thus hindering the visualisation. Oh, there is also an error in your code relating the equation of the circle:

Plot3D[x^2 + 2 + Sin[3 x - 2 y], {x, 1, 3}, {y, -5, -4},
PlotPoints -> 50, Mesh -> None, PlotRange -> All,
RegionFunction -> ((#1 - 2)^2 + (#2 + 4)^2 <= 1 &),
PlotStyle -> Cyan, ImageSize -> Medium, BoxRatios -> #] & /@ {{1, 1/2, 10}, {1, 1/2, 1}} 