0
$\begingroup$

I have an optimization problem as follows and I want to solve it with the Lagrange method. Is there a special command?

enter image description here

$\endgroup$
2
  • 4
    $\begingroup$ There are a few questions here and here related to using Lagrange multipliers. $\endgroup$
    – ydd
    Commented Oct 16 at 14:47
  • $\begingroup$ And the first one here. $\endgroup$
    – Artes
    Commented Oct 18 at 11:49

2 Answers 2

3
$\begingroup$

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!

$\endgroup$
2
  • $\begingroup$ Thanks for the advice. How to draw? (determining the feasible region and max point) $\endgroup$
    – Erfan
    Commented Oct 19 at 15:58
  • $\begingroup$ For this 3D problem try DensityPlot3D to plot J $\endgroup$ Commented Oct 21 at 14:07
0
$\begingroup$

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

enter image description here

$\endgroup$
2
  • $\begingroup$ The code comes with an error "Could not combine the graphics objects in Show". Can you correct it? $\endgroup$
    – Erfan
    Commented 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$
    – ubpdqn
    Commented Oct 20 at 1:10

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