Nice Code. This is the generalization to n dimensions:
lagrange(f,g,vars):=
block([i,j,sys,allvars,k,lm,n:length(vars),m:length(g),h],
lm:makelist(k[i],i,1,m),
z:f+sum(k[i]*g[i],i,1,m),
sys:append(g,makelist(diff(z,vars[i]),i,1,n)),
realonly:true,
allvars:append(vars,lm),
v:to_poly_solve(sys,allvars),
h:makelist(makelist(diff(diff(z,allvars[i]),allvars[j]),i,1,n+m),j,1,n+m),
H:apply(matrix,h),
for s in v do (
mn: subst(s, H),mn1:subst(s,z),
de:determinant(mn),
if de >0 then print ("Constrained_Extrema Max at",s,"f(z)=",mn1),
if de <0 then print ("Constrained_Extrema Min at",s,"f(z)=",mn1),
if de=0 then print ("Determinant is zero"),
ratprint:false),
v)$
Use e.g. lagrange(x^2+y^2,[x+y+1],[x,y]);
Reinhard
Luigi Marino schrieb:
> Constained Extrema with Lagrange Method
> finds extrema of f(x,y) with constrained g(x,y)
>
> lagrange(f,g):= block(
> z:f+k*g, s1:diff(z,x,1),
> s2:diff(z,y,1),
> s3:diff(z,k,1),
> load(topoly_solver), realonly:true,
> v:to_poly_solve([s1,s2,s3],[x,y,k]),
> u1:diff(z,x,2),
> u2:diff(z,y,2),
> u3:diff(diff(z,x,2),y),
> u4:diff(diff(z,y,2),x),
> g1:diff(g,x),g2:diff(g,y),
> H:matrix([0,g1,g2],[g1,u1,u4],[g2,u3,u2]),
> for s in v do (
> mn: subst(s, H),mn1:subst(s,z),
> de:determinant(mn),
> if de >0 then print ("Constrained_Extrema Max at",s,"f(z)=",mn1),
> if de <0 then print ("Constrained_Extrema Min at",s,"f(z)=",mn1),
> if de=0 then print ("Determinant is zero"),
> ratprint:false,
> wxdraw2d(xrange = [-8,8], yrange = [-8,8],
> grid=true, xaxis = true, yaxis = true,
> title = "f(x,y)-g(x,y)",
> color = red,
> implicit(f=mn1,x,-8,8,y,-8,8),
> color =blue,
> implicit(g,x,-8,8,y,-8,8))) )$
>
> For the usage look at included file
>
> Luigi Marino
> ------------------------------------------------------------------------
>
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>
--
Prof. Dr. Reinhard Oldenburg
Goethe-Universit?t Frankfurt
Institut f?r Didaktik der Mathematik und Informatik
Senckenberganlage 9
D-60325 Frankfurt/Main
Telefon +49 (0)69 798 23770
Telefax +49 (0)69 798 22553
E-Mail oldenbur at math.uni-frankfurt.de
WWW www.math.uni-frankfurt.de/~oldenbur