Re: optimize
- To: mathgroup@smc.vnet.net
- Subject: [mg10820] Re: optimize
- From: Paul Abbott <paul@physics.uwa.edu.au>
- Date: Tue, 10 Feb 1998 21:01:34 -0500
- Organization: University of Western Australia
- References: <199802020544.AAA28177@smc.vnet.net.> <6ba8fc$b97@smc.vnet.net>
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)
- optimize