I have an optimization problem as follows and I want to solve it with the Lagrange method. Is there a special command?
2 Answers
$\begingroup$
$\endgroup$
2
Try
J = x + y + 2 z + \[Lambda] (x^2 + y^2 + z^2 - 3) (* Lagrange parameter `\[Lambda] ` *)
sol = Solve[D[J, {{x, y, z, \[Lambda]}}] == 0, {x, y, z, \[Lambda]}]
Two solutions, check for maximum
x + y + 2 z /.sol[[1]] (* 3 Sqrt[2]*)
x + y + 2 z /.sol[[2]] (* -3 Sqrt[2]*)
First solution {x -> 1/Sqrt[2], y -> 1/Sqrt[2], z -> Sqrt[2], \[Lambda] -> -(1/Sqrt[2])}
is maximum!
-
$\begingroup$ Thanks for the advice. How to draw? (determining the feasible region and max point) $\endgroup$– ErfanCommented Oct 19 at 15:58
-
$\begingroup$ For this 3D problem try
DensityPlot3D
to plot J $\endgroup$ Commented Oct 21 at 14:07
$\begingroup$
$\endgroup$
2
Just for illustrative purposes:
f[x_, y_, z_] := x + y + 2 z
g[x_, y_, z_] := x^2 + y^2 + z^2 - 3
sol = Solve[{Grad[f[x, y, z], {x, y, z}] ==
m Grad[g[x, y, z], {x, y, z}], g[x, y, z] == 0}, {x, y, z, m}]
ex = f[x, y, z] == # & /@ (f[x, y, z] /. sol);
pt = {x, y, z} /. sol;
Show[Graphics3D[{Opacity[0.4], Sphere[{0, 0, 0}, Sqrt[3]], Opacity[1],
Red, PointSize[0.02], Point[pt]}],
ContourPlot3D[ex, {x, -2, 2}, {y, -2, 2}, {z, -2, 2}, Mesh -> None,
ContourStyle -> Opacity[0.5]]]
You can, of course, use Maximize
also, e.g.:
Maximize[{f[x, y, z], g[x, y, z] == 0}, {x, y, z}]
-> {3 Sqrt[2], {x -> 1/Sqrt[2], y -> 1/Sqrt[2], z -> Sqrt[2]}}
-
$\begingroup$ The code comes with an error "Could not combine the graphics objects in Show". Can you correct it? $\endgroup$– ErfanCommented Oct 19 at 16:02
-
$\begingroup$ @Erfan I am using version 14.1 and it runs without error. I have just checked. I would also run fresh kernel in case there are conflicts with previous code. $\endgroup$– ubpdqnCommented Oct 20 at 1:10