MathGroup Archive 1998

[Date Index] [Thread Index] [Author Index]

Search the Archive

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];
> eqns = Append[MapThread[Equal,{grad1,grad2}], surf==0]
> 
> Now it is simple to eliminate lambda and solve for {x,y,z}.
> 
> In[54]:= InputForm[soln = Solve[eqns, vars, lambda]] Out[54]//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[1]:= surf = x^2 + 2*y^2 + 4*z^2 - 17;  In[2]:= cost = x + 3*y + 5*z; 

Compute the (polynomial variables):

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

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

In[4]:= \[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[5]:= Solve[{\[Del]cost == \[Lambda]*\[Del]surf, surf == 0}, vars,
\[Lambda]]
Out[5]=
                                                    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