In [1]:
#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()
In [2]:
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()
C:\Users\Phazer\Anaconda3\lib\site-packages\ipykernel_launcher.py:18: RuntimeWarning: divide by zero encountered in true_divide
In [3]:
#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()
In [4]:
#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")
Lyman series / Balmer series / Paschen series
Series for m = 1
   121.48889773776683  nm
   102.50625746624077  nm
   97.19111819021346  nm
   94.91320135763033  nm
   93.72000682627727  nm
Series for m = 2
   656.0400477839408  nm
   485.95559095106734  nm
   433.88892049202434  nm
   410.0250298649631  nm
   396.8637326100383  nm
Series for m = 3
   1874.4001365255456  nm
   1281.3282183280096  nm
   1093.4000796399016  nm
   1004.5613231691594  nm
   954.240069503914  nm
MF = (c * eps0 * h / e_charge^2)^1/5 ~= 2.329 seconds
In [5]:
#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()
In [6]:
#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()
In [7]:
#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
In [8]:
#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()
C:\Users\Phazer\Anaconda3\lib\site-packages\ipykernel_launcher.py:32: RuntimeWarning: invalid value encountered in true_divide
In [9]:
#Legendre polynomials
import scipy.special as sp
for n in range (1, 10):
    print(sp.legendre(n))
 
1 x
     2
1.5 x - 0.5
     3
2.5 x - 1.5 x
       4             3        2
4.375 x + 4.857e-16 x - 3.75 x + 2.429e-16 x + 0.375
       5        3             2
7.875 x - 8.75 x - 4.372e-16 x + 1.875 x
       6         4             3         2
14.44 x - 19.69 x + 1.603e-15 x + 6.562 x - 0.3125
       7             6         5             4         3             2
26.81 x + 5.954e-15 x - 43.31 x + 2.977e-15 x + 19.69 x - 1.116e-15 x - 2.187 x
       8             7         6             5         4             3
50.27 x - 5.581e-15 x - 93.84 x - 2.233e-14 x + 54.14 x - 5.581e-15 x
          2
 - 9.844 x - 4.361e-17 x + 0.2734
       9             8         7             6         5             4
94.96 x - 2.109e-14 x - 201.1 x + 6.326e-14 x + 140.8 x + 1.581e-14 x
          3             2
 - 36.09 x - 1.977e-15 x + 2.461 x
In [35]:
#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()