Skip to content

Commit be205be

Browse files
authored
Add files via upload
1 parent 9faccf6 commit be205be

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

electrical_field_2_charges.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Electrical field due to 2 point charges
2+
#
3+
# |
4+
# q1 o
5+
# | + observation point
6+
# |
7+
# -----------------------------------------
8+
# |
9+
# |
10+
# q2 o
11+
# |
12+
#
13+
# rq1: vector to point charge 1
14+
# rq2: vector to point charge 2
15+
# ro: vector to observation point
16+
# r1o: vector from point charge 1 to observation point o
17+
# r2o: vector from point charge 2 to observation point o
18+
#
19+
# ro = rq1 + r1o
20+
# => r1o = ro - rq1
21+
# r2o = ro - rq2
22+
#
23+
# q1: charge value of point charge 1
24+
# q2: charge value of point charge 2
25+
# εo: permittivity of a vacuum,
26+
# 8.854 x 10⁻¹² F/m (Farads per meter)
27+
# E(ro): electric field at observation point ro
28+
#
29+
# Electrical field at ro:
30+
# E = 1/(4*pi*εo) * (r1o * q1 / |r1o|³ + r2o * q2 / |r2o|³)
31+
#
32+
#
33+
# | x1o = xo - xq1
34+
# | y1o = yo - yq1
35+
#
36+
# | x2o = xo - xq2
37+
# | y2o = yo - yq2
38+
#
39+
# | Ex = 1/(4*pi*εo) * ( (xo - xq1) * q1 / |ro - rq1|³ + (xo - xq2) * q2 / |ro - rq2|³ )
40+
# | Ey = 1/(4*pi*εo) * ( (yo - yq1) * q1 / |ro - rq1|³ + (yo - yq2) * q2 / |ro - rq2|³ )
41+
#
42+
# Electrical potential
43+
# V = 1/(4*pi*εo) * ( q1 / r1o + q2 / r2o )
44+
# V = 1/(4*pi*εo) * ( q1 / (ro - rq1) + q2 / (ro - rq2) )
45+
46+
import numpy as np
47+
import matplotlib.pyplot as plt
48+
49+
# parameters
50+
d = 0.01 # distance point charges from origin
51+
L = 2*d # distance between point charges
52+
q1 = 1; q2 = -1 # value point charges [coulomb]
53+
N = 20 # number of points calculated, result arrays will be N*N
54+
epsilon = 8.854E-12 # [F/m]
55+
#k = 1/(4 * np.pi * epsilon) # physical value
56+
k = 1 # used so E is scaled with 1/(4 * np.pi * epsilon)
57+
58+
# preparing vectors with coordinates of point charges [x, y]
59+
rq1 = np.array([0, +d / 2])
60+
rq2 = np.array([0, -d / 2])
61+
62+
# generate X and Y arrays
63+
x = np.linspace(-L, L, N)
64+
y = np.linspace(-L, L, N)
65+
X,Y = np.meshgrid(x,y)
66+
67+
# prepare arrays for x and y components of E
68+
Ex = np.zeros([N,N])
69+
Ey = np.zeros([N,N])
70+
71+
# prepare array for V,V will be limted to Vmax
72+
V = np.zeros([N,N])
73+
Vmax = 100
74+
75+
# function calculates x and y components of E
76+
# argument of function is vector ro with 2 coordinates
77+
# np.linalg.norm() gives the magnitude of the vector in this case
78+
def calc_E(ro):
79+
Ex = k * ( (ro[0] - rq1[0]) * q1 / np.linalg.norm(ro - rq1)**3
80+
+ (ro[0] - rq2[0]) * q2 / np.linalg.norm(ro - rq2)**3 )
81+
Ey = k * ( (ro[1] - rq1[1]) * q1 / np.linalg.norm(ro - rq1)**3
82+
+ (ro[1] - rq2[1]) * q2 / np.linalg.norm(ro - rq2)**3 )
83+
return Ex, Ey
84+
85+
# function calculates V
86+
# argument is vector ro with 2 coordinates
87+
def calc_V(ro):
88+
Vt = k * ( q1 / np.linalg.norm(ro - rq1) + q2 / np.linalg.norm(ro - rq2) )
89+
return Vt
90+
91+
# calling calc_E() and calc_V() for each point
92+
for row in range(N):
93+
for column in range(N):
94+
ro = np.array([X[row,column], Y[row,column]])
95+
Ex[row,column], Ey[row,column] = calc_E(ro)
96+
V[row,column] = calc_V(ro)
97+
98+
# calculating array containing magnitude of E
99+
E = np.sqrt(Ex**2 + Ey**2)
100+
101+
# limiting V to +-Vmax to aid contour plot
102+
V = np.clip(V, -Vmax, Vmax)
103+
104+
# plotting
105+
plt.rcParams.update({'font.size': 15})
106+
plt.figure(figsize=(11, 9))
107+
plt.quiver(X, Y, Ex/E, Ey/E)
108+
plt.contour(X, Y, V, levels = 15)
109+
plt.scatter(rq1[0], rq1[1], marker = "o", color = "red")
110+
plt.scatter(rq2[0], rq2[1], marker = "o", color = "blue")
111+
plt.title("Electric field / (1/(4*pi*εo)) [V/m]\nElectric potential [V]")
112+
plt.xlabel("E/(1/(4*pi*εo)) [V/M]\nV [V]")
113+
plt.ylabel("E/(1/(4*pi*εo)) [V/M]\nV [V]")
114+
plt.show()
115+
116+
117+
118+
119+
290 KB
Loading

0 commit comments

Comments
 (0)