## Final enero 2018

### Apartado 1

kill(all)$
define(f(x,y,z), x-y+\sqrt(3)*z);

define(lig(x,y,z),x^2+2*y^2+4*z^2-9);

define(L(x,y,z,l),f(x,y,z)+l*lig(x,y,z));

e1:diff(L(x,y,z,l),x);
e2:diff(L(x,y,z,l),y);
e3:diff(L(x,y,z,l),z);
el:diff(L(x,y,z,l),l);

realonly:true$
sol:solve([e1,e2,e3,el],[x,y,z,l]);
nn:length(%)$

for kk:1 thru nn do print("f =",ev(f(x,y,z),sol[kk]) , "* Punto ", sol[kk] )$

define(hes(x,y,z,l),hessian(L(x,y,z,l),[x,y,z,l]));

M0:minor(hes(x,y,z,l),4,4);
vect:[dx,dy,dz]$
vec:matrix( [dx], [dy] ,[dz])$
define(FQ0(dx,dy,dz),expand(vect.M0.vec));

jacobian([lig(x,y,z)],[x,y,z]);

kk:1$
jacobian([lig(x,y,z)],[x,y,z]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dy);
ev(FQ0(dx,dy,dz),sollig);
ev(%,sol[kk]);

kk:2$
jacobian([lig(x,y,z)],[x,y,z]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dy);
ev(FQ0(dx,dy,dz),sollig);
ev(%,sol[kk]);

### Apartado 2

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

define(lig(x,y,z),x^2+2*y^2+4*z^2-9);

define(L(x,y,z,l),f(x,y,z)+l*lig(x,y,z));

e1:diff(L(x,y,z,l),x),factor;
e2:diff(L(x,y,z,l),y),factor;
e3:diff(L(x,y,z,l),z),factor;
el:diff(L(x,y,z,l),l);

realonly:true$
sol:solve([e1,e2,e3,el],[x,y,z,l]);
nn:length(%)$

for kk:1 thru nn do print("f =",ev(f(x,y,z),sol[kk]) , "* Punto ", sol[kk] )$

define(hes(x,y,z,l),hessian(L(x,y,z,l),[x,y,z,l]));
M0:minor(hes(x,y,z,l),4,4);
vect:[dx,dy,dz]$
vec:matrix( [dx], [dy] ,[dz])$
define(FQ0(dx,dy,dz),expand(vect.M0.vec));

kk:1$
jacobian([lig(x,y,z)],[x,y,z]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dz);
ev(FQ0(dx,dy,dz),sollig);
ev(%,sol[kk]);

kk:2$
jacobian([lig(x,y,z)],[x,y,z]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dz);
ev(FQ0(dx,dy,dz),sollig);
ev(%,sol[kk]);

kk:3$
jacobian([lig(x,y,z)],[x,y,z]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dz);
ev(FQ0(dx,dy,dz),sollig);
ev(%,sol[kk]);

kk:4$
jacobian([lig(x,y,z)],[x,y,z]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dz);
ev(FQ0(dx,dy,dz),sollig);
ev(%,sol[kk]);

k:2$
wxdraw3d(xlabel = "x",  ylabel = "y",color = cyan, view=[80,0],
     implicit( x^2+2*y^2+4*z^2-9=0 , x, -4,4, y, -4,4,z,-4,4),  xaxis=true, yaxis=true,
    point_type=filled_circle, point_size = 1,color = red, points([[0,0,3]]),
color = black, points([ [rhs(sol[k][1]),rhs(sol[k][2]),rhs(sol[k][3]) ] ])
)$

k:1$
wxdraw3d(xlabel = "x",  ylabel = "y",color = cyan, view=[80,0],
     implicit( x^2+2*y^2+4*z^2-9=0 , x, -4,4, y, -4,4,z,-4,4),  xaxis=true, yaxis=true,
    point_type=filled_circle, point_size = 1,color = red, points([[0,0,3]]),
color = black, points([ [rhs(sol[k][1]),rhs(sol[k][2]),rhs(sol[k][3]) ] ])
)$

k:3$
wxdraw3d(xlabel = "x",  ylabel = "y",color = cyan, view=[80,0],
     implicit( x^2+2*y^2+4*z^2-9=0 , x, -4,4, y, -4,4,z,-4,4),  xaxis=true, yaxis=true,
    point_type=filled_circle, point_size = 1,color = red, points([[0,0,3]]),
color = black, points([ [rhs(sol[k][1]),rhs(sol[k][2]),rhs(sol[k][3]) ] ]),
    points([ [rhs(sol[k+1][1]),rhs(sol[k+1][2]),rhs(sol[k+1][3]) ] ])
)$

## Final diciembre 2018

### Apartado 1

kill(all)$
define(f(x,y,z), x-z+sqrt(3)*y);

define(lig(x,y,z),x^2+4*y^2+2*z^2-9);

define(L(x,y,z,l),f(x,y,z)+l*lig(x,y,z));

e1:diff(L(x,y,z,l),x);
e2:diff(L(x,y,z,l),y);
e3:diff(L(x,y,z,l),z);
el:diff(L(x,y,z,l),l);

realonly:true$
sol:solve([e1,e2,e3,el],[x,y,z,l]);
nn:length(%)$

for kk:1 thru nn do print("f =",ev(f(x,y,z),sol[kk]) , "* Punto ", sol[kk] )$

define(hes(x,y,z,l),hessian(L(x,y,z,l),[x,y,z,l]));

M0:minor(hes(x,y,z,l),4,4);
vect:[dx,dy,dz]$
vec:matrix( [dx], [dy] ,[dz])$
define(FQ0(dx,dy,dz),expand(vect.M0.vec));

jacobian([lig(x,y,z)],[x,y,z]);

kk:1$
jacobian([lig(x,y,z)],[x,y,z]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dy);
ev(FQ0(dx,dy,dz),sollig);
ev(%,sol[kk]);

kk:2$
jacobian([lig(x,y,z)],[x,y,z]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dy);
ev(FQ0(dx,dy,dz),sollig);
ev(%,sol[kk]);

### Apartado 2

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

define(lig(x,y,z),x^2+4*y^2+2*z^2-9);

define(L(x,y,z,l),f(x,y,z)+l*lig(x,y,z));

e1:diff(L(x,y,z,l),x),factor;
e2:diff(L(x,y,z,l),y),factor;
e3:diff(L(x,y,z,l),z),factor;
el:diff(L(x,y,z,l),l);

realonly:true$
sol:solve([e1,e2,e3,el],[x,y,z,l]);
nn:length(%)$

for kk:1 thru nn do print("f =",ev(f(x,y,z),sol[kk]) , "* Punto ", sol[kk] )$

define(hes(x,y,z,l),hessian(L(x,y,z,l),[x,y,z,l]));
M0:minor(hes(x,y,z,l),4,4);
vect:[dx,dy,dz]$
vec:matrix( [dx], [dy] ,[dz])$
define(FQ0(dx,dy,dz),expand(vect.M0.vec));

kk:1$
jacobian([lig(x,y,z)],[x,y,z]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dy);
ev(FQ0(dx,dy,dz),sollig);
ev(%,sol[kk]);

kk:2$ sol[kk];
jacobian([lig(x,y,z)],[x,y,z]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dy);
ev(FQ0(dx,dy,dz),sollig);
ev(%,sol[kk]);

kk:3$
jacobian([lig(x,y,z)],[x,y,z]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dy);
ev(FQ0(dx,dy,dz),sollig);
ev(%,sol[kk]);

kk:4$
jacobian([lig(x,y,z)],[x,y,z]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dy);
ev(FQ0(dx,dy,dz),sollig);
ev(%,sol[kk]);

k:1$
draw3d(xlabel = "x",  ylabel = "y",color = cyan, view=[50,80],
     implicit( lig(x,y,z)=0 , x, -4,4, y, -4,4,z,-4,4),  xaxis=true, yaxis=true,
    point_type=filled_circle, point_size = 1,color = red, points([[0,3,0]]),
color = black, points([ [rhs(sol[k][1]),rhs(sol[k][2]),rhs(sol[k][3]) ] ])
)$

k:2$
draw3d(xlabel = "x",  ylabel = "y",color = cyan, view=[50,80],
     implicit(lig(x,y,z)=0 , x, -4,4, y, -4,4,z,-4,4),  xaxis=true, yaxis=true,
    point_type=filled_circle, point_size = 1,color = red, points([[0,3,0]]),
color = black, points([ [rhs(sol[k][1]),rhs(sol[k][2]),rhs(sol[k][3]) ] ])
)$

k:3$
draw3d(xlabel = "x",  ylabel = "y",color = cyan, view=[50,80],
     implicit( lig(x,y,z)=0 , x, -4,4, y, -4,4,z,-4,4),  xaxis=true, yaxis=true,
    point_type=filled_circle, point_size = 1,color = red, points([[0,3,0]]),
color = black, points([ [rhs(sol[k][1]),rhs(sol[k][2]),rhs(sol[k][3]) ] ]),
    points([ [rhs(sol[k+1][1]),rhs(sol[k+1][2]),rhs(sol[k+1][3]) ] ])
)$

## Parcial enero 2018 G1

### Puntos más alejados y más cercanos

remfunction(f)$
a:2$ b:2$
define(f(x,y),(x-a)^2+(y-b)^2);
define(lig(x,y),(x)^2-x*y+y^2-1);

wxdraw2d(grid=true, line_type=solid, line_width=3, nticks=200, proportional_axes = xy ,
        xaxis =true, xaxis_type=solid, yaxis =true, yaxis_type=solid, 
        color=blue, implicit(lig(x,y)=0, x, -2,2, y, -2,2), 
        line_width=1,  color=red, explicit(x,x,-2.5,2.5),  
        point_type = filled_circle , point_size =2,  points([[a,b]]) )$

define(L(x,y,l),f(x,y)+l*lig(x,y));

realonly:true$
e1:diff(L(x,y,l),x); 
e2:diff(L(x,y,l),y); 
el:diff(L(x,y,l),l); 
sol:solve([e1,e2,el],[x,y,l]);
nn:length(%)$

for kk:1 thru nn do print("f =",ev(f(x,y),sol[kk]) , "* Punto ", sol[kk] ) $

define(hes(x,y,l),hessian(L(x,y,l),[x,y,l])); 
kill(dx,dy)$
vec:matrix( [dx], [dy] )$ vect:[dx,dy]$

M0:minor(hes(x,y,l),3,3);
define(FQ0(dx,dy),expand(vect.M0.vec));

kk:1$
jacobian([lig(x,y)],[x,y]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dy);
ev(FQ0(dx,dy),sollig);
ev(%,sol[kk]);

kk:2$
jacobian([lig(x,y)],[x,y]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dy);
ev(FQ0(dx,dy),sollig);
ev(%,sol[kk]);

ptos1:makelist( [rhs(sol[k][1]),rhs(sol[k][2])] , k,1,2);

wxdraw2d(grid=true, line_type=solid, line_width=3, nticks=200,
        xaxis =true, xaxis_type=solid, yaxis =true, yaxis_type=solid, 
        color=blue, implicit(lig(x,y)=0, x, -3,3, y, -3,3), 
        line_width=1,  color=red, explicit(x,x,-3,3),  proportional_axes = xy,
    point_type = filled_circle , point_size =2,  points([[a,b]]) ,
        point_type = filled_circle , point_size =2,   color=black, points(ptos1) )$

### Extremos sobre una curva

remfunction(f)$
define(f(x,y),(x)^2+4*y-4);
define(lig(x,y),(x)^2+y^2-9);

e1:diff(f(x,y),x);
e2:diff(f(x,y),y);
sol:solve([e1,e2],[x,y]);

define(L(x,y,l),f(x,y)+l*lig(x,y));

realonly:true$
e1:diff(L(x,y,l),x); 
e2:diff(L(x,y,l),y); 
el:diff(L(x,y,l),l); 
sol:solve([e1,e2,el],[x,y,l]);
nn:length(%)$

for kk:1 thru nn do print("f =",ev(f(x,y),sol[kk]) , "* Punto ", sol[kk] )$

define(hes(x,y,l),hessian(L(x,y,l),[x,y,l])); 
kill(dx,dy)$
vec:matrix( [dx], [dy] )$ vect:[dx,dy]$

M0:minor(hes(x,y,l),3,3);
define(FQ0(dx,dy),expand(vect.M0.vec));

kk:1$
jacobian([lig(x,y)],[x,y]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dy);
ev(FQ0(dx,dy),sollig);
ev(%,sol[kk]);

kk:2$
jacobian([lig(x,y)],[x,y]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dy);
ev(FQ0(dx,dy),sollig);
ev(%,sol[kk]);

kk:3$
jacobian([lig(x,y)],[x,y]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dy);
ev(FQ0(dx,dy),sollig);
ev(%,sol[kk]);

kk:4$
jacobian([lig(x,y)],[x,y]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dy);
ev(FQ0(dx,dy),sollig);
ev(%,sol[kk]);

kk:1$
punto1:[rhs(sol[kk][1]), rhs(sol[kk][2]), ev(f(x,y),sol[kk])];
kk:2$
punto2:[rhs(sol[kk][1]), rhs(sol[kk][2]), ev(f(x,y),sol[kk])];
kk:3$
punto3:[rhs(sol[kk][1]), rhs(sol[kk][2]), ev(f(x,y),sol[kk])];
kk:4$
punto4:[rhs(sol[kk][1]), rhs(sol[kk][2]), ev(f(x,y),sol[kk])];

draw3d( xu_grid = 20, yv_grid = 20, view = [65,68], surface_hide = true,
             parametric_surface( 3*cos(u), 3*sin(u), f(3*cos(u),3*sin(u)), u, 0, 2*%pi, v, -%pi, %pi), 
            point_type=filled_circle, point_size = 2,
           color=dark_green, points([punto1]),
           color=red, points([punto2]),
           color = black, points([punto3,punto4]))$

## Parcial C enero 2020

### Puntos más alejados y más cercanos

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

define(lig(x,y,z),4*(x-1)^2+y^2+z^2-1);

define(L(x,y,z,l),f(x,y,z)+l*lig(x,y,z));

e1:diff(L(x,y,z,l),x),factor;
e2:diff(L(x,y,z,l),y),factor;
e3:diff(L(x,y,z,l),z),factor;
el:diff(L(x,y,z,l),l);

realonly:true$
sol:solve([e1,e2,e3,el],[x,y,z,l]);
nn:length(%)$

for kk:1 thru nn do print("f =",ev(f(x,y,z),sol[kk]) , "* Punto ", sol[kk] )$

define(hes(x,y,z,l),hessian(L(x,y,z,l),[x,y,z,l]));
M0:minor(hes(x,y,z,l),4,4);
vect:[dx,dy,dz]$
vec:matrix( [dx], [dy] ,[dz])$
define(FQ0(dx,dy,dz),expand(vect.M0.vec));

kk:1$
jacobian([lig(x,y,z)],[x,y,z]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dy);
ev(FQ0(dx,dy,dz),sollig);
ev(%,sol[kk]);

kk:2$
jacobian([lig(x,y,z)],[x,y,z]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dy);
ev(FQ0(dx,dy,dz),sollig);
ev(%,sol[kk]);

lig(x,y,z);

a:1$b:0$ c:0$
px:1$ py:2$ pz:0$

lig(x,y,z):=z^2+y^2+4*(x-1)^2-1;

k:1$
wxdraw3d(xlabel = "x",  ylabel = "y",color = cyan, view=[60,80],
     implicit( lig(x,y,z)=0 , x, a-1,a+1, y, b-1,b+1 ,z,c-1,c+1),  xaxis=true, yaxis=true,
    point_type=filled_circle, point_size = 1,color = red, points([[px,py,pz]]),
color = black, points([ [rhs(sol[k][1]),rhs(sol[k][2]),rhs(sol[k][3]) ] ])
)$

k:2$
wxdraw3d(xlabel = "x",  ylabel = "y",color = cyan, view=[60,80],
     implicit( lig(x,y,z)=0 , x, a-1,a+1, y, b-1,b+1 ,z,c-1,c+1),  xaxis=true, yaxis=true,
    point_type=filled_circle, point_size = 1,color = red, points([[px,py,pz]]),
color = black, points([ [rhs(sol[k][1]),rhs(sol[k][2]),rhs(sol[k][3]) ] ])
)$

### Extremos sobre una curva

remfunction(f)$
define(f(x,y),2*(x)^2+y^2);
define(lig(x,y),(x)^2+y^2-3*y-4);

e1:diff(f(x,y),x);
e2:diff(f(x,y),y);
sol:solve([e1,e2],[x,y]);

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

define(L(x,y,l),f(x,y)+l*lig(x,y));

realonly:true$
e1:diff(L(x,y,l),x); 
e2:diff(L(x,y,l),y); 
el:diff(L(x,y,l),l); 
sol:solve([e1,e2,el],[x,y,l]);
nn:length(%)$

for kk:1 thru nn do print("f =",ev(f(x,y),sol[kk]) , "* Punto ", sol[kk] )$

define(hes(x,y,l),hessian(L(x,y,l),[x,y,l])); 
kill(dx,dy)$
vec:matrix( [dx], [dy] )$ vect:[dx,dy]$

M0:minor(hes(x,y,l),3,3);
define(FQ0(dx,dy),expand(vect.M0.vec));

kk:1$
jacobian([lig(x,y)],[x,y]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dy);
ev(FQ0(dx,dy),sollig);
ev(%,sol[kk]);

kk:2$
jacobian([lig(x,y)],[x,y]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dy);
ev(FQ0(dx,dy),sollig);
ev(%,sol[kk]);

kk:3$
jacobian([lig(x,y)],[x,y]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dy);
ev(FQ0(dx,dy),sollig);
ev(%,sol[kk]);

kk:4$
jacobian([lig(x,y)],[x,y]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dy);
ev(FQ0(dx,dy),sollig);
ev(%,sol[kk]);

kk:1$
punto1:[rhs(sol[kk][1]), rhs(sol[kk][2]), ev(f(x,y),sol[kk])];
kk:2$
punto2:[rhs(sol[kk][1]), rhs(sol[kk][2]), ev(f(x,y),sol[kk])];
kk:3$
punto3:[rhs(sol[kk][1]), rhs(sol[kk][2]), ev(f(x,y),sol[kk])];
kk:4$
punto4:[rhs(sol[kk][1]), rhs(sol[kk][2]), ev(f(x,y),sol[kk])];

x(u):=5/2*cos(u); y(u):=3/2+5/2*sin(u);

draw3d( xu_grid = 20, yv_grid = 20, view = [65,68], surface_hide = true,
             parametric_surface(x(u), y(u), f(x(u),y(u)), u, 0, 2*%pi, v, -%pi, %pi), 
            point_type=filled_circle, point_size = 2,
           color=dark_green, points([punto1]),
           color=red, points([punto2]),
           color = black, points([punto3,punto4]))$

x^2+(y-3/2)^2-4-9/4;

ratsimp(%);

## Parcial enero 2018 G2

### Puntos más alejados y más cercanos

remfunction(f)$
a:-2$ b:2$
define(f(x,y),(x-a)^2+(y-b)^2);
define(lig(x,y),(x)^2-x*y+y^2-1);

wxdraw2d(grid=true, line_type=solid, line_width=3, nticks=200, proportional_axes = xy ,
        xaxis =true, xaxis_type=solid, yaxis =true, yaxis_type=solid, 
        color=blue, implicit(lig(x,y)=0, x, -2,2, y, -2,2), 
        line_width=1,  color=red, explicit(-x,x,-2.5,2.5),  
        point_type = filled_circle , point_size =2,  points([[a,b]]) )$

define(L(x,y,l),f(x,y)+l*lig(x,y));

realonly:true$
e1:diff(L(x,y,l),x); 
e2:diff(L(x,y,l),y); 
el:diff(L(x,y,l),l); 
sol:solve([e1,e2,el],[x,y,l]);
nn:length(%)$

for kk:1 thru nn do print("f =",ev(f(x,y),sol[kk]) , "* Punto ", sol[kk] ) $

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

define(hes(x,y,l),hessian(L(x,y,l),[x,y,l])); 
kill(dx,dy)$
vec:matrix( [dx], [dy] )$ vect:[dx,dy]$

M0:minor(hes(x,y,l),3,3);
define(FQ0(dx,dy),expand(vect.M0.vec));

kk:1$
jacobian([lig(x,y)],[x,y]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dy);
ev(FQ0(dx,dy),sollig);
ev(%,sol[kk]);

kk:2$
jacobian([lig(x,y)],[x,y]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dy);
ev(FQ0(dx,dy),sollig);
ev(%,sol[kk]);

ptos1:makelist( [rhs(sol[k][1]),rhs(sol[k][2])] , k,1,2);

wxdraw2d(grid=true, line_type=solid, line_width=3, nticks=200,
        xaxis =true, xaxis_type=solid, yaxis =true, yaxis_type=solid, 
        color=blue, implicit(lig(x,y)=0, x, -3,3, y, -3,3), 
        line_width=1,  color=red, explicit(-x,x,-3,3),  proportional_axes = xy,
    point_type = filled_circle , point_size =2,  points([[a,b]]) ,
        point_type = filled_circle , point_size =2,   color=black, points(ptos1) )$

### Extremos sobre una curva

remfunction(f)$
define(f(x,y),(x)^2+4*y-4);
define(lig(x,y),(2*x)^2+4*y^2-1);

e1:diff(f(x,y),x);
e2:diff(f(x,y),y);
sol:solve([e1,e2],[x,y]);

define(L(x,y,l),f(x,y)+l*lig(x,y));

realonly:true$
e1:diff(L(x,y,l),x); 
e2:diff(L(x,y,l),y); 
el:diff(L(x,y,l),l); 
sol:solve([e1,e2,el],[x,y,l]);
nn:length(%)$

for kk:1 thru nn do print("f =",ev(f(x,y),sol[kk]) , "* Punto ", sol[kk] )$

define(hes(x,y,l),hessian(L(x,y,l),[x,y,l])); 
kill(dx,dy)$
vec:matrix( [dx], [dy] )$ vect:[dx,dy]$

M0:minor(hes(x,y,l),3,3);
define(FQ0(dx,dy),expand(vect.M0.vec));

kk:1$
jacobian([lig(x,y)],[x,y]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dy);
ev(FQ0(dx,dy),sollig);
ev(%,sol[kk]);

kk:2$
jacobian([lig(x,y)],[x,y]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dy);
ev(FQ0(dx,dy),sollig);
ev(%,sol[kk]);

kk:1$
punto1:[rhs(sol[kk][1]), rhs(sol[kk][2]), ev(f(x,y),sol[kk])];
kk:2$
punto2:[rhs(sol[kk][1]), rhs(sol[kk][2]), ev(f(x,y),sol[kk])];

draw3d( xu_grid = 20, yv_grid = 20, view = [65,68], surface_hide = true,
             parametric_surface( sqrt(2)/2*cos(u), 1/2*sin(u), f(sqrt(2)/2*cos(u), 1/2*sin(u)), u, 0, 2*%pi, v, -%pi, %pi), 
            point_type=filled_circle, point_size = 2,
           color=dark_green, points([punto1]),
           color=red, points([punto2]))$

## Recuperación P2 enero 2020

### Puntos más alejados y más cercanos

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

define(lig(x,y,z),4*(x)^2+y^2+(z-2)^2-1);

define(L(x,y,z,l),f(x,y,z)+l*lig(x,y,z));

e1:diff(L(x,y,z,l),x),factor;
e2:diff(L(x,y,z,l),y),factor;
e3:diff(L(x,y,z,l),z),factor;
el:diff(L(x,y,z,l),l);

realonly:true$
sol:solve([e1,e2,e3,el],[x,y,z,l]);
nn:length(%)$

for kk:1 thru nn do print("f =",ev(f(x,y,z),sol[kk]) , "* Punto ", sol[kk] )$

9/4-7/3;

define(hes(x,y,z,l),hessian(L(x,y,z,l),[x,y,z,l]));
M0:minor(hes(x,y,z,l),4,4);
vect:[dx,dy,dz]$
vec:matrix( [dx], [dy] ,[dz])$
define(FQ0(dx,dy,dz),expand(vect.M0.vec));

kk:1$
jacobian([lig(x,y,z)],[x,y,z]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dx);
ev(FQ0(dx,dy,dz),sollig);
ev(%,sol[kk]);

kk:2$
jacobian([lig(x,y,z)],[x,y,z]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dx);
ev(FQ0(dx,dy,dz),sollig);
ev(%,sol[kk]);

 kk:3$
jacobian([lig(x,y,z)],[x,y,z]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dx);
ev(FQ0(dx,dy,dz),sollig);
ev(%,sol[kk]);
   

 kk:4$
jacobian([lig(x,y,z)],[x,y,z]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dx);
ev(FQ0(dx,dy,dz),sollig);
ev(%,sol[kk]);

a:0$b:0$ c:2$
px:-1$ py:0$ pz:2$

lig(x,y,z):=(z-2)^2+y^2+4*x^2-1;

k:1$[rhs(sol[k][1]),rhs(sol[k][2]),rhs(sol[k][3]) ];

lig(x,y,z);

k:1$
draw3d(xlabel = "x",  ylabel = "y",color = cyan, view=[30,340],
     implicit( lig(x,y,z)=0 , x, a-1,a+1, y, b-1,b+1 ,z,c-1,c+1),  xaxis=true, yaxis=true,
    point_type=filled_circle, point_size = 1,color = red, points([[px,py,pz]]),
color = black, points([ [rhs(sol[k][1]),rhs(sol[k][2]),rhs(sol[k][3]) ] ])
)$

k:2$
draw3d(xlabel = "x",  ylabel = "y",color = cyan, view=[30,340],
     implicit( lig(x,y,z)=0 , x, a-1,a+1, y, b-1,b+1 ,z,c-1,c+1),  xaxis=true, yaxis=true,
    point_type=filled_circle, point_size = 1,color = red, points([[px,py,pz]]),
color = black, points([ [rhs(sol[k][1]),rhs(sol[k][2]),rhs(sol[k][3]) ] ])
)$

k:3$
draw3d(xlabel = "x",  ylabel = "y",color = cyan, view=[40,340],
     implicit( lig(x,y,z)=0 , x, a-1,a+1, y, b-1,b+1 ,z,c-1,c+1),  xaxis=true, yaxis=true,
    point_type=filled_circle, point_size = 1,color = red, points([[px,py,pz]]),
color = black, points([ [rhs(sol[k][1]),rhs(sol[k][2]),rhs(sol[k][3]) ] ])
)$

k:4$
draw3d(xlabel = "x",  ylabel = "y",color = cyan, view=[40,340],
     implicit( lig(x,y,z)=0 , x, a-1,a+1, y, b-1,b+1 ,z,c-1,c+1),  xaxis=true, yaxis=true,
    point_type=filled_circle, point_size = 1,color = red, points([[px,py,pz]]),
color = black, points([ [rhs(sol[k][1]),rhs(sol[k][2]),rhs(sol[k][3]) ] ])
)$

### Extremos sobre una curva

remfunction(f)$
define(f(x,y),(x)^2-2*y^2);
define(lig(x,y),(x)^2+y^2-3*x-4);

e1:diff(f(x,y),x);
e2:diff(f(x,y),y);
sol:solve([e1,e2],[x,y]);

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

define(L(x,y,l),f(x,y)+l*lig(x,y));

realonly:true$
e1:diff(L(x,y,l),x); 
e2:diff(L(x,y,l),y); 
el:diff(L(x,y,l),l); 
sol:solve([e1,e2,el],[x,y,l]);
nn:length(%)$

for kk:1 thru nn do print("f =",ev(f(x,y),sol[kk]) , "* Punto ", sol[kk] )$

define(hes(x,y,l),hessian(L(x,y,l),[x,y,l])); 
kill(dx,dy)$
vec:matrix( [dx], [dy] )$ vect:[dx,dy]$

M0:minor(hes(x,y,l),3,3);
define(FQ0(dx,dy),expand(vect.M0.vec));

kk:1$
jacobian([lig(x,y)],[x,y]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dx);
ev(FQ0(dx,dy),sollig);
ev(%,sol[kk]);

kk:2$
jacobian([lig(x,y)],[x,y]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dx);
ev(FQ0(dx,dy),sollig);
ev(%,sol[kk]);

kk:3$
jacobian([lig(x,y)],[x,y]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dy);
ev(FQ0(dx,dy),sollig);
ev(%,sol[kk]);

kk:4$
jacobian([lig(x,y)],[x,y]);ev(%,sol[kk]);
%.vect; sollig:solve(%,dy);
ev(FQ0(dx,dy),sollig);
ev(%,sol[kk]);

kk:1$
punto1:[rhs(sol[kk][1]), rhs(sol[kk][2]), ev(f(x,y),sol[kk])];
kk:2$
punto2:[rhs(sol[kk][1]), rhs(sol[kk][2]), ev(f(x,y),sol[kk])];
kk:3$
punto3:[rhs(sol[kk][1]), rhs(sol[kk][2]), ev(f(x,y),sol[kk])];
kk:4$
punto4:[rhs(sol[kk][1]), rhs(sol[kk][2]), ev(f(x,y),sol[kk])];

y(u):=5/2*cos(u); x(u):=3/2+5/2*sin(u);

draw3d( xu_grid = 20, yv_grid = 20, view = [65,68], surface_hide = true,
             parametric_surface(x(u), y(u), f(x(u),y(u)), u, 0, 2*%pi, v, -%pi, %pi), 
            point_type=filled_circle, point_size = 2,
           color=dark_green, points([punto1]),
           color=red, points([punto2]),
           color = black, points([punto3,punto4]))$