0

I am visualizing the extrema of $f(x,y)=x^3+3y^2$ constrained to the level curve $g(x,y)=xy=-4$, found by using the Lagrange Multiplier Method.

DynamicModule[{f, g, cpts, cp},
 f[x_, y_] := x^3 + 3 y^2;
 g[x_, y_] := x y;
 cpts = Solve[{Grad[
      f[x, y], {x, y}] == \[Lambda] Grad[g[x, y], {x, y}], 
    g[x, y] == -4}, {x, y, \[Lambda]}, Reals];
 cp = ContourPlot[f[x, y], {x, -5, 5}, {y, -5, 5},
   Contours -> Range[-120, 180, 5],
   MeshFunctions -> Function[{x, y}, g[x, y]],
   Mesh -> {{-4}},
   MeshStyle -> {Thick, Yellow},
   Epilog -> {Red, PointSize[Large], Point[{x, y} /. cpts]},
   PlotLegends -> Automatic];
 Manipulate[
  Show[cp,
   ContourPlot[f[x, y] == c, {x, -5, 5}, {y, -5, 5},
    ContourStyle -> {Thickness[.008], Black},
    PerformanceGoal -> "Quality",
    PlotLabel -> Row[{"f(x,y) = ", ToString[c]}]],
   PlotRange -> All
   ], {{c, -60}, -70, 70, 5, Appearance -> "Labeled"}]
 ]

Which shows this:

enter image description here

Notice that I tried to put a PlotLabel and the second contour plot, but because it is the second object in the Show command, I guess that is why it did not show up. The line I refer to is:

PlotLabel -> Row[{"f(x,y) = ", ToString[c]}]]

I also tried a PlotRange->All afterwords, but it still did not show up. Now I need to keep my DynamicModule so that this doesn't interfere with similar Manipulate cells in the notebook.

Thanks.

David
  • 14,883
  • 4
  • 44
  • 117

0 Answers0