## Ejemplo: x³+y³+z³-2z(x+y)-2x+y-2z+1=0 . Existe z(x,y) en U(0,0,1)?

### Definimos la función y comprobamos las hipótesis del TFI

kill(all)$
diff(z,x);diff(z,y);

F(x,y,z):=x^3+y^3+z^3-2*z*(x+y)-2*x+y-2*z+1 ;
a:0$ b:0$c:1$

F(a,b,c);

diff(F(x,y,z),z);
ev(%,x=a,y=b,z=c);

draw3d(xu_grid=20, yv_grid=20,  z_voxel=20, point_type=filled_circle, 
point_size =2,color = black, points([[a,b,c]]),  
color = blue,  colorbox=true, xlabel="x",ylabel="y", 
zlabel="z",font="Arial", font_size=20, palette = color,
implicit(F(x,y,z)=0 , x,a-.25,a+.25,y,b-0.25,b+.25,z,c-.25,c+.25) )$

### Calculamos las primeras derivadas

depends(z,[x,y]);

### Calculamos zx

eq1:diff(F(x,y,z),x);

solve(eq1,'diff(z,x,1));
zx:rhs(%[1]);
vzx:ev(%,x=a,y=b,z=c);

### Calculamos zy

eq2:diff(F(x,y,z),y);

solve(eq2,'diff(z,y,1));
zy:rhs(%[1]);
vzy:ev(%,x=a,y=b,z=c);

### Calculamos las segundas derivadas

### Calculando zxx

/*  Derivando dos veces */
zxx1:diff(F(x,y,z),x,2);
subst(['diff(z,y,1)=zy,'diff(z,x,1)=zx, 'diff(z,x,2)=zxx],   zxx1);
ev(%, x=a,y=b,z=c);
solve(%,zxx);

/*Método general */
eqzxx:solve(zxx1,'diff(z,x,2));

/* calculando zxx  el general */
subst(['diff(z,y,1)=zy,'diff(z,x,1)=zx],eqzxx);
rhs(%[1])$
vzxx: ev(%, x=a,y=b,z=c);

/* Usando la fórmula del TFI */
diff(zx,x);
subst(['diff(z,y,1)=zy,'diff(z,x,1)=zx],%);

vzxx: ev(%, x=a,y=b,z=c);

/* calculando zxx directamente en el punto (a,b,c) */
subst(['diff(z,y,1)=zy,'diff(z,x,1)=zx,x=a,y=b,z=c],rhs(eqzxx[1]));
vzxx:subst(['diff(z,y,1)=vzy,'diff(z,x,1)=vzx,x=a,y=b,z=c],rhs(eqzxx[1]));

### Calculando zyy

zyy1:diff(F(x,y,z),y,2);

subst(['diff(z,y,1)=zy,'diff(z,x,1)=zx, 'diff(z,y,2)=zyy],   zyy1);
ev(%, x=a,y=b,z=c);
solve(%,zyy);

eqzyy:solve(diff(F(x,y,z),y,2),'diff(z,y,2));

/* calculando zyy  el general */
subst(['diff(z,y,1)=zy,'diff(z,x,1)=zx],eqzyy);
rhs(%[1])$
vzyy: ev(%, x=a,y=b,z=c);

/* calculando zyy en el punto (a,b,c) */
subst(['diff(z,y,1)=vzy,'diff(z,x,1)=vzx],eqzyy);
vzyy:subst(['diff(z,y,1)=vzy,'diff(z,x,1)=vzx,x=a,y=b,z=c],rhs(eqzyy[1]));

### Calculando zxy

zxy1:diff(F(x,y,z),x,1,y,1);

subst(['diff(z,y,1)=zy,'diff(z,x,1)=zx,'diff(z,x,1,y,1)=zxy],   zxy1);
ev(%, x=a,y=b,z=c);
solve(%,zxy);

eqzxy:solve(diff(F(x,y,z),x,1,y,1),'diff(z,x,1,y,1));

subst(['diff(z,y,1)=zy,'diff(z,x,1)=zx],eqzxy);
rhs(%[1])$
vzxy: ev(%, x=a,y=b,z=c);

/* calculando zyy en el punto (a,b,c) */
subst(['diff(z,y,1)=vzy,'diff(z,x,1)=vzx],eqzxy);
vzxy:subst(['diff(z,y,1)=vzy,'diff(z,x,1)=vzx,x=a,y=b,z=c],rhs(eqzxy[1]));

### Calculando el polinomio de Taylor

### La diferencial Dz(x,y)

Dz: matrix( [zx,zy] )$
vDz:ev(Dz, x=a,y=b,z=c);

### La diferencial D²z(x,y)

D2z: matrix(
 [zxx,zxy], 
 [zxy,zyy] )$
vD2z:ev(D2z,x=a,y=b,z=c,zxx=vzxx,zyy=vzyy,zxy=vzxy);

### El polinomio de orden 2: z(a,b)+ Dz(a,b)(x,y)+(1/2)D²z(a,b)(x,y)

c + vDz.[(x-a),(y-b)]+ [(x-a),(y-b)].(1/2*vD2z).[(x-a),(y-b)];

### Cálculo derivando las expresiones del TFI

kill(all)$
F(x,y,z):=x^3+y^3+z^3-2*z*(x+y)-2*x+y-2*z+1 ;
a:0$ b:0$c:1$

define(zx(x,y),-diff(F(x,y,z),x)/diff(F(x,y,z),z));
define(zy(x,y),-diff(F(x,y,z),y)/diff(F(x,y,z),z));

vzx:ev(zx(x,y),x=a,y=b,z=c );
vzy:ev(zy(x,y),x=a,y=b,z=c );

depends(z,[x,y]);

dzxx:diff(zx(x,y),x);
subst(['diff(z,x,1)=zx],dzxx);
ev(%, x=a,y=b,z=c,zx=vzx);

dzyy:diff(zy(x,y),y)$
subst(['diff(z,y,1)=zy],dzyy);
ev(%, x=a,y=b,z=c,zy=vzy);

dzxy:diff(zy(x,y),x)$
subst(['diff(z,y,1)=zy,'diff(z,x,1)=zx],dzxy);
ev(%, x=a,y=b,z=c,zx=vzx,zy=vzy);

## Ejemplo: x²+y²+z²=exp(x+y+z)-1.  Existe z(x,y) en U(0,0,0)?

### Definimos la función y comprobamos las hipótesis del TFI

kill(all)$
diff(z,x);diff(z,y);

kill(all)$
define(F(x,y,z),x^2+y^2+z^2-exp(x+y+z)+1);
a:0$ b:0$ c:0$

F(a,b,c);

diff(F(x,y,z),z);
ev(%,x=a,y=b,z=c);

draw3d(xu_grid=30, yv_grid=30,  z_voxel=30, point_type=filled_circle, 
point_size =2,color = black, points([[a,b,c]]),  
color = blue,  colorbox=true, xlabel="x",ylabel="y", 
zlabel="z",font="Arial", font_size=20, palette = color,
implicit(F(x,y,z)=0 , x,a-.5,a+.5,y,b-0.5,b+.5,z,c-.5,c+.5) )$

### Calculamos las primeras derivadas

depends(z,[x,y]);

### Calculamos zx

eq1:diff(F(x,y,z),x);

solve(eq1,'diff(z,x,1));
zx:rhs(%[1]);
vzx:ev(%,x=a,y=b,z=c);

### Calculamos zy

eq2:diff(F(x,y,z),y);

solve(eq2,'diff(z,y,1));
zy:rhs(%[1]);
vzy:ev(%,x=a,y=b,z=c);

### Calculamos las segundas derivadas

### Calculando zxx

/*  Derivando dos veces */
zxx1:diff(F(x,y,z),x,2);
subst(['diff(z,y,1)=zy,'diff(z,x,1)=zx, 'diff(z,x,2)=zxx],   zxx1);
solve(%,zxx);
vzxx: ev(%, x=a,y=b,z=c);

subst(['diff(z,y,1)=vzy,'diff(z,x,1)=vzx, 'diff(z,x,2)=zxx,x=a,y=b,z=c],   zxx1);
solve(%,zxx);
vzxx:rhs(%[1]);

/* Usando la fórmula del TFI */
diff(zx,x);
subst(['diff(z,y,1)=zy,'diff(z,x,1)=zx],%);

ratsimp(%);

vzxx: ev(%, x=a,y=b,z=c);

### Calculando zyy

zyy1:diff(F(x,y,z),y,2);

subst(['diff(z,y,1)=vzy,'diff(z,x,1)=vzx, 'diff(z,y,2)=zyy, x=a,y=b,z=c],   zyy1);
solve(%,zyy);
vzyy:rhs(%[1]);

### Calculando zxy

zxy1:diff(F(x,y,z),x,1,y,1);

subst(['diff(z,y,1)=zy,'diff(z,x,1)=zx,'diff(z,x,1,y,1)=zxy,x=a,y=b,z=c],   zxy1);
solve(%,zxy);
vzxy:rhs(%[1]);

### Calculando el polinomio de Taylor

### La diferencial Dz(x,y)

Dz: matrix( [zx,zy] );
vDz:ev(Dz, x=a,y=b,z=c);

### La diferencial D²z(x,y)

D2z: matrix(
 [zxx,zxy], 
 [zxy,zyy]
)$
vD2z:ev(D2z,x=a,y=b,z=c,zxx=vzxx,zyy=vzyy,zxy=vzxy);

### El polinomio de orden 2: z(a,b)+ Dz(a,b)(x,y)+(1/2)D²z(a,b)(x,y)

c + vDz.[(x-a),(y-b)]+ [(x-a),(y-b)].(1/2*vD2z).[(x-a),(y-b)];

## Ejemplo: sin(αx+βy+γz)+exp(γxyz)+x+2y-1. Si el polinomio de Taylor de orden 2 de z(x,y) es nulo cuál es el valor de α, β y γ.

### Definimos la función y comprobamos las hipótesis del TFI

kill(all)$
diff(z,x);diff(z,y);

kill(all)$
define(F(x,y,z), sin(alpha*x+beta*y+gamma*z)+exp(gamma*x*y*z)+x+2*y-1  );
a:0$ b:0$ c:0$

F(a,b,c);

diff(F(x,y,z),z);
ev(%,x=a,y=b,z=c);

draw3d(xu_grid=30, yv_grid=30,  z_voxel=30, point_type=filled_circle, 
point_size =2,color = black, points([[a,b,c]]),  
color = blue,  colorbox=true, xlabel="x",ylabel="y", 
zlabel="z",font="Arial", font_size=20, palette = color,
implicit(F(x,y,z)=0 , x,a-.5,a+.5,y,b-0.5,b+.5,z,c-.5,c+.5) )$

### Calculamos las primeras derivadas

depends(z,[x,y]);

### Calculamos zx

eq1:diff(F(x,y,z),x);

solve(eq1,'diff(z,x,1));
zx:rhs(%[1]);
vzx:ev(%,x=a,y=b,z=c);

### Calculamos zy

eq2:diff(F(x,y,z),y);

solve(eq2,'diff(z,y,1));
zy:rhs(%[1]);
vzy:ev(%,x=a,y=b,z=c);

### Calculamos las segundas derivadas

### Calculando zxx

/*  Derivando dos veces */
zxx1:diff(F(x,y,z),x,2);

subst(['diff(z,y,1)=vzy,'diff(z,x,1)=vzx, 'diff(z,x,2)=zxx,x=a,y=b,z=c],   zxx1);
solve(%,zxx);
vzxx:rhs(%[1]);

### Calculando zyy

zyy1:diff(F(x,y,z),y,2);

subst(['diff(z,y,1)=vzy,'diff(z,x,1)=vzx, 'diff(z,y,2)=zyy, x=a,y=b,z=c],   zyy1);
solve(%,zyy);
vzyy:rhs(%[1]);

### Calculando zxy

zxy1:diff(F(x,y,z),x,1,y,1);

subst(['diff(z,y,1)=zy,'diff(z,x,1)=zx,'diff(z,x,1,y,1)=zxy,x=a,y=b,z=c],   zxy1);
solve(%,zxy);
vzxy:rhs(%[1]);

### Calculando el polinomio de Taylor

### La diferencial Dz(x,y)

Dz: matrix( [zx,zy] );
vDz:ev(Dz, x=a,y=b,z=c);

### La diferencial D²z(x,y)

D2z: matrix(
 [zxx,zxy], 
 [zxy,zyy]
)$
vD2z:ev(D2z,x=a,y=b,z=c,zxx=vzxx,zyy=vzyy,zxy=vzxy);

### El polinomio de orden 2: z(a,b)+ Dz(a,b)(x,y)+(1/2)D²z(a,b)(x,y)

c + vDz.[(x-a),(y-b)]+ [(x-a),(y-b)].(1/2*vD2z).[(x-a),(y-b)];

## Ejemplo:  F(x,y)=-log(x)+xy+log(y)=0. Prueba que y=f(x) tiene un extremo.

kill(all)$
define(F(x,y),x*y-log(x)+log(y));

a:10$
wxplot3d( [ [ F(x,y),[x, .01, a], [y, .01,1] ], [ 0 ,[x, .01,a], [y, .01,1]  ]] ,[grid, 80, 80])$

load(implicit_plot);

wximplicit_plot(F(x,y)=0,[x,0.1,5],[y,0.1,2]);

 sol:makelist([0.5+k/10, find_root(F(0.5+k/10,y), y, .1, 4)],k,0,30)$

wxplot2d([discrete,sol])$

diff(F(x,y),y);

define(dy(x),factor(-diff(F(x,y),x)/diff(F(x,y),y)));

depends(y,x);

eq:diff(F(x,y),x);

solve(eq,'diff(y,x,1));

rhs(%[1])$ 
yx:factor(%);

solve(yx=0,y);

diff(eq,x);

solve(%,'diff(y,x,2));

rhs(%[1]);

ev(%,'diff(y,x,1)=yx);

factor(%);

ev(%,y=1/x);

solve(F(x,1/x),x);

xm:rhs(float(%)[1]);

ym:find_root(F(xm,y), y, .1, 4);

lmax:[[xm,ym]];

wxplot2d( [ [discrete, lmax ],[discrete, sol ]  ] ,[style, [points,4,2], [points,2,1]] ,
     [point_type, bullet, bullet] )$