3
$\begingroup$

Consider a function, z (x,y) = x^2 + y^2 + 3*x*y. For a range of x, I like to plot the minimum values of z. Specifically, suppose, when x = 1, we get z(1,y) = 1 + y^2 + 3y. And the function has a minimum at y=y_c=-3/2that is z_min(1)= z(1, -3/2) = -5/4.
This way, I like to plot all such z_min(x) in the range -10<=x<=10.

I am implementing it in the following way:

z = x^2 + y^2 + 3*x*y;
dzdy = D[z, y];
soln = Solve[dzdy == 0, y];
yc = y /. soln[[1]];
zmin = z /. y -> yc;
Plot[zmin, {x, -10, 10}, 
   AxesLabel -> {Style[Text["x"], 16], Style[Row[{Subscript["z", "min"], "(x) = z (x, ", Subscript["y","c"], "(x))"}], 16, Blue]},
   PlotStyle -> Blue]

I like to know if the coding can be written in an efficient and compact manner to obtain my desired plot. Does there exist dedicated built-in functions to plot such minima?

$\endgroup$

3 Answers 3

5
$\begingroup$

When the x is fixed, then we set {D[z, y] == 0, D[z, {y, 2}] >= 0} to get the local minimum.

f[x_, y_] := x^2 + y^2 + 3*x*y;
min = Plot3D[f[x, y], {x, -10, 10}, {y, -40, 40}, 
   MeshFunctions -> {Function[{x, y, z}, D[f[x, y], y] // Evaluate]}, 
   RegionFunction -> 
    Function[{x, y, z}, D[f[x, y], {y, 2}] > 0 // Evaluate], 
   Mesh -> {{0}}, MeshStyle -> Directive@{Thick, Red}, 
   PlotStyle -> None, BoundaryStyle -> None];
plot3D = 
  Show[Plot3D[f[x, y], {x, -10, 10}, {y, -40, 40}, Mesh -> None], min,
    PlotRange -> All, ViewProjection -> "Orthographic", 
   BoxRatios -> 1];
reg = ParametricRegion[{{x, f[x, y]}, {D[f[x, y], y] == 0, 
     D[f[x, y], {y, 2}] >= 0, -10 <= x <= 10}}, {x, y}];
plot2D = Region[Style[reg, Red], Axes -> True, AspectRatio -> 1];
{plot3D, plot2D}

enter image description here

  • For the function f[x_,y_]:=x^2+y^2-3*x*y-x^2*y-x*y^2;
Clear["Global`*"];
f[x_, y_] := x^2 + y^2 + 3*x*y - x^2*y - x*y^2;
reg3d = ParametricRegion[{{x, y, f[x, y]}, {D[f[x, y], y] == 0, 
     D[f[x, y], {y, 2}] >= 0, -10 <= x <= 10, -20 <= y <= 20}}, {x, 
    y}];
plot3d = 
  HighlightMesh[
   DiscretizeRegion[reg3d, Axes -> True, BoxRatios -> 1, 
    PlotRange -> {{-10, 10}, {-20, 20}, {-50, 50}}], {Style[0, None], 
    Style[1, Directive@{Thick, Red}]}, ViewPoint -> Front, 
   ViewProjection -> "Orthographic"];
plot = Show[Plot3D[f[x, y], {x, -10, 10}, {y, -20, 20}, Mesh -> None],
    plot3d, BoxRatios -> 1];
reg = ParametricRegion[{{x, f[x, y]}, {D[f[x, y], y] == 0, 
     D[f[x, y], {y, 2}] >= 0, -10 <= x <= 10, -20 <= y <= 20}}, {x, 
    y}];
plot2D = 
  Region[Style[reg, Red], Axes -> True, AspectRatio -> 1, 
   PlotRange -> {{-10, 10}, {-50, 50}}];
{plot, plot3d, plot2D}

enter image description here

f[x_, y_] := x^2 + y^2 + 3*x*y - x^2*y - x*y^2;
Plot3D[f[x, y], {x, -10, 10}, {y, -20, 20}, 
 MeshFunctions -> Function[{x, y, z}, Evaluate@D[f[x, y], y]], 
 Mesh -> {{0}}, MeshStyle -> Red, 
 RegionFunction -> 
  Function[{x, y, z}, Evaluate[D[f[x, y], {y, 2}] >= 0]], 
 PlotPoints -> 80, MaxRecursion -> 4, BoundaryStyle -> None, 
 ClippingStyle -> None, Axes -> True, BoxRatios -> 1, 
 PlotRange -> {{-10, 10}, {-20, 20}, {-50, 50}}, PlotStyle -> None, 
 ViewPoint -> Front, ViewProjection -> "Orthographic"]
$\endgroup$
7
  • $\begingroup$ Thank you. In my MWE, I missed to consider taking the 2nd derivate positive. $\endgroup$
    – raf
    Commented Jun 26 at 8:17
  • $\begingroup$ Kindly recheck, when the x is fixed, the minima of z is at y == -((3 x)/2), isn't it? So, shouldn't it be sol = Reduce[{D[z, y] == 0, D[z, {y, 2}] >= 0}, {x, y}]? For example, for x=1, your value should be -5/4 whereas ur code gives -5/9. Check my question and @ubpdqn's answer. $\endgroup$
    – raf
    Commented Jun 26 at 10:59
  • 1
    $\begingroup$ @raf You are right. The y is a variable when x is fixed. $\endgroup$
    – cvgmt
    Commented Jun 26 at 11:31
  • $\begingroup$ Thanks for the update. Instead of specifying the condition of y == -((3 x)/2) in Plot3D, Can the code be made such that it is computed and used internally, like implementing the sol somehow? $\endgroup$
    – raf
    Commented Jun 26 at 11:43
  • $\begingroup$ Also, on the line Plot[x^2 + y^2 + 3*x*y /. ToRules[sol], {x, -10, 10}], isn't it a bit redundant to state the function again instead of just calling by z? :) $\endgroup$
    – raf
    Commented Jun 26 at 12:07
3
$\begingroup$
z = x^2 + y^2 + 3*x*y;
pt = {x, y, z} /. Minimize[{z, x == 1}, {x, y}][[2]];
Show[Plot3D[z, {x, -2, 2}, {y, -2, 2}, Mesh -> None], 
 Graphics3D[{Opacity[0.4], 
   InfinitePlane[{{1, 0, 0}, {1, 1, 0}, {1, 0, 1}}], Opacity[1], Red, 
   PointSize[0.04], Point[pt], Black, 
   Text[pt, pt, {-1, -2}, Background -> LightBlue]}]]

enter image description here

Update: based on comment

z = x^2 + y^2 + 3*x*y;
min = Minimize[{z, x == a}, {x, y}][[2]];
p = {x, z} /. min
q = {x, y, z} /. min;
ParametricPlot[p, {a, -10, 10}, AspectRatio -> 1]
Show[Plot3D[z, {x, -10, 10}, {y, -10, 10}, MeshFunctions -> (#1 &), 
  Mesh -> 20], ParametricPlot3D[q, {a, -10, 10}, PlotStyle -> Red], 
 AxesLabel -> {"x", "y", "z"}]

enter image description here

$\endgroup$
4
  • $\begingroup$ The target is to obtain a 2D plot of z_min(x) vs x. $\endgroup$
    – raf
    Commented Jun 26 at 7:30
  • $\begingroup$ The way you have defined pt, I like to plot all z for different values ofx in the range -10<=x<=10. $\endgroup$
    – raf
    Commented Jun 26 at 7:43
  • $\begingroup$ Thank you for the update $\endgroup$
    – raf
    Commented Jun 26 at 8:44
  • $\begingroup$ Can you please look at this question too? It's related to the same topic. mathematica.stackexchange.com/q/305184/83038 $\endgroup$
    – raf
    Commented Jul 14 at 9:12
2
$\begingroup$

If I understand your question correctly, you want to plot z against x where z[x,y] has a minimum for given x. This can be done by noting that the partial derivative of f[x,y] relative to y by keeping x fix is zero. This will determine y: ymin. And with ymin we can define zmin[x] = z[x,ymin[x]]:

z[x_, y_] = x^2 + y^2 + 3*x*y;
ymin = y /. Solve[D[z[x, y], y] == 0, y][[1]];
zmin[x_] = z[x, ymin];
Plot[zmin[x], {x, -10, 10}]

enter image description here

Addendum

To create a 3D plot, you would create a 3D plot of z[x,y] and a parametric plot of {x,ymin,zmin[x]} and combine both using Show:

gr1 = Plot3D[z[x, y], {x, -10, 10}, {y, -10, 10}];
gr2 = ParametricPlot3D[{x, ymin, zmin[x]}, {x, -10, 10}];
Show[gr1, gr2]

enter image description here

$\endgroup$
10
  • $\begingroup$ Can you generate a 3D plot of it too with this method? $\endgroup$
    – raf
    Commented Jun 26 at 9:22
  • $\begingroup$ Also, it's not required for this function, it would be wise to check the condition of 2nd derivative to be positive too, right? $\endgroup$
    – raf
    Commented Jun 26 at 9:25
  • $\begingroup$ Simply take 'D[zmin[x],{x,2}]' and ckeck if it is negative. $\endgroup$ Commented Jun 26 at 9:32
  • $\begingroup$ I understand that. But I wanted to understand how to add the extra condition into the definition of your ymin. $\endgroup$
    – raf
    Commented Jun 26 at 10:04
  • 1
    $\begingroup$ You are right, I screwed up, done in a hurry. fixed it $\endgroup$ Commented Jun 26 at 14:38

Your Answer

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

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