# How do I plot a function subject to a constraint?

While learning about Lagrange multipliers, I am finding examples on how a constraint is applied to a function.

Given the following two functions (where E^ is ::e::):

f[x_, y_] := E^(-(3 x^2 + 2 y^2 - x y)/(3))
g[x_, y_] := x^2 + 2*(y + 1/2)^2 - 1


And the following constraint:

g[x,y]==0


I would like to get a plot of the path of the constraint g[x,y] over the function f[x,y].

I have tried to plot the function f[x,y] with:

f3d = Plot3D[f[x, y], {x, -2, 2}, {y, -2, 2}]


Which results in: And I would like to see the path of the constrain g[x,y]==0 over that surface. Similar to this example:

ContourPlot3D[{x^2 + y^2 + z^2 - 4, (x - 1)^2 + y^2 - 7/8}, {x, -2,
2}, {y, -2, 2}, {z, -2, 2}, ContourStyle -> {Automatic, Opacity},
Mesh -> None,
BoundaryStyle -> {2 -> None, {1, 2} -> {Green, Thick, Dashed}}] But I can not get that plot. I have tried this but I am missing the condition g[x,y]==0 which I do not know how to add:

ContourPlot3D[f[x, y] == g[x, y], {x, -2, 2}, {y, -2, 2}, {z, -2, 2}] In 2D is easier as you can draw both contour plots and then combine, but in 3D I do not know how to do it.

Try MeshFunction

Plot3D[f[x, y], {x, -2, 2}, {y, -2, 2}, MeshFunctions -> Function[{x, y}, g[x, y]], Mesh -> {{0}}] • Or to retain original mesh, Plot3D[f[x, y], {x, -2, 2}, {y, -2, 2}, MeshFunctions -> {#1 &, #2 &, Function[{x, y}, g[x, y]]}, Mesh -> {15, 15, {0}}, MeshStyle -> {Automatic, Automatic, Directive[Red, Thick]}] Sep 15, 2022 at 17:47
• Or use RegionFunction and only display it's boundary.
Plot3D[f[x, y], {x, -2, 2}, {y, -2, 2},
RegionFunction -> Function[{x, y}, g[x, y] <= 0],
BoundaryStyle -> Red, PlotStyle -> None, Mesh -> None] • Or still use ContourPlot3D.
ContourPlot3D[{z - f[x, y] == 0, g[x, y] == 0}, {x, -2, 2}, {y, -2,
2}, {z, -2, 2}, Mesh -> None, ContourStyle -> {Automatic, None},
BoundaryStyle -> {2 -> None, {1, 2} -> {Green, Thick, Dashed}}] • ImplicitRegion also work.
reg = ImplicitRegion[{z == f[x, y],
g[x, y] == 0}, {{x, -2, 2}, {y, -2, 2}, {z, -2, 2}}];
Region[Style[reg, Directive[Thick, Red]], Axes -> True] • Thanks that helps to understand well how plots work.
– M.E.
Sep 15, 2022 at 13:33