# forelse.py
# Author: Alex Gezerlis
# Numerical Methods in Physics with Python (CUP, 2020)
def look(target,names):
for name in names:
if name==target:
val = name
break
else:
val = None
return val
names = ["Alice", "Bob", "Eve"]
print(look("Eve", names))
print(look("Jack", names))
# plotex.py
# Author: Alex Gezerlis
# Numerical Methods in Physics with Python (CUP, 2020)
import matplotlib.pyplot as plt
def plotex(cxs,cys,dxs,dys):
plt.xlabel('x', fontsize=20)
plt.ylabel('f(x)', fontsize=20)
plt.plot(cxs, cys, 'r-', label='quadratic')
plt.plot(dxs, dys, 'b--^', label='other function')
plt.legend()
plt.show()
cxs = [0.1*i for i in range(60)]
cys = [x**2 for x in cxs]
dxs = [i for i in range(7)]
dys = [x**1.8 - 0.5 for x in dxs]
plotex(cxs, cys, dxs, dys)
# vectorfield.py
# 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)