Sunday, October 29, 2023

Partial differentiation and Jacobian

 Syntax

diff(f, var, n)

computes the nth derivative of f with respect to var.

Example1

$$\text{Find all the first order and second order partial derivatives of the function }u=e^{\frac{x}{y}}.$$ $$\text{Also, find }\frac{\partial u}{\partial x}\text{ at }x=2, \frac{\partial u}{\partial x}\text{ at }y=2 \text{ and } \frac{\partial u}{\partial y}\text{ at }x=2, y=3.$$ 

clear

clc

syms x y

%Define U

U(x,y)=exp(x/y);

%First derivatives

Ux=diff(U,x);

Uy=diff(U,y);

%Second derivatives

Uxx=simplify(diff(Ux,x));

Uyy=simplify(diff(Uy,y))

Uxy=simplify(diff(Ux,y))

Uyx=simplify(diff(Uy,x))

 

U1=Ux(2,y);

U2=Ux(x,2);

U3=Uy(2,3);

 

fprintf('\n Ux(2,y)=%s; \t Ux(x,2)=%s; \t Uy(2,3)=%f \n',U1,U2,U3)

Output

The first order partial derivatives are

Ux = exp(x/y)/y

Uy = -(x*exp(x/y))/y^2

 

The second order partial derivatives are

Uxx = exp(x/y)/y^2

Uyy = (x*exp(x/y)*(x + 2*y))/y^4

Uxy = -(exp(x/y)*(x + y))/y^3

Uyx = -(exp(x/y)*(x + y))/y^3

 

Ux(2,y)=exp(2/y)/y; Ux(x,2)=exp(x/2)/2;      Uy(2,3)=-0.432830

 

Example 2

$$\text{If }z=\frac{1}{\sqrt(y^2-2xy+1)}\text{ then prove that }x\frac{\partial z}{\partial x}-y\frac{\partial z}{\partial y}=y^2z^3.$$

clear all

clc

syms z x y

z=(1-2*x*y+y^2)^(-1/2);

zx=diff(z,x);

zy=diff(z,y);

LHS=simplify(x*zx-y*zy)

RHS=simplify(y^2*z^3)

if(LHS==RHS)

    fprintf('\n The given condition is satisfied\n');

   

else

    fprintf('\n The given condition is not satisfied')

end

Output

LHS =

y^2/(- 2*x*y + y^2 + 1)^(3/2) 

RHS =

y^2/(- 2*x*y + y^2 + 1)^(3/2)

The given condition is satisfied

 

Jacobian

jacobian([u1,u2,…],[x1,x2,…])

 computes the Jacobian of a vector function which is a matrix of the partial derivatives of that function

·       To find the determinant find the determinant of the resultant Jacobian matrix.

Example 1

$$\text{If }u=x^2-2y\text{ and }v=x+y\text{ then find the Jacobian }\frac{\partial (u,v)}{\partial (x, y)}.\text{ Also, find the value of}$$ $$\text{Jacobian at }(1, 0).$$

clear all

clc

syms x y u v

u(x,y)=input('Enter u(x,y):');

v(x,y)=input('Enter v(x,y):');

J(x,y)=simplify(det(jacobian([u,v],[x,y])));

fprintf('\n J((u,v)/(x,y))= %s \n', J);

 

p=input('\n Enter the point in the form [a,b] : ');

val=J(p(1),p(2));

fprintf('\n J((u,v)/(x,y)) at (%d,%d) is %d \n', p(1),p(2), val)

Output

Enter u(x,y):x^2-2*y

Enter v(x,y):x+y

J((u,v)/(x,y))= 2*x + 2

Enter the point in the form [a,b] : [1,0]

J((u,v)/(x,y)) at (1,0) is 4

Example 2

$$\text{If }u=x^2-y^2\text{ and }v=2xy\text{ where }x=r \cos(t)\text{ and } y=r \sin(t)\text{ then find the Jacobian }\frac{\partial(u,v)}{\partial(x,y).}$$ $$\text{Also, show that }\frac{\partial(u,v)}{\partial(r,t)}=4r^3\text{ and hence find the value of Jacobian }\frac{\partial(u,v)}{\partial(r,t)}\text{ at }(3,1).$$

clear all

clc

syms x y u v r t

u(x,y)=input('Enter u(x,y):');

v(x,y)=input('Enter v(x,y):');

J1(x,y)=det(jacobian([u(x,y),v(x,y)],[x,y]));

fprintf('\n J((u,v)/(x,y))= %s \n', J1);

 

x=input('\n Enter x(r,t):');

y=input('Enter y(r,t):');

J2(r,t)=simplify(det(jacobian([u(x,y),v(x,y)],[r,t])));

fprintf('\n J((u,v)/(r,t))=%s \n', J2)

 

p=input('\n Enter the point in the form [a,b] : ');

val=J2(p(1),p(2));

fprintf('\n J((u,v)/(r,t)) at (%d,%d) is %d \n', p(1),p(2), val)

 

Output

Enter u(x,y):x^2-y^2

Enter v(x,y):2*x*y

 

J((u,v)/(x,y))= 4*x^2 + 4*y^2

 

Enter x(r,t):r*cos(t)

Enter y(r,t):r*sin(t)

 

J((u,v)/(r,t))=4*r^3

 

Enter the point in the form [a,b] : [3,1]

J((u,v)/(r,t)) at (3,1) is 108

Example 3

$$\text{If }u=x\sin(y)\cos(z), v=x\sin(y)\sin(z)\text{ and } w=x\cos(y)\text{ then find }\frac{\partial(u,v,w)}{\partial(x,y,z)}$$

clear all

clc

syms x y z u v w

u(x,y,z)=input('Enter u(x,y,z):');

v(x,y,z)=input('Enter v(x,y,z):');

w(x,y,z)=input('Enter w(x,y,z):');

J(x,y,z)=simplify(det(jacobian([u,v,w],[x,y,z])));

fprintf('\n J((u,v,w)/(x,y,z))= %s \n', J);

Output

Enter u(x,y,z):x*sin(y)*cos(z)

Enter v(x,y,z):x*sin(y)*sin(z)

Enter w(x,y,z):x*cos(y)

J((u,v,w)/(x,y,z))= x^2*sin(y)



No comments:

Post a Comment

Search This Blog