0% found this document useful (0 votes)
70 views3 pages

Lab 3

This document provides 3 examples of calculating the gradient, divergence, and curl of vector functions using Sympy. In the first example, it calculates the gradient of φ = x2y + 2xz - 4. In the second example, it finds the divergence of F⃗ = x2yzî + y2zx̂ + z2xyk̂. In the third example, it computes the curl of F⃗ = x2yzî + y2zx̂ + z2xyk̂. It demonstrates using both Method I and Method II to perform these vector calculus operations.

Uploaded by

Nutankumar KM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views3 pages

Lab 3

This document provides 3 examples of calculating the gradient, divergence, and curl of vector functions using Sympy. In the first example, it calculates the gradient of φ = x2y + 2xz - 4. In the second example, it finds the divergence of F⃗ = x2yzî + y2zx̂ + z2xyk̂. In the third example, it computes the curl of F⃗ = x2yzî + y2zx̂ + z2xyk̂. It demonstrates using both Method I and Method II to perform these vector calculus operations.

Uploaded by

Nutankumar KM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

2TG22CG003

LAB 3: Finding gradient, divergent, curl and their geometrical


interpretation
1.2 Method I :

To find gradient of ϕ = x 2 y + 2xz − 4.


from sympy .vector import* from
sympy import symbols
N=CoordSys3D('N')
x,y,z=symbols('x y z')
A=N.x**2*N.y+2*N.x*N.z-4
delop=Del() display(delop(A))
gradA=gradient(A) print(f"\n
Gradient of {A} is \n")
display(gradA)

Output :

(∂/∂𝐱N(𝐱N2𝐲𝐍+2𝐱𝐍𝐳𝐍−4))𝐢̂ 𝐍+(∂/∂𝐲𝐍(𝐱𝐍2𝐲𝐍+2𝐱𝐍𝐳𝐍−4))𝐣 𝐍+(∂/∂𝐳𝐍(𝐱𝐍2𝐲𝐍+2𝐱𝐍𝐳𝐍−4))𝐤̂ 𝐍


Gradient of N.x**2*N.y + 2*N.x*N.z - 4 is
(2𝐱𝐍𝐲𝐍+2𝐳𝐍)𝐢̂ 𝐍+(𝐱𝐍2)𝐣 𝐍+(2𝐱𝐍)𝐤̂ 𝐍

Example 2 :

To find divergence of F⃗ = x 2 yzˆi + y 2 zxˆj + z 2xyˆk


from sympy .vector import* from
sympy import symbols
N=CoordSys3D('N')
x,y,z=symbols('x y z')
A=N.x**2*N.y*N.z*N.i+N.y**2*N.z*N.x*N.j+N.z**2*N.x*N.y*N.k
delop=Del() divA=delop.dot(A) display(divA)
print(f"\n Divergence of {A} is \n")
display(divergence(A))

Output :

∂/∂𝐳𝐍 (𝐱𝐍𝐲𝐍𝐳𝐍2)+∂/∂𝐲𝐍 (𝐱𝐍𝐲𝐍2𝐳𝐍 )+∂/∂𝐱𝐍 (𝐱𝐍2𝐲𝐍𝐳𝐍 )

Divergence of N.x**2*N.y*N.z*N.i + N.x*N.y**2*N.z*N.j + N.x*N.y*N.z**2*N.k is

6𝐱𝐍𝐲𝐍𝐳𝐍
Example 3 :

To find curl of F⃗ = x 2 yzˆi + y 2 zxˆj + z 2xyˆk


from sympy .vector import* from
sympy import symbols

1
2TG22CG003

N=CoordSys3D('N')
x,y,z=symbols('x y z')
A=N.x**2*N.y*N.z*N.i+N.y**2*N.z*N.x*N.j+N.z**2*N.x*N.y*N.k
delop=Del() curlA=delop.cross(A) display(curlA)
print(f"\n Curl of {A} is \n")
display(curl(A))

Output :

(∂/∂𝐲𝐍(𝐱𝐍𝐲𝐍𝐳𝐍2)−∂/∂𝐳𝐍(𝐱𝐍𝐲𝐍2𝐳𝐍))𝐢 ̂ 𝐍+(−∂/∂𝐱𝐍(𝐱𝐍𝐲𝐍𝐳𝐍2)+∂/∂𝐳𝐍(𝐱𝐍2𝐲𝐍𝐳𝐍))𝐣 ̂ 𝐍+(∂/∂𝐱𝐍(𝐱𝐍𝐲𝐍2𝐳𝐍)−∂/∂𝐲𝐍


(𝐱𝐍2𝐲𝐍𝐳𝐍 ))𝐤 ̂ 𝐍

1.3 Method II:

To find gradient of ϕ = x 2 yz.


from sympy.physics .vector import*
from sympy import var,pprint
var('x y z') v=ReferenceFrame('v')
F=v[0]**2*v[1]*v[2]
G=gradient(F,v)
F=F.subs([(v[0],x),(v[1],y),(v[2],z)])
print("Given scalar function F=")
display(F)
G=G.subs([(v[0],x),(v[1],y),(v[2],z)])
print(f"\n Gradient of F=") display(G)

Output :

Given scalar function F=


𝑥2𝑦𝑧
Gradient of F=
2𝑥𝑦𝑧𝐯 ̂ 𝐱+𝑥2𝑧𝐯 ̂ 𝐲+𝑥2𝑦𝐯 ̂ 𝐳

To find divergence of F⃗ = x 2 yˆi + yz2ˆj + x 2 z ˆk.

from sympy.physics .vector import* from


sympy import var
var('x y z')
v=ReferenceFrame('v')
F=v[0]*v[1]**2*v.x+2*v[0]**2*v[1]*v[2]*v.y-3*v[1]*v[2]**2*v.z
G=curl(F,v)
F=F.subs([(v[0],x),(v[1],y),(v[2],z)])
print("Given vector point function is")
display(F)
G=G.subs([(v[0],x),(v[1],y),(v[2],z)])
print("Curl of F=") display(G)

2
2TG22CG003

Output :

Given vector point function is


𝑥2𝑦𝐯 ̂ 𝐱+𝑦𝑧2𝐯 ̂ 𝐲+𝑥2𝑧𝐯 ̂ 𝐳
Divergence of F=
𝑥2+2𝑥𝑦+𝑧2

To find curl of F⃗ = xy2ˆi + 2x 2 yzˆj − 3yz2ˆk

from sympy.physics .vector import* from sympy import var


var('x y z')
v=ReferenceFrame('v')
F=v[0]*v[1]**2*v.x+2*v[0]**2*v[1]*v[2]*v.y-3*v[1]*v[2]**2*v.z
G=curl(F,v)
F=F.subs([(v[0],x),(v[1],y),(v[2],z)])
print("Given vector point function is")
display(F)
G=G.subs([(v[0],x),(v[1],y),(v[2],z)])
print("Curl of F=")
display(G)

Output :

Given vector point function is


𝑥𝑦2𝐯 ̂ 𝐱+2𝑥2𝑦𝑧𝐯 ̂ 𝐲−3𝑦𝑧2𝐯 ̂ 𝐳
Curl of F=
(−2𝑥2𝑦−3𝑧2)𝐯 ̂ 𝐱+(4𝑥𝑦𝑧−2𝑥𝑦)𝐯 ̂ 𝐳

You might also like