Showing posts with label MAC11. Show all posts
Showing posts with label MAC11. Show all posts

Friday, November 10, 2023

Vector Calculus

 

Gradient, Divergence, Curl, Laplacian

Syntax

 

 g = gradient(f,v) 

returns the gradient vector of symbolic scalar field f with respect to vector v in Cartesian coordinates.

 d = divergence(V,X) 

returns the divergence of symbolic vector field V with respect to vector X in Cartesian coordinates. Vectors V and X must have the same length.

c = curl(V,X) 

returns the curl of symbolic vector field V with respect to vector X in three-dimensional Cartesian coordinates. Both the vector field V and the vector X must be vectors with three components.

 l = laplacian(f,v)

returns the Laplacian of the symbolic field f with respect to the vector v in Cartesian coordinates. If f is an array, then the function computes the Laplacian for each element of f and returns the output l that is the same size as f.

 

Example 1

Find gradient of $$f=2yz\sin(x)+3x\sin(z)cos(y)$$ a given scalar function  at (1,2,3)

clear

clc

syms x y z

f(x,y,z) =input("Enter the function f(x,y,z):");

gradf=(simplify(gradient(f,[x y z])));

fprintf("grad(f)=%s\n",gradf)

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

val=vpa(gradf(p(1),p(2),p(3)));

fprintf('\n grad(f) at (%d,%d,%d) is (%.4f)i + (%.4f)j + (%.4f)k \n',p(1),p(2),p(3),val(1),val(2),val(3))


Output

Enter the function f(x,y,z):2*y*z*sin(x) + 3*x*sin(z)*cos(y)

grad(f)=[3*cos(y)*sin(z) + 2*y*z*cos(x); 2*z*sin(x) - 3*x*sin(y)*sin(z); 2*y*sin(x) + 3*x*cos(y)*cos(z)]

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

grad(f) at (1,2,3) is (6.3074)i + (4.6639)j + (4.6018)k

Example 2

Find the divergence of the given vector field $$f=xyi+2xy^2 j+3xz^3 k$$ at (3,4,5)

clear 

clc

syms x y z     

F =input('Enter the components of vector F in [x y z] form : ');

div(x,y,z)=divergence(F,[x y z]);

fprintf('\n div(F) = %s \n',div);

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

val=div(p(1),p(2),p(3));

fprintf('\n div(F) at (%d,%d,%d) is %d \n',p(1),p(2),p(3),val);

Output

Enter the components of vector F in [x y z] form : [x*y 2*x*y^2 3*x*z^3]

 div(F) = y + 4*x*y + 9*x*z^2

 Enter the point in the form [a,b,c] : [3,4,5]

 div(F) at (3,4,5) is 727

Example 3

Find the gradient of the scalar function $$f=-(\sin(x)+\sin(y))^2$$ and interpret geometrically.

clear

clc

syms x y

f = input("Enter the function f(x,y):");

gradf = gradient(f,[x y])

[X1, Y1] = meshgrid(-1:.1:1,-1:.1:1);

G1 = subs(gradf(1),[x y],{X1,Y1});

G2 = subs(gradf(2),[x y],{X1,Y1});

quiver(X1,Y1,G1,G2)

Output

Enter the function f(x,y):-(sin(x) + sin(y))^2

gradf =

-2*cos(x)*(sin(x) + sin(y))

-2*cos(y)*(sin(x) + sin(y))



Example 4

Find the gradient of the scalar function $$f=xy^2+yz^2+zx^2$$ and interpret geometrically.

clear

clc

syms x y z

f = input("Enter the function f(x,y,z):");

gradf = gradient(f,[x y z])

[X1, Y1, Z1] = meshgrid(-1:.2:1,-1:.2:1,-1:.2:1);

G1 = subs(gradf(1),[x y z],{X1,Y1,Z1});

G2 = subs(gradf(2),[x y z],{X1,Y1,Z1});

G3 = subs(gradf(3),[x y z],{X1,Y1,Z1});

quiver3(X1,Y1,Z1,G1,G2,G3)

xlabel('X')

ylabel('Y')

zlabel('Z')

Output

Enter the function f(x,y,z):x*y^2+y*z^2+z*x^2

gradf =

2*x*z + y^2

2*x*y + z^2

x^2 + 2*y*z


Example 5

Find the Laplacian of the scalar function $$x^3+y^2-log(z)$$

clear

clc

syms x y z

f(x,y,z) =input("Enter the scalar function f(x,y,z):");

L=(simplify(laplacian(f,[x y z])));

fprintf("Laplacian of f(x,y,z) is %s \n",L)

Output

Enter the scalar function f(x,y,z):x^3 + y^2 - log(z)

Laplacian of f(x,y,z) is 6*x + 1/z^2 + 2

Example 6

To find directional derivative of function $$xy^2+yz^2+zx^2$$ in the direction of the vector 𝑖+𝑗+𝑘 at (1,2,3)

clear

clc

syms x y z

f= input("Enter the function f(x,y,z):");

gradf = gradient(f, [x y z])

n=input('\nEnter the components of directional vector as [x y z]:');

m = n/norm(n);

D(x,y,z) = simplify(dot(gradf',m));

fprintf('\nDirectional derivative is %s\n', D)

p=input('\nEnter the point as [a,b,c]:');

val=vpa(D(p(1),p(2),p(3)));

fprintf('Directional derivative at (%d,%d,%d) is %f\n', p(1),p(2),p(3),val)

Output

Enter the function f(x,y,z):x^2*z + x*y^2 + y*z^2

gradf =

2*x*z + y^2

2*x*y + z^2

x^2 + 2*y*z

Enter the components of directional vector as [x y z]:[1 1 1]

Directional derivative is (3^(1/2)*(x + y + z)^2)/3

Enter the point as [a,b,c]:[1,2,3]

Directional derivative at (1,2,3) is 20.784610




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)



Search This Blog