import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
from IPython.display import HTML
Poisson’s Equation (Voltage)
\[\nabla V = -\frac{1}{\epsilon_0}\rho\]
Import required libraries
Set up the data required
= 50
size = np.zeros((size, size))
voltages = 50
V0 = -0.5
cond 10, 10] = V0 voltages[
The partial differential equation we are solving is \[ \nabla V = -\frac{1}{\epsilon_0}\rho \]
the right hand side is cond
. Then we can solve this with the boundaries of a 50x50 having a voltage of 0. To solve this we can use the following formula for \(u_{i,j}\) \[
u_{i,j} = \frac{1}{4}(u_{i,j+1}+u_{i+1,j}+u_{i,j-1}+u_{i-1,j}-cond)
\]
for _ in range(500):
for i in range(size):
for j in range(size):
= voltages[i, j + 1] if j + 1 < size else 0
u1 = voltages[i, j - 1] if j - 1 > -1 else 0
u2 = voltages[i + 1, j] if i + 1 < size else 0
u3 = voltages[i - 1, j] if i - 1 > -1 else 0
u4 = (u1 + u2 + u3 + u4 - cond) / 4 voltages[i, j]
Then we get the gradient of this to find the electric field
= np.array(range(size))
pos
= np.gradient(-voltages, pos, pos) dy, dx
Then we graph it
= 5 # Number of points to skip
skip
plt.figure()
plt.imshow(abs(voltages),
np.=(0, size, 0, size),
extent="lower",
origin="viridis",
cmap
)="Voltage")
plt.colorbar(label
plt.quiver(
pos[::skip],
pos[::skip],
dx[::skip, ::skip],
dy[::skip, ::skip],="r",
color=3,
headlength
)"Scalar Field using contour")
plt.title("X-axis")
plt.xlabel("Y-axis")
plt.ylabel(
plt.show()