forked from agezerlis/NumericalMethodsPhysicsWithPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvectorfield.py
More file actions
34 lines (30 loc) · 980 Bytes
/
vectorfield.py
File metadata and controls
34 lines (30 loc) · 980 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Author: Alex Gezerlis
# Numerical Methods in Physics with Python (CUP, 2020)
import numpy as np
import matplotlib.pyplot as plt
from math import sqrt
from copy import deepcopy
def makefield(xs, ys):
qtopos = {1: (-1,0), -1: (1,0)}
n = len(xs)
Exs = [[0. for k in range(n)] for j in range(n)]
Eys = deepcopy(Exs)
for j,x in enumerate(xs):
for k,y in enumerate(ys):
for q,pos in qtopos.items():
posx, posy = pos
R = sqrt((x - posx)**2 + (y - posy)**2)
Exs[k][j] += q*(x - posx)/R**3
Eys[k][j] += q*(y - posy)/R**3
return Exs, Eys
def plotfield(boxl,n):
xs = [-boxl + i*2*boxl/(n-1) for i in range(n)]
ys = xs[:]
Exs, Eys = makefield(xs, ys)
xs=np.array(xs); ys=np.array(ys)
Exs=np.array(Exs); Eys=np.array(Eys)
plt.streamplot(xs, ys, Exs, Eys, density=1.5, color='m')
plt.xlabel('$x$')
plt.ylabel('$y$')
plt.show()
plotfield(2.,20)