Chapter 3: Vector Calculus

Example 3.1, Page number: 58

In [1]:
 

import scipy
from numpy import *
import scipy.integrate

#Variable Declaration

A=array([5,0,0])
B=array([0,5,0])
C=array([0,5,10])
D=array([5,0,10])

#Calculations

 #A,B,C,D in cylindrical coordinates
 
A=array([5,0,0])
B=array([5,scipy.pi,0])
C=array([5,scipy.pi,10])
D=array([5,0,10])

p=5

def BC(z): 
 return 1
ansa, erra = scipy.integrate.quad(BC, 0, 10)
 
def CD(phi): 
 return p
ansb, errb = scipy.integrate.quad(CD, 0, scipy.pi/2)
ansbb=ansb/scipy.pi         #answer in multiples of pi

def ABCD(phi,z): 
 return p
ansc, errc = scipy.integrate.dblquad(lambda z , phi: ABCD(phi,z),    
             0, scipy.pi/2, lambda z: 0, lambda z: 10) 
anscc=ansc/scipy.pi         #answer in multiples of pi
             
def ABO(phi,rho): 
 return rho
ansd, errd = scipy.integrate.dblquad(lambda rho , phi: ABO(phi,rho),    
             0, scipy.pi/2, lambda rho: 0, lambda rho: 5)
ansdd=ansd/scipy.pi         #answer in multiples of pi

def AOFD(rho,z): 
 return 1
anse, erre = scipy.integrate.dblquad(lambda z , rho: AOFD(rho,z),    
             0, 10, lambda z: 0, lambda z: 5)
             
def ABDCFO(z,phi,rho):
 return rho
ansf, errf=scipy.integrate.tplquad(ABDCFO,0,5,lambda rho:0,
  lambda rho:scipy.pi/2,lambda rho,phi:0,lambda rho,phi:10)
ansff=ansf/scipy.pi         #answer in multiples of pi

#Results

print 'The distance BC =',ansa
print 'The distance CD =',ansbb,'pi'
print 'The surface area ABCD =',anscc,'pi'
print 'The surface area ABO =',ansdd,'pi'
print 'The surface area AOFD =',anse
print 'The volume ABDCFO =',ansff,'pi'
The distance BC = 10.0
The distance CD = 2.5 pi
The surface area ABCD = 25.0 pi
The surface area ABO = 6.25 pi
The surface area AOFD = 50.0
The volume ABDCFO = 62.5 pi

Example 3.2, Page number: 61

In [2]:
 
import scipy
from numpy import *
import scipy.integrate
from fractions import Fraction

#Variable Declaration

ax=array([1,0,0])             #Unit vector along x direction
ay=array([0,1,0])             #Unit vector along y direction
az=array([0,0,1])             #Unit vector along z direction

#Calculations

def C1(x): 
 return x**2
seg1, err1 = scipy.integrate.quad(C1, 1, 0)     #segment 1
Seg1=Fraction(seg1).limit_denominator(100)      #converting to fraction

def C2(y): 
 return 0
seg2, err2 = scipy.integrate.quad(C2, 0, 1)     #segment 2

def C3(x): 
 return (x**2-1)
seg3, err3 = scipy.integrate.quad(C3, 0, 1)     #segment 3
Seg3=Fraction(seg3).limit_denominator(100)      #converting to fraction

def C4(y): 
 return (-y-y**2)
seg4, err4 = scipy.integrate.quad(C4, 1, 0)     #segment 4
Seg4=Fraction(seg4).limit_denominator(100)      #converting to fraction

seg=Seg1+seg2+Seg3+Seg4                         #total circulation around path
Seg=Fraction(seg).limit_denominator(100)        #converting to fraction

#Results

print 'F along segment 1 is',Seg1
print 'F along segment 2 is',seg2
print 'F along segment 3 is',Seg3
print 'F along segment 4 is',Seg4
print 'Circulation of F around the path is',Seg
F along segment 1 is -1/3
F along segment 2 is 0.0
F along segment 3 is -2/3
F along segment 4 is 5/6
Circulation of F around the path is -1/6

Example 3.4, Page number: 68

In [6]:
 
import scipy
from fractions import Fraction

#Variable Declaration

ax=array([1,0,0])             #Unit vector along x direction
ay=array([0,1,0])             #Unit vector along y direction
az=array([0,0,1])             #Unit vector along z direction
Al=array([3,4,12])
x=2
y=-1
z=0

#Calculations

gradW=(2*x*y**2+y*z)*ax+(2*x**2*y+x*z)*ay+(x*y)*az
gradWl=Fraction(dot(gradW,Al)/scipy.sqrt(dot(Al,Al))).limit_denominator(1000)

#Result

print 'dW/dl =',gradWl
dW/dl = -44/13

Example 3.7, Page number: 74

In [9]:
 
import scipy
import scipy.integrate

#Variable Declaration

ap=array([1,0,0])              #Unit vector along radial direction
az=array([0,0,1])              #Unit vector along z direction

#Calculations

def psi1(phi,p): 
 return 10*scipy.e**(-2)*p
psit, errt = scipy.integrate.dblquad(lambda p , phi: psi1(phi,p),    #flux through top
             0, 2*scipy.pi, lambda p: 0, lambda p: 1) 

def psi2(phi,p): 
 return -10*p
psib, errb = scipy.integrate.dblquad(lambda p , phi: psi2(phi,p),    #flux through bottom
             0, 2*scipy.pi, lambda p: 0, lambda p: 1) 

def psi3(phi,z): 
 return 10*scipy.exp(-2*z)
psis, errs = scipy.integrate.dblquad(lambda z , phi: psi3(phi,z),    #flux through side
             0, scipy.pi*2, lambda z: 0, lambda z: 1) 

psi=psit+psib+psis    #total flux through cylinder

#Results

print 'The total flux through the cylinder is',psi
print 'The total flux through cylinder using divergence theorem is also 0'
The total flux through the cylinder is 0.0
The total flux through cylinder using divergence theorem is also 0

Example 3.9, Page number: 81

In [3]:
 
import scipy
from numpy import *
import scipy.integrate

#Variable Declaration

ap=array([1,0,0])              #Unit vector along radial direction
ath=array([0,1,0])             #Unit vector along theta direction
aph=array([0,0,1])             #Unit vector along phi direction

#Calculations

 #segment 1
def ab(phi): 
 return 2*scipy.sin(phi)
seg1,err1 = scipy.integrate.quad(ab,scipy.pi*60/180,scipy.pi*30/180) 

 #segment 2
def bc(p): 
 return p*scipy.cos(scipy.pi*30/180)
seg2,err2 = scipy.integrate.quad(bc,2,5)    

 #segment 3
def cd(phi): 
 return 5*scipy.sin(phi)
seg3,err3 = scipy.integrate.quad(cd,-scipy.pi*30/180,scipy.pi*60/180)

 #segment 4
def da(p): 
 return p*scipy.cos(scipy.pi*60/180)
seg4,err4 = scipy.integrate.quad(da,5,2)

I1=seg1+seg2+seg3+seg4

 #using stoke's theorem

def curlA(phi,p): 
 return ((1+p)*scipy.sin(phi))
I2, err = scipy.integrate.dblquad(lambda p , phi: curlA(phi,p),    
             scipy.pi*30/180, scipy.pi*60/180, lambda p: 2, lambda p: 5)

#Results

print 'The integral calculated segment wise =',round(I1,3)
print 'The integral calculated using Stokes Theorem =',round(I2,3)
print 'Since I1 = I2, Stokes theorem is verified'
The integral calculated segment wise = 4.941
The integral calculated using Stokes Theorem = 4.941
Since I1 = I2, Stokes theorem is verified