Synoptic Exercises
1. You have been provided with the melting and boiling points of various metals:
metals = ['Fe', 'Al', 'Hg', 'Ti', 'Pb']
melting_points = [1811, 933, 234, 1941, 601]
boiling_points = [3134, 2743, 630, 3560, 2022]Using a for loop, determine whether each metal is a solid, liquid or gas when \(T = 500\,\mathrm{K}\).
2. Recall that in Question 2 of Session 1’s Synoptic Exercises we calculated various lines in the hydrogen emission spectrum using the Rydberg equation.
Use the following function to calculate the wavelengths of the first ten lines in each of the Lyman, Balmer and Paschen series of hydrogen and store them in separate lists using a list comprehension.
from scipy import constants
def calc_rydberg_wavelength(n_i, n_f):
'''
Calculates wavelength of hydrogen emission signal corresponding to a transition
between two energy levels with quantum numbers
n_i and n_f using the Rydberg equation.
λ = (R (1/n_f^2 - 1/n_i^2))
Arguments
---------
n_i: int
Quantum number of intial state (n_i>n_f)
n_f: int
Quantum number of final state (n_i>n_f)
Returns
-------
float
Wavelength of transition in nm
'''
iwavelength = constants.Rydberg * (1/n_f**2 - 1/n_i**2)
wavelength = 1 / iwavelength
wavelength *= 10**9
return wavelength3. There are several infinite series that converge to something proportional to \(\pi\). For example, the Basel series is defined as an infinite sum of reciprocal square numbers:
\[\frac{1}{1^{2}} + \frac{1}{2^{2}} + \frac{1}{3^{2}} + \cdots = \sum_{k=1}^{\infty} \frac{1}{k^{2}} = \frac{\pi^{2}}{6}.\]
a) Write a function to approximate \(\pi\) by evaluating the Basel series up to a given term \(n\). Test your function for \(n = 1000\).
b) Another infinite series that converges to something proportional to \(\pi\) is the Leibniz formula:
\[1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \frac{1}{9} - \cdots = \sum_{k=0}^{\infty} \frac{(-1)^{k}}{2k + 1} = \frac{\pi}{4}.\]
Write a function to approximate \(\pi\) by evaluating the Leibniz formula up to a given term \(n\). Test your function for \(n = 1000\).
c) The accuracy of an approximate value of \(\pi\) is simply measured by calculating the absolute difference from the exact value:
\[|\pi - \pi_{\mathrm{approx}}|.\]
Write a function to evaluate the above expression and use this to determine which of your previous functions is more accurate when evaluated up to the \(50\)th term.
4. Consider the following blocks of code. Each one of them will cause an error when run. Taking each block one-by-one:
- Predict which line of code will cause the error and why this line is problematic.
- Run the block of code in a Jupyter notebook and see whether or not your were correct.
- Modify the code to solve the problem and thus remove the error.
Block 1
def calculate_product(sequence):
'''
Calculate the product of a sequence of numbers (n_1 * n_2 * n_3 * ...).
Arguments
---------
sequence: iterable
A sequence of numbers to multiply together.
Returns
-------
float
The cumulative product of the numbers in the sequence.
'''
product = sequence[0]
for number in sequence[1:]:
product *= number
return product
product_of_sequence = calculate_product(5.12, 6.79, 2.22, 0.19, 13.6)
print(f'The cumulative product = {product_of_sequence}')Block 2
This block also contains a problem which, whilst it will not cause an error, will lead to the wrong result. See if you can spot and fix this problem in addition to removing the error.
def check_palindrome(convert_to_lower=False, word):
'''
Check if a word is a palindrome.
Arguments
---------
word: str
The word to be checked.
convert_to_lower: bool, default False
If True, converts word to all lower case
before comparing to reverse form.
Returns
-------
bool
True if word is a palindrome, False otherwise.
'''
# Convert to lower case if requested
if convert_to_lower:
word = word.lower()
if word == word[::-1]:
check = True
else:
check = False
example_word = 'Civic'
if check_palindrome(example_word, convert_to_lower=True):
print(f'{example_word} is a palindrome.')
else:
print(f'{example_word} is not a palindrome.')Block 3
from scipy import constants
def harmonic_energy(v, omega):
'''
Calculate the energy of a quantum harmonic oscillator.
Arguments
---------
v: int
Quantum number labelling each energy eigenstate.
omega: float
Angular frequency of the oscillator in rad s^-1.
Returns
-------
float
The energy of the oscillator in Joules.
'''
energy = (v + 0.5) * constants.hbar * omega
return energy
# Angular frequency of HCl
frequency_HCl = 5.40e14 # rad s^-1
energies = [harmonic_energy([0, 1, 2], frequency_HCl)]
print(f'The zero-point energy of HCl = {energy} J')