8
$\begingroup$

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:

Function f

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[0]},
  Mesh -> None, 
 BoundaryStyle -> {2 -> None, {1, 2} -> {Green, Thick, Dashed}}]

Example

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}]

Try

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.

$\endgroup$

2 Answers 2

9
$\begingroup$

Try MeshFunction

Plot3D[f[x, y], {x, -2, 2}, {y, -2, 2}, MeshFunctions -> Function[{x, y}, g[x, y]], Mesh -> {{0}}]

enter image description here

$\endgroup$
1
  • $\begingroup$ 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]}] $\endgroup$
    – Bob Hanlon
    Sep 15, 2022 at 17:47
8
$\begingroup$
  • 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]

enter image description here

  • 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}}]

enter image description here

  • 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]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ Thanks that helps to understand well how plots work. $\endgroup$
    – M.E.
    Sep 15, 2022 at 13:33

Your Answer

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

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