#template for plotting vector fields
import numpy as np
import matplotlib.pyplot as plt
n = 8
X, Y = np.mgrid[0:n, 0:n]
T = np.arctan2(Y-n / 2., X- n/2.)
R = 10 + np.sqrt((Y-n / 2.0)**2 + (X-n / 2.0)**2)
U, V = R * np.cos(T), R * np.sin(T)
plt.axes([0.025, 0.025, 0.95, 0.95])
plt.quiver(X, Y, U, V, R, alpha=0.5)
plt.quiver(X, Y, U, V, edgecolor='k', facecolor='None', linewidth=.5)
plt.xlim(-1, n)
plt.xticks(())
plt.ylim(-1, n)
plt.yticks(())
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
#something
h0 = 2277
R = 4
x = y = np.linspace(-10., 10., 41)
xv, yv = np.meshgrid(x, y, indexing='ij', sparse=False)
hv = h0/(1 + (xv**2 + yv**2)/(R**2))
#parameterized curve
s = np.linspace(0, 2*np.pi, 100)
curve_x = 10*(1-s/(2*np.pi))*np.cos(s)
curve_y = 10*(1-s/(2*np.pi))*np.sin(s)
curve_z = h0/(1+100*(1/s/(2*np.pi))**2/(R**2))
#plot
fig = plt.figure(1)
ax = fig.gca(projection='3d')
ax.plot_wireframe(xv, yv, hv, rstride=2, cstride=2)
fig = plt.figure(2)
ax = fig.gca(projection='3d')
ax.plot_surface(xv, yv, hv, cmap=cm.coolwarm, rstride=1, cstride=1)
ax.plot(curve_x, curve_y, curve_z, linewidth=5)
plt.show()
#template for plotting E&M fields
import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle, Arrow
def E(q, r0, x, y):
"""Return the electric field vector E=(Ex,Ey) due to charge q at r0."""
den = np.hypot(x-r0[0], y-r0[1])**3
return q * (x - r0[0]) / den, q * (y - r0[1]) / den
# Grid of x, y points
nx, ny = 64, 64
x = np.linspace(-2, 2, nx)
y = np.linspace(-2, 2, ny)
X, Y = np.meshgrid(x, y)
# Create a multipole with nq charges of alternating sign, equally spaced
# on the unit circle.
#Number of charges
#nq = 2**int(sys.argv[1])
nq = 4
charges = []
for i in range(nq):
q = i%2 * 2 - 1
charges.append((q, (np.cos(2*np.pi*i/nq), np.sin(2*np.pi*i/nq))))
# Electric field vector, E=(Ex, Ey), as separate components
Ex, Ey = np.zeros((ny, nx)), np.zeros((ny, nx))
for charge in charges:
ex, ey = E(*charge, x=X, y=Y)
Ex += ex
Ey += ey
fig = plt.figure()
ax = fig.add_subplot(111)
# Plot the streamlines with an appropriate colormap and arrow style
color = 2 * np.log(np.hypot(Ex, Ey))
ax.streamplot(x, y, Ex, Ey, color=color, linewidth=1, cmap=plt.cm.inferno,
density=2, arrowstyle='->', arrowsize=1.5)
# Add filled circles for the charges themselves
charge_colors = {True: '#aa0000', False: '#0000aa'}
for q, pos in charges:
ax.add_artist(Circle(pos, 0.05, color=charge_colors[q>0]))
#line visual
#ax.add_artist(Arrow(-2, -2, 0, 0))
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')
ax.set_xlim(-2,2)
ax.set_ylim(-2,2)
ax.set_aspect('equal')
plt.show()
#Rydberg forumula
#Prints out the wavelengths of hydrogen lines
#R = 1.09737e-2 #in nm
c = 299792458 #speed of light (m/s)
h = 6.62607004e-34 #plank's constant (m^2*kg/s)
e_mass = 9.11e-31 #electron's mass (kg)
eps0 = 8.854e-12 #vacuum permittitvity (F/m)
e_charge = 1.60217662e-19 #electron's charge (C)
#print("f = c*eps0")
#f= c*eps0
#print(f)
#print(c*eps0)
#print("F/s")
#print(" ")
#print("f2 = f * h")
#f2 = f*h
#print(f2)
#print("s^3 A^2")
#print("f3 = f2 / e_charge^2")
#f3 = f2 / e_charge**2
#print(f3)
#print("s^5")
#print("root(5)of_f3")
#print(f3**(1/5))
#print("s")
#print("~2.329 seconds")
#fe = f*e_mass
#print(fe)
#print("s4A2m-2kg-2")
#print("F/s")
#Rydberg constant
R = ((e_mass * e_charge**4) / (8 * eps0**2 * h**3 * c))*1e-9
def hydrogen_spectral():
print("Lyman series / Balmer series / Paschen series")
for m in range(1,4):
print("Series for m =",m)
for n in range(m+1,m+6):
invlambda = R*(1/m**2-1/n**2)
print(" ",1/invlambda," nm")
hydrogen_spectral()
print("MF = (c * eps0 * h / e_charge^2)^1/5 ~= 2.329 seconds")
#spherical coords.
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as axes3d
theta, phi = np.linspace(0, 2 * np.pi, 40), np.linspace(0, np.pi, 40)
THETA, PHI = np.meshgrid(theta, phi)
R = np.cos(PHI**2)
X = R * np.sin(PHI) * np.cos(THETA)
Y = R * np.sin(PHI) * np.sin(THETA)
Z = R * np.cos(PHI)
fig = plt.figure()
ax = fig.add_subplot(1,1,1, projection='3d')
plot = ax.plot_surface(
X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('jet'),
linewidth=0, antialiased=False, alpha=0.5)
plt.show()
#vector field plotting via matplotlib
import matplotlib.pyplot as plt
import numpy as np
from numpy import ma
X, Y = np.meshgrid(np.arange(0, 2 * np.pi, .2), np.arange(0, 2 * np.pi, .2))
U = 0 #x-axis
V = X #y-axis
#other examples would inclue U = np.cos(x); V = np.sin(Y)
plt.figure()
plt.title('Arrows scale with plot width, not view')
Q = plt.quiver(X, Y, U, V, units='width')
qk = plt.quiverkey(Q, 0.9, 0.9, 2, r'$2 \frac{m}{s}$', labelpos='E',
coordinates='figure')
plt.show()
#using sympy package to plot equations
from sympy.physics.vector import ReferenceFrame
R = ReferenceFrame('R')
from sympy.physics.vector import curl
field = R[0]*R[1]*R[2]*R.x
curl(field, R)
from sympy import symbols
from sympy.plotting import plot
x = symbols('x')
p1 = plot(x*x)
p2 = plot(x)
p1.append(p2[0])
#p1
#PHYS 340A HW#1
#due 1/31/2018
import numpy as np
import matplotlib.pyplot as plt
#print(plt.style.available)
plt.style.use('classic')
n = 10
X, Y = np.mgrid[-10:n, -10:n]
#U --> X
#V --> Y
#example usage -- ?? formula
#T = np.arctan2(Y-n / 2., X- n/2.)
#R = 10 + np.sqrt((Y-n / 2.0)**2 + (X-n / 2.0)**2)
#U, V = R * np.cos(T), R * np.sin(T)
#PROBLEMS
#----------------------
#(select one)
#A) x * y_hat
#U, V = 0, X
#B)
#U, V = X, Y
#C)
U, V = X/(np.sqrt(X**2 + Y**2)), Y/(np.sqrt(X**2 + Y**2))
plt.axes([0.025, 0.025, 0.95, 0.95])
#plt.quiver(X, Y, U, V, R, alpha=0.5)
plt.quiver(X, Y, U, V, edgecolor='k', facecolor='None', linewidth=.9)
plt.xlim(-10, n)
plt.xticks(())
plt.ylim(-10, n)
plt.yticks(())
plt.show()
#Legendre polynomials
import scipy.special as sp
for n in range (1, 10):
print(sp.legendre(n))
#template for plotting E&M fields
import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle, Arrow
def E(q, r0, x, y):
"""Return the electric field vector E=(Ex,Ey) due to charge q at r0."""
den = np.hypot(x-r0[0], y-r0[1])**3
return q * (x - r0[0]) / den, q * (y - r0[1]) / den
# Grid of x, y points
nx, ny = 64, 64
x = np.linspace(-2, 2, nx)
y = np.linspace(-2, 2, ny)
X, Y = np.meshgrid(x, y)
# Create a multipole with nq charges of alternating sign, equally spaced
# on the unit circle.
#Number of charges
#nq = 2**int(sys.argv[1])
nq = 40
charges = []
for i in range(nq):
#alternating charges
#q = i%2 * 2 - 1
q = -1
charges.append((q, (np.cos(2*np.pi*i/nq), np.sin(2*np.pi*i/nq))))
#charges = []
charges.append((40, (0,0)))
#---4 point charges HW PROB 1
#charges.append((1, (1, 1)))
#charges.append((-1, (-1, 1)))
#charges.append((1, (-1, -1)))
#charges.append((-1, (1, -1)))
#HW PROB 2
#charges.append((1, (1, np.tan(np.pi/6))))
#charges.append((-1, (1, -np.tan(np.pi/6))))
#charges.append((-1, (0, 1)))
#charges.append((1, (-1, -1)))
#-------------------
#T/B
#charges.append((1, (0, 1)))
#charges.append((-1, (0, -1)))
#L
#charges.append((-1, (-1, 1/np.cos(np.pi/6))))
#charges.append((1, (-1, -1/np.tan(np.pi/6))))
#R
#charges.append((-1, (1, 1/np.cos(np.pi/6))))
#charges.append((1, (1, -1/np.tan(np.pi/6))))
# Electric field vector, E=(Ex, Ey), as separate components
Ex, Ey = np.zeros((ny, nx)), np.zeros((ny, nx))
for charge in charges:
ex, ey = E(*charge, x=X, y=Y)
Ex += ex
Ey += ey
fig = plt.figure()
ax = fig.add_subplot(111)
# Plot the streamlines with an appropriate colormap and arrow style
color = 2 * np.log(np.hypot(Ex, Ey))
ax.streamplot(x, y, Ex, Ey, color=color, linewidth=1, cmap=plt.cm.inferno,
density=2, arrowstyle='->', arrowsize=1.5)
# Add filled circles for the charges themselves
charge_colors = {True: '#aa0000', False: '#0000aa'}
for q, pos in charges:
ax.add_artist(Circle(pos, 0.05, color=charge_colors[q>0.5]))
#line visual
#ax.add_artist(Arrow(-2, -2, 0, 0))
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')
ax.set_xlim(-2,2)
ax.set_ylim(-2,2)
ax.set_aspect('equal')
plt.show()