patternpythonMinor
Calculating a table of deBroglie wavelengths for various electron energies
Viewed 0 times
variousenergieselectroncalculatingdebroglieforwavelengthstable
Problem
Here is the formula for the deBroglie wavelength of an electron versus its kinetic energy:
$$ \lambda(E_k) = h\left/\sqrt{\frac{(E_k+m_eC^2)^2-m_e^2C^4}{C^2}}\right.$$
and here is simple script that prints a CSV with plot data:
The
$$ \lambda(E_k) = h\left/\sqrt{\frac{(E_k+m_eC^2)^2-m_e^2C^4}{C^2}}\right.$$
and here is simple script that prints a CSV with plot data:
#!/usr/bin/python
import csv
from scipy import constants as spc
import numpy as np
vev = np.arange(1,1e6,100);
vWavelength = map( lambda n: \
spc.h/np.sqrt( (np.power(spc.eV*n+spc.m_e*np.power(spc.c,2),2)-np.power(spc.m_e,2)*np.power(spc.c,4))/np.power(spc.c,2) ), \
vev );
np.savetxt('plotdata/1a.csv', np.transpose(np.array([vev, vWavelength])));The
vWavelength line is abysmal, in my opinion. How can I make this code better?Solution
First, simplify: factor out \$C^4\$ out of square root:
\$\lambda(E_k) = h\left/C\sqrt{(\frac{E_k}{C^2}+m_e)^2-m_e^2}\right.\$
Simplify even more: factor out \$m_e^2\$:
\$\lambda(E_k) = h\left/C m_e\sqrt{(\frac{E_k}{(C m_e)^2}+1)^2-1}\right.\$
Do not put everything in
\$\lambda(E_k) = h\left/C\sqrt{(\frac{E_k}{C^2}+m_e)^2-m_e^2}\right.\$
Simplify even more: factor out \$m_e^2\$:
\$\lambda(E_k) = h\left/C m_e\sqrt{(\frac{E_k}{(C m_e)^2}+1)^2-1}\right.\$
Do not put everything in
lambda:# These are all constants; no need to recompute them.
# Consider putting them into a namespace.
# I didn't think too hard to figure appropriate names; my physics is too rusty
CM = spc.c * spc.me
e1 = spc.ev/(CM * CM)
scale = spc.h / CM
# This is an actual computation.
# Can be converted to lambda, but I strongly advise against.
def deBroglieWaveLength(n):
arg = e1 * n + 1.0
return scale / np.sqrt(arg * arg - 1)Code Snippets
# These are all constants; no need to recompute them.
# Consider putting them into a namespace.
# I didn't think too hard to figure appropriate names; my physics is too rusty
CM = spc.c * spc.me
e1 = spc.ev/(CM * CM)
scale = spc.h / CM
# This is an actual computation.
# Can be converted to lambda, but I strongly advise against.
def deBroglieWaveLength(n):
arg = e1 * n + 1.0
return scale / np.sqrt(arg * arg - 1)Context
StackExchange Code Review Q#143580, answer score: 9
Revisions (0)
No revisions yet.