Skip to content

Instantly share code, notes, and snippets.

@abcsds
Last active August 15, 2017 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abcsds/9221431 to your computer and use it in GitHub Desktop.
Save abcsds/9221431 to your computer and use it in GitHub Desktop.
Calculate potential of electrical dipole
import numpy as np
print "Calculate n terms of dipolar potential: "
n = int(raw_input("How many terms? "))
q = float(raw_input("What's the charges(C)? "))
l = float(raw_input("What's the distance between the charges(m)? "))
r = float(raw_input("What's the distance to the dipole(m)? "))
#theta = float(raw_input("What's the angle to the dipole in radians? "))
f0 = []
i = 1.0
j = -1
w = 0.0
e0 = 8.854187817
f0.append(i)
for x in range(n):
i = np.divide(float(i*j),2)
j -= 2
#print j
f0.append(i)
# print f0
angles = [0,(np.pi/2),(np.pi),(3*np.pi/2),(2*np.pi)]
for theta in angles:
x = (-2*r*l*np.cos(theta))/(r*r)
w = 0.0
for i in range(n):
w+= x * f0[i]/np.math.factorial(i)
w = w*(q/(4*np.pi*e0/1000000000000))
print "U(r) = ",w," for angle ", theta
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment