# Electric-fields.ipynb

In [1]:
from sympy import *
from sympy.vector import CoordSys3D, Vector
import matplotlib.pyplot as plt
import numpy as np

In [2]:
E, epsilon_0, q, q_1, q_2 = symbols('E, epsilon_0, q, q_1, q_2')
r, vec_r, univec_r = symbols('r, {\overrightarrow{{r}}} , {\widehat{{r}}}')
r_A, vec_r_A, univec_r_A = symbols('r_A, {\overrightarrow{{r_A}}} , {\widehat{{r_A}}}')
r_B, vec_r_B, univec_r_B = symbols('r_B, {\overrightarrow{{r_B}}} , {\widehat{{r_B}}}')

In [3]:
Electric_field_Law = Eq(E, 1/(4*pi*epsilon_0) * (q/r**3) * vec_r)
Electric_field_Law

Out[3]:
$\displaystyle E = \frac{q {\overrightarrow{{r}}}}{4 \pi \epsilon_{0} r^{3}}$
In [4]:
value_of_epsilon_0 = Eq(epsilon_0, 8.85148781762039e-12)
value_of_epsilon_0

Out[4]:
$\displaystyle \epsilon_{0} = 8.85148781762039 \cdot 10^{-12}$
In [5]:
N = CoordSys3D('N')


### If we have a 2$C$ point charge at point (1, 3) then what will be the field strength at (-2, -4)

In [55]:
A = np.array([3, -5])
B = np.array([-5, 2])

In [56]:
distanceVec = B - A
distanceSca = np.linalg.norm(distanceVec)

In [57]:
plt.figure(figsize = (6, 6))
plt.plot(A[0], A[1], marker = 'o', color = 'blue', markersize = 8, label = 'A - q = 2$C$')
plt.annotate('A', xy = (A[0] + 0.5, A[1]), fontsize = 14)

plt.plot(B[0], B[1], marker = 'o', color = 'red', markersize = 8, label = 'B - Field strength at B')
plt.annotate('B', xy = (B[0] + 0.5, B[1]), fontsize = 14)

plt.arrow(A[0], A[1], distanceVec[0], distanceVec[1], width = 0.15, color = 'lime', label = distanceVec, length_includes_head = True)
plt.annotate(round(distanceSca, 2), xy = (((B[0] + A[0])/2) + 0.5, (B[1] + A[1])/2), fontsize = 14)

#required
plt.legend()
plt.xlim(-6, 6)
plt.ylim(-6, 6)
plt.xticks(np.arange(-6, 7))
plt.yticks(np.arange(-6, 7))
plt.grid()
plt.gca().set_aspect('equal')

In [58]:
vec_r_A = A[0]*N.i + A[1]*N.j
vec_r_A

Out[58]:
$\displaystyle (3)\mathbf{\hat{i}_{N}} + (-5)\mathbf{\hat{j}_{N}}$
In [59]:
vec_r_B = B[0]*N.i + B[1]*N.j
vec_r_B

Out[59]:
$\displaystyle (-5)\mathbf{\hat{i}_{N}} + (2)\mathbf{\hat{j}_{N}}$
In [60]:
vec_r_eq = Eq(vec_r, vec_r_B - vec_r_A, evaluate = False)
vec_r_eq

Out[60]:
$\displaystyle {\overrightarrow{{r}}} = (-8)\mathbf{\hat{i}_{N}} + (7)\mathbf{\hat{j}_{N}}$
In [61]:
r_eq = Eq(r, (vec_r_B - vec_r_A).magnitude())
r_eq

Out[61]:
$\displaystyle r = \sqrt{113}$
In [62]:
q_eq = Eq(q, 2)
q_eq

Out[62]:
$\displaystyle q = 2$
In [65]:
field1 = Electric_field_Law.subs({
epsilon_0: value_of_epsilon_0.rhs,
q: q_eq.rhs,
vec_r: vec_r_eq.rhs,
r: r_eq.rhs
})
field1.n(4)

Out[65]:
$\displaystyle E = (-1.198 \cdot 10^{8})\mathbf{\hat{i}_{N}} + (1.048 \cdot 10^{8})\mathbf{\hat{j}_{N}}$
In [67]:
plt.figure(figsize = (6, 6))
plt.plot(A[0], A[1], marker = 'o', color = 'blue', markersize = 8, label = 'A - q = 2$C$')
plt.annotate('A', xy = (A[0] + 0.5, A[1]), fontsize = 14)

plt.plot(B[0], B[1], marker = 'o', color = 'red', markersize = 8, label = f'B - ${latex(field1.n(4))}$')
plt.annotate('B', xy = (B[0] + 0.5, B[1]), fontsize = 14)

plt.arrow(A[0], A[1], distanceVec[0], distanceVec[1], width = 0.15, color = 'lime', label = distanceVec, length_includes_head = True)
plt.annotate(round(distanceSca, 2), xy = (((B[0] + A[0])/2) + 0.5, (B[1] + A[1])/2), fontsize = 14)

#required
plt.legend()
plt.xlim(-6, 6)
plt.ylim(-6, 6)
plt.xticks(np.arange(-6, 7))
plt.yticks(np.arange(-6, 7))
plt.grid()
plt.gca().set_aspect('equal')

In [ ]:


In [ ]: