## Ejemplos de extremos libres

### Ejemplo 1

 kill(all)$
define(f(x,y),x^4+y^4+6*x^2*y^2+8*x^3);

realonly:true$
eq1:diff(f(x,y),x);
eq2:diff(f(x,y),y);
sol:solve([eq1,eq2],[x,y]);

hes:hessian(f(x,y),[x,y]);

h1:ev(hes,sol[1]);

taylor(f(x,y),[x,y],[0,0],3);

plot3d(f(x,y),[x,-.5,.5],[y,-.8,.8],[legend,""],[palette, false], [color, blue])$

wxplot2d([f(x,.2),f(x,0),f(x,-.2),f(x,x),f(x,-x)],[x,-.5,.5])$

h1:ev(hes,sol[2]);

plot3d(f(x,y),[x,-6.2,-5.8],[y,-.1,.1],[legend,""],[palette, false], [color, blue])$

define(tay(x,y), taylor(f(x,y),[x,y],[-6,0],2));

plot3d( [[ f(x,y),[x,-6.5,-5.5],[y,-.5,.5]  ], [ tay(x,y),[x,-6.5,-5.5],[y,-.5,.5] ] ],
     [palette, false] ,  [color, blue, red] )$

### Ejemplo 2

#### Ejemplo 2a

 kill(all)$
define(f(x,y),x^4+y^4-(x+y)^2+2);

realonly:true$
eq1:diff(f(x,y),x);
eq2:diff(f(x,y),y);
sol:solve([eq1,eq2],[x,y]);

hes:hessian(f(x,y),[x,y]);

sol[1];

h1:ev(hes,sol[1]);determinant(h1);

eigenvalues(h1);

wxplot3d(f(x,y),[x,-1.1,-.9],[y,-1.1,-.9],[legend,""],[palette, false], [color, blue])$

sol[2];

h2:ev(hes,sol[2]);determinant(h2);

sol[3];

h3:ev(hes,sol[3]);determinant(h3);

eigenvalues(h3);

plot3d(f(x,y),[x,-.2,.2],[y,-.2,.2],[legend,""],[palette, false], [color, blue])$

f(x,x);
f(x,-x);

wxplot2d([f(x,x),f(x,-x)],[x,-.5,.5]);

f(x,x);diff(f(x,x),x);diff(f(x,x),x,2); ev(%,x=0);

f(x,-x);diff(f(x,-x),x);diff(f(x,-x),x,2); ev(%,x=0);

#### Ejemplo 2b

kill(all)$
define(f(x,y),x^4+y^4+(x+y)^2+2);

realonly:true$
eq1:diff(f(x,y),x);
eq2:diff(f(x,y),y);
sol:solve([eq1,eq2],[x,y]);

hes:hessian(f(x,y),[x,y]);

h1:ev(hes,sol[1]);determinant(h1);

wxplot3d(f(x,y),[x,-1,1],[y,-1,1],[legend,""],[palette, false], [color, blue])$

define(tay(x,y), taylor(f(x,y),[x,y],[0,0],2));

f(x,y);

(y+x)^2+y^4+x^4;

### Ejemplo 3 (3 variables)

kill(all)$ 
realonly:true$
define(f(x,y,z),x^4+y^4+z^4-2*(x^2+z^2)+y^2);

eq1:diff(f(x,y,z),x);
eq2:diff(f(x,y,z),y);
eq3:diff(f(x,y,z),z);
sol:solve([eq1,eq2,eq3],[x,y,z]);

length(%);

hes:hessian(f(x,y,z),[x,y,z]);

sol[1];sol[2];sol[3];

h1:ev(hes,sol[1]);

h2:ev(hes,sol[2]);

h3:ev(hes,sol[3]);

sol[4];sol[5];sol[6];

h10:ev(hes,sol[4]);

h11:ev(hes,sol[5]);

h12:ev(hes,sol[6]);

sol[7];sol[8];sol[9];

h19:ev(hes,sol[7]);

h20:ev(hes,sol[8]);

h21:ev(hes,sol[9]);

### Ejemplo 4 f(x,y)=xy log(x²+y²)

kill(all)$
define(f(x,y),x*y*log(x^2+y^2));

eq1:diff(f(x,y),x),ratsimp;
eq2:diff(f(x,y),y),ratsimp;
sol:solve([eq1,eq2],[x,y]);

factor(eq1);

factor(eq2);

hes:hessian(f(x,y),[x,y]);

ratsimp(hes);

6*x*y^3+2*x^3*y,factor;

y^4+2*x^2*y^2+x^4,factor;

draw3d( xu_grid = 20, yv_grid = 20,  proportional_axes = xyz,   
line_width = 1, color=blue, explicit(f(x,y),x,-.5,.5,y,-.5,.5),
point_type = filled_circle , point_size =2, color=black, points([[0,0,0]]))$

#### Puntos sillas

sol:makelist(0,k,1,8)$

a:0$ b:1$
sol[1]:[x=a,y=b];
ev(hes,%);

taylor(f(x,y),[x,y],[a,b],2); /*  punto silla */;

draw3d( xu_grid = 20, yv_grid = 20,  line_width = 1, color=blue, 
    explicit(f(x,y),x,a-.5,a+.5,y,b-.5,b+.5),
point_type = filled_circle , point_size =2, color=black, points([[a,b,f(a,b)]]))$

wxplot2d([f(x,.2),f(x,0.1),f(x,-0.1),f(x,-.2)],[x,-.5,.5])$

a:0$ b:-1$
sol[2]:[x=a,y=b];
ev(hes,%);

taylor(f(x,y),[x,y],[a,b],2); /*  punto silla */;

draw3d( xu_grid = 20, yv_grid = 20,   line_width = 1, color=blue, 
    explicit(f(x,y),x,a-.5,a+.5,y,b-.5,b+.5),
point_type = filled_circle , point_size =2, color=black, points([[a,b,f(a,b)]]))$

a:1$ b:0$
sol[3]:[x=a,y=b];
ev(hes,%);

taylor(f(x,y),[x,y],[a,b],2); /*  punto silla */;

draw3d( xu_grid = 20, yv_grid = 20,  line_width = 1, color=blue, 
    explicit(f(x,y),x,a-.5,a+.5,y,b-.5,b+.5),
point_type = filled_circle , point_size =2, color=black, points([[a,b,f(a,b)]]))$

a:-1$ b:0$
sol[4]:[x=a,y=b];
ev(hes,%);
taylor(f(x,y),[x,y],[a,b],2);

#### Mínimos

a:1/sqrt(2*%e)$ b:1/sqrt(2*%e)$
sol[5]:[x=a,y=b];
ev(hes,%);

draw3d( xu_grid = 20, yv_grid = 20,  line_width = 1, color=blue, 
            explicit(f(x,y),x,a-.5,a+.5,y,b-.5,b+.5),
point_type = filled_circle , point_size =2, color=black, points([[a,b,f(a,b)]]))$

a:-1/sqrt(2*%e)$ b:-1/sqrt(2*%e)$
sol[6]:[x=a,y=b];
ev(hes,%);

draw3d( xu_grid = 20, yv_grid = 20,  line_width = 1, color=blue, 
    explicit(f(x,y),x,a-.5,a+.5,y,b-.5,b+.5),
point_type = filled_circle , point_size =2, color=black, points([[a,b,f(a,b)]]))$

#### Máximos

a:-1/sqrt(2*%e)$ b:1/sqrt(2*%e)$
sol[7]:[x=a,y=b];
ev(hes,%);

draw3d( xu_grid = 20, yv_grid = 20,  line_width = 1, color=red, 
    explicit(f(x,y),x,a-.5,a+.5,y,b-.5,b+.5),
point_type = filled_circle , point_size =2, color=black, points([[a,b,f(a,b)]]))$

a:1/sqrt(2*%e)$ b:-1/sqrt(2*%e)$
sol[8]:[x=a,y=b];
ev(hes,%);

draw3d( xu_grid = 20, yv_grid = 20,  line_width = 1, color=red, 
    explicit(f(x,y),x,a-.5,a+.5,y,b-.5,b+.5),
point_type = filled_circle , point_size =2, color=black, points([[a,b,f(a,b)]]))$

#### Dibujando todos los puntos críticos

psilla:makelist( [rhs(sol[k][1]),rhs(sol[k][2]),ev(f(x,y),sol[k])] , k,1,4);

pmin:makelist( [rhs(sol[k][1]),rhs(sol[k][2]),ev(f(x,y),sol[k])] , k,5,6);

pmax:makelist( [rhs(sol[k][1]),rhs(sol[k][2]),ev(f(x,y),sol[k])] , k,7,8);

draw3d( proportional_axes = xyz, xu_grid = 30, yv_grid = 30,  line_width = 1, color=cyan,
    explicit(0.7*f(x,y),x,-1.2,1.2,y,-1.2,1.2), 
point_type = filled_circle , point_size =1.5, color=black, points([[0,0,0]]), 
    points(psilla) , color=blue, points(pmin) ,  color=red,    points(pmax) )$

draw3d( point_type = filled_circle , point_size =1.5, color=black, points([[0,0,0]]), points(psilla) , 
    color=blue, points(pmin) ,  color=red,    points(pmax) , proportional_axes = xyz,
    enhanced3d = true, cbrange = [-.5,.5], view = [0,0],
    xu_grid = 50, yv_grid = 50,  line_width = 1, color=cyan, explicit(f(x,y),x,-1.2,1.2,y,-1.2,1.2) )$

draw3d(  proportional_axes = xyz, contour=both,view = [0,0],
          surface_hide=true,enhanced3d=true, color=blue, 
          contour_levels=[-3,.05,3],
          user_preamble="set for [i=1:8] linetype i dashtype i linecolor 0",
         explicit(0.7*f(x,y),x,-1.2,1.2,y,-1.2,1.2))$

draw3d( colorbox = false,proportional_axes = xyz, contour=both,view = [65,25],
          surface_hide=true,enhanced3d=true, color=blue, wired_surface=true,
          contour_levels=[-3,.05,3],
          user_preamble="set for [i=1:8] linetype i dashtype i linecolor 0",
         explicit(0.7*f(x,y),x,-1.2,1.2,y,-1.2,1.2))$

wxdraw3d( proportional_axes = xyz, colorbox = false,
          contour=both,view = [78,15],
          surface_hide=true,enhanced3d=true,wired_surface=true,
          contour_levels=[-3,.1,3],
          user_preamble="set for [i=1:8] linetype i dashtype i linecolor 0",
         explicit(f(x,y),x,-1.2,1.2,y,-1.2,1.2) );

### Ejemplo 5

remfunction(f)$
define(f(x,y),(x-y)*exp(-x^2-y^2)); /* Como ejercicio cambiar x+y por x-y*/;

eq1:diff(f(x,y),x);
eq2:diff(f(x,y),y);
sol:solve([eq1,eq2],[x,y]);

hes:hessian(f(x,y),[x,y])$
ratsimp(hes);

kk:1$
sol[kk];
a1:rhs(sol[kk][1]);
b1:rhs(sol[kk][2]);

h1:ev(hes,sol[1]);

eigenvalues(h1);
h1[1,1];
determinant(h1);

kk:2$
sol[kk];
a2:rhs(sol[kk][1]);
b2:rhs(sol[kk][2]);

h2:ev(hes,sol[2]);
h2[1,1];
determinant(h2);

eigenvalues(h2) ;

ptos:makelist( [rhs(sol[k][1]),rhs(sol[k][2]),ev(f(x,y),sol[k])] , k,1,2);

ev(f(x,y),sol[1]);

ev(f(x,y),sol[2]);

draw3d( point_type = filled_circle , point_size =2, color=black, points(ptos) , 
  proportional_axes = xyz, enhanced3d=false, cbrange = [-.5,.5], view = [80,45],
  line_width = 1, color=red, explicit(f(x,y),x,-1.2,1.2,y,-1.2,1.2) )$

wxdraw3d( proportional_axes = xyz, enhanced3d=true, view = [0,0], 
  line_width = 1,   explicit(f(x,y),x,-1.2,1.2,y,-1.2,1.2) , 
point_type = filled_circle , point_size =2, color=black, points(ptos)    
)$

wxdraw3d( proportional_axes = xyz, enhanced3d=true, view = [0,0], 
  line_width = 1,   explicit(f(x,y),x,-8,8,y,-8,8) , 
point_type = filled_circle , point_size =2, color=black, points(ptos)    
)$

wxplot3d (f(x,y), [x,-10,10],[y,-10,10], [zlabel,""],
[mesh_lines_color,false], [elevation,0], [azimuth,0],
color_bar, [grid,80,80], [ztics,false], [color_bar_tics,1]);

### Ejemplo 6a

remfunction(f)$
define(f(x,y),(x*y)*exp(-x^2-y^2));

eq1:diff(f(x,y),x);
eq2:diff(f(x,y),y);
sol:solve([eq1,eq2],[x,y]);
length(%);

hes:hessian(f(x,y),[x,y]),ratsimp;

kk:1$
sol[kk];
a:rhs(sol[kk][1])$
b:rhs(sol[kk][2])$
pto:[[a,b,f(a,b)]];

h:ev(hes,sol[kk]);
eigenvalues(%);

draw3d( point_type = filled_circle , point_size =2, color=black, points(pto) , 
 enhanced3d=false, cbrange = [-.5,.5], view = [80,45],
  line_width = 1, color=red, explicit(f(x,y),x,a-.2,a+.2,y,b-.2,b+.2) )$

wxdraw3d( proportional_axes = xyz, enhanced3d=true, cbrange = [-.1,.1], view = [0,0], 
  line_width = 1,  explicit(f(x,y),x,a-.5,a+.5,y,b-.5,b+.5) )$

kk:2$ 
sol[kk];
a:rhs(sol[kk][1])$
b:rhs(sol[kk][2])$
pto:[[a,b,f(a,b)]];
h:ev(hes,sol[kk]);
eigenvalues(%);

wxdraw3d( point_type = filled_circle , point_size =2, color=black, points(pto) , 
 enhanced3d=false, cbrange = [-.5,.5], view = [80,45],
  line_width = 1, color=red, explicit(f(x,y),x,a-.2,a+.2,y,b-.2,b+.2) )$

wxdraw3d( proportional_axes = xyz, enhanced3d=true,  view = [0,0], 
  line_width = 1,  explicit(f(x,y),x,a-.5,a+.5,y,b-.5,b+.5) )$

kk:3$ 
sol[kk];
a:rhs(sol[kk][1])$
b:rhs(sol[kk][2])$
pto:[[a,b,f(a,b)]];
h:ev(hes,sol[kk]);
eigenvalues(%);

wxdraw3d( point_type = filled_circle , point_size =2, color=black, points(pto) , 
 enhanced3d=false, cbrange = [-.5,.5], view = [80,45],
 line_width = 1, color=red, explicit(f(x,y),x,a-.2,a+.2,y,b-.2,b+.2) )$

wxdraw3d( proportional_axes = xyz, enhanced3d=true,  view = [0,0], 
  line_width = 1,  explicit(f(x,y),x,a-.5,a+.5,y,b-.5,b+.5) )$

wxdraw3d( proportional_axes = xyz, enhanced3d=true,  view = [0,0], 
  line_width = 1,  explicit(f(x,y),x,-5,5,y,b-5,5) )$

### Ejemplo 6b

remfunction(f)$
define(f(x,y),(x*y)*exp(x^2+y^2));

eq1:diff(f(x,y),x);
eq2:diff(f(x,y),y);
realonly:true$
sol:solve([eq1,eq2],[x,y]);

factor(eq1);

hes:hessian(f(x,y),[x,y]),ratsimp;

kk:1$
sol[kk];
a:rhs(sol[kk][1])$
b:rhs(sol[kk][2])$
pto:[[a,b,f(a,b)]];

h:ev(hes,sol[kk]);
eigenvalues(%);

wxdraw3d( point_type = filled_circle , point_size =2, color=black, points(pto) , 
 enhanced3d=false, cbrange = [-.5,.5], view = [80,45],
  line_width = 1, color=red, explicit(f(x,y),x,a-.2,a+.2,y,b-.2,b+.2) )$

wxdraw3d( proportional_axes = xyz, enhanced3d=true, cbrange = [-.1,.1], view = [0,0], 
  line_width = 1,  explicit(f(x,y),x,a-.5,a+.5,y,b-.5,b+.5) )$

wxdraw3d( proportional_axes = xyz, enhanced3d=true,  view = [0,0], 
  line_width = 1,  explicit(f(x,y),x,-1.5,1.5,y,-1.5,1.5) )$

### Ejemplo 7

remfunction(f)$
realonly:true$
define(f(x,y),(x^3+y^3)-9*x*y+27);

eq1:diff(f(x,y),x);
eq2:diff(f(x,y),y);
sol:solve([eq1,eq2],[x,y]);

hes:hessian(f(x,y),[x,y]);
determinant(hes);

kk:1$
sol[kk];
a:rhs(sol[kk][1])$
b:rhs(sol[kk][2])$
pto:[[a,b,f(a,b)]];

ev(hes,sol[kk]);
eigenvalues(%); 

wxdraw3d( point_type = filled_circle , point_size =2, color=black, points(pto) , 
 enhanced3d=false, cbrange = [-.5,.5], view = [80,45],
  line_width = 1, color=red, explicit(f(x,y),x,a-.2,a+.2,y,b-.2,b+.2) )$

wxdraw3d( proportional_axes = xyz, enhanced3d=true,   view = [0,0], 
  line_width = 1,  explicit(f(x,y),x,a-.5,a+.5,y,b-.5,b+.5) )$

kk:2$
sol[kk];
a:rhs(sol[kk][1])$
b:rhs(sol[kk][2])$
pto:[[a,b,f(a,b)]];

ev(hes,sol[kk]);
eigenvalues(%); 

wxdraw3d( point_type = filled_circle , point_size =2, color=black, points(pto) , 
 enhanced3d=false, cbrange = [-.5,.5], view = [80,45],
  line_width = 1, color=red, explicit(f(x,y),x,a-.2,a+.2,y,b-.2,b+.2) )$

wxdraw3d( proportional_axes = xyz, enhanced3d=true,   view = [0,0], 
  line_width = 1,  explicit(f(x,y),x,a-.5,a+.5,y,b-.5,b+.5) )$

wxdraw3d( proportional_axes = xyz, enhanced3d=true,   view = [0,0], 
  line_width = 1,  explicit(f(x,y),x,3-5,3+5,y,3-5,3+5) )$