       # Re: optimize

```Daniel Lichtblau wrote:

> You could use a Lagrange multiplier. As a matter of programming it can
> be tricky to get all the equations into the correct format. I show one
> way to do this below.
>
> surf = x^2 + 2*y^2 + 4*z^2 - 17;
> vars = {x,y,z};
> grad1 = lambda*Map[D[surf,#]&,vars]; cost = x + 3*y + 5*z;
> grad2 = Map[D[cost,#]&,vars];
>
> Now it is simple to eliminate lambda and solve for {x,y,z}.
>
> In:= InputForm[soln = Solve[eqns, vars, lambda]] Out//InputForm=
>   {{x -> -2*Sqrt[17/47], y -> -3*Sqrt[17/47], z -> (-5*Sqrt[17/47])/2},
>    {x -> 2*Sqrt[17/47], y -> 3*Sqrt[17/47], z -> (5*Sqrt[17/47])/2}}

Here is a slight variation on this:

In:= surf = x^2 + 2*y^2 + 4*z^2 - 17;  In:= cost = x + 3*y + 5*z;

Compute the (polynomial variables):

In:= vars = Union[Variables[cost], Variables[surf]] Out= {x, y, z}

Use the \[Del] operator to defined the Cartesian gradient operator:

In:= \[Del](f_) := (D[f, #1] & ) /@ vars

Now the equations can be solved using a notation which is pretty similar
to that found in most calculus books:

In:= Solve[{\[Del]cost == \[Lambda]*\[Del]surf, surf == 0}, vars,
\[Lambda]]
Out=
17
5 Sqrt[--]
17                17                 47 {{x -> -2
Sqrt[--], y -> -3 Sqrt[--], z -> -(----------)},
47                47              2

17
5 Sqrt[--]
17               17               47
{x -> 2 Sqrt[--], y -> 3 Sqrt[--], z -> ----------}}
47               47            2

Cheers,
Paul

____________________________________________________________________
Paul Abbott                                   Phone: +61-8-9380-2734
Department of Physics                           Fax: +61-8-9380-1014
The University of Western Australia            Nedlands WA  6907
mailto:paul@physics.uwa.edu.au  AUSTRALIA
http://www.pd.uwa.edu.au/~paul

God IS a weakly left-handed dice player
____________________________________________________________________

```

• References:
• optimize
• From: kimhhans@online.no (Holger)
• Prev by Date: RE: How to change the argument of a function?
• Next by Date: Re: 3D Graphics in Spherical Coordinates
• Prev by thread: Re: optimize
• Next by thread: Re: optimize