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




No comments:

Post a Comment

Search This Blog