mathgenerator.physics

  1import random
  2import math
  3
  4# Mechanics
  5def kinetic_energy(max_mass=1000, max_vel=100):
  6   r"""Kinetic Energy calculation using Ek = 0.5 * m * v^2
  7
  8    | Ex. Problem | Ex. Solution |
  9    | --- | --- |
 10    | What is the kinetic energy of an object of mass $5 kg$ and velocity $10 m/s$ | $250 J$ |
 11    """
 12   velocity = round(random.uniform(1, max_vel),2)
 13   mass = round(random.uniform(1, max_mass),2)
 14   kinetic_energy = round((0.5 * mass * velocity**2), 2)
 15
 16
 17   problem = f"What is the kinetic energy of an object of mass ${mass} kg$ and velocity ${velocity} m/s$?"
 18   solution = f'${kinetic_energy} J$'
 19   return problem, solution
 20
 21
 22# Electricity & Electric Fields
 23def potential_dividers(max_vin=50, max_resistance=500):
 24   r"""Potential Divider question using Vout = (Vin * R2) / (R2 + R1)
 25
 26    | Ex. Problem | Ex. Solution |
 27    | --- | --- |
 28    | In a Potential Divider, if resistors R1 and R2 have resistances of $100 \Omega$ and $50 \Omega$ respectively, and the cell has $12 V$ What is the output potential difference across R2? | $4 V$ |
 29    """
 30   '''
 31    This is what a potential divider circuit looks like:
 32    ------
 33    |    R1
 34 Vi =    |----o
 35    |    R2      Vout
 36    |____|____o
 37    '''
 38   vin = random.randint(0, max_vin)          # Voltage input of cell
 39   r1 = random.randint(0, max_resistance)    # Resistance of R1
 40   r2 = random.randint(0, max_resistance)    # Resistance of R2
 41   vout = round((vin * r2) / (r1 + r2),2)    # Voltage output across R2
 42
 43   problem = f"In a Potential Divider, if resistors R1 and R2 have resistances of ${r1} \\Omega$ and ${r2} \\Omega$ respectively, and the cell has ${vin} V$ What is the output potential difference across R2?"
 44   solution = f"${vout} V$"
 45   return problem, solution
 46
 47def resistivity(max_diameter_mm=5, max_length_cm=100, max_resistance=0.1):
 48   r"""Calculate the Resistivity using the equation R = (pL)/A, where R = Resistance, L = length of wire, p = resistivity and A = cross sectional area of wire
 49
 50    | Ex. Problem | Ex. Solution |
 51    | --- | --- |
 52    | A wire has resistance $30 m\Omega$ when it is $83.64 cm$ long with a diameter of $4.67 mm$. Calculate the resistivity of the wire | $6.14e-07 \Omega m$ |
 53    """
 54   # This question requires a lot of unit conversions and calculating the area of a circle from diameter
 55   diameter_mm = round(random.uniform(0, max_diameter_mm),2)   # Random diameter in mm
 56   cross_sectional_area = math.pi * (diameter_mm / 2000)**2    # Calculate the cross sectional area using pi r²
 57   length_cm = round(random.uniform(0, max_length_cm),2)       # Random wire length in cm
 58   resistance = round(random.uniform(0, max_resistance),2)     # Random reistance in ohms
 59
 60   resistivity = (resistance * cross_sectional_area) / (length_cm / 100)
 61
 62   problem = f"A wire has resistance ${resistance*1000} m\\Omega$ when it is ${length_cm} cm$ long with a diameter of ${diameter_mm} mm$. Calculate the resistivity of the wire"
 63   solution = f"${resistivity:.2e} \\Omega m$"
 64
 65   return problem, solution
 66
 67def electric_field_strength_two_points(max_seperation_cm=100, max_charge_uC=1000):
 68    r"""Calculate the total electric field strength at point P with given points A and B, using the equation kQ/r²
 69
 70    | Ex. Problem | Ex. Solution |
 71    | --- | --- |
 72    | Charges A and B and point P are arranged like this: B <-- 7 cm --> P <-- 79 cm --> A, Where A and B have charges of -56 µC and -410 µC, What is the electric field strength at point P? | $-751417824 NC^{-1}$ (to the right) |
 73
 74    """
 75    a_charge = random.randint(-max_charge_uC,max_charge_uC)
 76    b_charge = random.randint(-max_charge_uC,max_charge_uC)
 77    arrangement = [['P'],['A',a_charge],['B',b_charge]] # Arrangement of charge A, B and the point of focus
 78    random.shuffle(arrangement)
 79    seperations = [random.randint(0,max_seperation_cm), random.randint(0,max_seperation_cm)]
 80    total_efs = 0
 81    # Work out how far A and B are from P (vector)
 82    if arrangement[0][0] == 'P':
 83        arrangement[1].append(seperations[0])
 84        arrangement[2].append(seperations[0]+seperations[1])
 85    elif arrangement[1][0] == 'P':
 86        arrangement[0].append(-seperations[0])
 87        arrangement[2].append(seperations[1])
 88    else:
 89        arrangement[0].append(-(seperations[0]+seperations[1]))
 90        arrangement[1].append(-seperations[1])
 91
 92    # Work out the EFS at point P caused by A and B seperatley, then sum them together in `total_efs`
 93    for point in arrangement:
 94        if point[0] == 'P':
 95            continue
 96        else:
 97            efs = ((8.99*10**9)*(point[1]*10**-6))/((point[2]/100)**2) # efs = kQ/r²
 98            if point[2] > 0: efs = -efs
 99            point.append(efs)
100            total_efs += efs
101
102    problem = f"Charges A and B and point P are arranged like this:\n{arrangement[0][0]} <-- ${seperations[0]}$ cm --> {arrangement[1][0]} <-- ${seperations[1]}$ cm --> {arrangement[2][0]}\nWhere A and B have charges of ${a_charge}$ µC and ${b_charge}$ µC\nWhat is the electric field strength at point P?"
103    solution = f"${round(total_efs)} NC^{-1}$ (to the right)"
104    return problem, solution
105
106
107# Waves
108def fringe_spacing(max_screen_distance=30, max_slit_spacing_mm=100):
109   r"""Calculate the fringe spacing in a double slit experiment with w=(λD)/s
110    | Ex. Problem | Ex. Solution |
111    | --- | --- |
112    | A laser with a wavelength of $450nm$ is shone through a double slit system to produce an interference pattern on a screen.  The screen is $12m$ from the slits and the slits are $0.30mm$ apart. Calculate the spacing between the bright fringes. | Using the equation $\\frac{{\\lambda D}}{{s}}$, we get a fringe spacing of $0.018m$ |
113    """
114   wavelength_nm = random.randint(380,750)      # Random wavelength between violet and red (nm)
115   screen_distance = random.randint(0, max_screen_distance)    # Random distance between screen and slits (m)
116   slit_spacing_mm = random.randint(0, max_slit_spacing_mm)    # Random slit spacing (mm)
117
118   fringe_spacing = round((((wavelength_nm * 10**-9) * screen_distance) / (slit_spacing_mm * 10**-3)),5)
119
120   problem = f"A laser with a wavelength of ${wavelength_nm}nm$ is shone through a double slit system to produce an interference pattern on a screen.  The screen is ${screen_distance}m$ from the slits and the slits are ${slit_spacing_mm}mm$ apart. Calculate the spacing between the bright fringes."
121   solution = f"Using the equation $\\frac{{\\lambda D}}{{s}}$, we get a fringe spacing of ${fringe_spacing}m$"
122   return problem, solution
123
124def diffraction_grating_wavelength(min_slits_per_mm=100, max_slits_per_mm=500, max_order_number=5):
125    r"""Calculate the wavelength when given the number of slits per mm, order number and angle of order using the equation nλ = dsinθ
126
127    | Ex. Problem | Ex. Solution |
128    | --- | --- |
129    | A laser is shone through a diffraction grating which has $293$ lines per mm, the fringe of order number $2$ is at an angle of $0.39$ rad. Calculate the wavelength of the light | $\lambda = 6.487856913364529e-07m = 649nm |
130
131    """
132    slits_per_mm = random.randint(min_slits_per_mm, max_slits_per_mm)
133    slit_spacing = 1/(slits_per_mm * 1000)
134    order_number = random.randint(1, max_order_number)
135    angle_of_order = round(random.uniform(0.2, (math.pi/2)-0.2),2)
136    wavelength = ((slit_spacing * math.sin(angle_of_order)) / order_number)
137
138    problem = f"A laser is shone through a diffraction grating which has ${slits_per_mm}$ lines per mm, the fringe of order number ${order_number}$ is at an angle of ${angle_of_order}$ rad. Calculate the wavelength of the light"
139    solution = f"$\\lambda = {wavelength}m = {round(wavelength / 10**-9)}nm$"
140
141    return problem, solution
def kinetic_energy(max_mass=1000, max_vel=100):
 6def kinetic_energy(max_mass=1000, max_vel=100):
 7   r"""Kinetic Energy calculation using Ek = 0.5 * m * v^2
 8
 9    | Ex. Problem | Ex. Solution |
10    | --- | --- |
11    | What is the kinetic energy of an object of mass $5 kg$ and velocity $10 m/s$ | $250 J$ |
12    """
13   velocity = round(random.uniform(1, max_vel),2)
14   mass = round(random.uniform(1, max_mass),2)
15   kinetic_energy = round((0.5 * mass * velocity**2), 2)
16
17
18   problem = f"What is the kinetic energy of an object of mass ${mass} kg$ and velocity ${velocity} m/s$?"
19   solution = f'${kinetic_energy} J$'
20   return problem, solution

Kinetic Energy calculation using Ek = 0.5 * m * v^2

Ex. Problem Ex. Solution
What is the kinetic energy of an object of mass $5 kg$ and velocity $10 m/s$ $250 J$
def potential_dividers(max_vin=50, max_resistance=500):
24def potential_dividers(max_vin=50, max_resistance=500):
25   r"""Potential Divider question using Vout = (Vin * R2) / (R2 + R1)
26
27    | Ex. Problem | Ex. Solution |
28    | --- | --- |
29    | In a Potential Divider, if resistors R1 and R2 have resistances of $100 \Omega$ and $50 \Omega$ respectively, and the cell has $12 V$ What is the output potential difference across R2? | $4 V$ |
30    """
31   '''
32    This is what a potential divider circuit looks like:
33    ------
34    |    R1
35 Vi =    |----o
36    |    R2      Vout
37    |____|____o
38    '''
39   vin = random.randint(0, max_vin)          # Voltage input of cell
40   r1 = random.randint(0, max_resistance)    # Resistance of R1
41   r2 = random.randint(0, max_resistance)    # Resistance of R2
42   vout = round((vin * r2) / (r1 + r2),2)    # Voltage output across R2
43
44   problem = f"In a Potential Divider, if resistors R1 and R2 have resistances of ${r1} \\Omega$ and ${r2} \\Omega$ respectively, and the cell has ${vin} V$ What is the output potential difference across R2?"
45   solution = f"${vout} V$"
46   return problem, solution

Potential Divider question using Vout = (Vin * R2) / (R2 + R1)

Ex. Problem Ex. Solution
In a Potential Divider, if resistors R1 and R2 have resistances of $100 \Omega$ and $50 \Omega$ respectively, and the cell has $12 V$ What is the output potential difference across R2? $4 V$
def resistivity(max_diameter_mm=5, max_length_cm=100, max_resistance=0.1):
48def resistivity(max_diameter_mm=5, max_length_cm=100, max_resistance=0.1):
49   r"""Calculate the Resistivity using the equation R = (pL)/A, where R = Resistance, L = length of wire, p = resistivity and A = cross sectional area of wire
50
51    | Ex. Problem | Ex. Solution |
52    | --- | --- |
53    | A wire has resistance $30 m\Omega$ when it is $83.64 cm$ long with a diameter of $4.67 mm$. Calculate the resistivity of the wire | $6.14e-07 \Omega m$ |
54    """
55   # This question requires a lot of unit conversions and calculating the area of a circle from diameter
56   diameter_mm = round(random.uniform(0, max_diameter_mm),2)   # Random diameter in mm
57   cross_sectional_area = math.pi * (diameter_mm / 2000)**2    # Calculate the cross sectional area using pi r²
58   length_cm = round(random.uniform(0, max_length_cm),2)       # Random wire length in cm
59   resistance = round(random.uniform(0, max_resistance),2)     # Random reistance in ohms
60
61   resistivity = (resistance * cross_sectional_area) / (length_cm / 100)
62
63   problem = f"A wire has resistance ${resistance*1000} m\\Omega$ when it is ${length_cm} cm$ long with a diameter of ${diameter_mm} mm$. Calculate the resistivity of the wire"
64   solution = f"${resistivity:.2e} \\Omega m$"
65
66   return problem, solution

Calculate the Resistivity using the equation R = (pL)/A, where R = Resistance, L = length of wire, p = resistivity and A = cross sectional area of wire

Ex. Problem Ex. Solution
A wire has resistance $30 m\Omega$ when it is $83.64 cm$ long with a diameter of $4.67 mm$. Calculate the resistivity of the wire $6.14e-07 \Omega m$
def electric_field_strength_two_points(max_seperation_cm=100, max_charge_uC=1000):
 68def electric_field_strength_two_points(max_seperation_cm=100, max_charge_uC=1000):
 69    r"""Calculate the total electric field strength at point P with given points A and B, using the equation kQ/r²
 70
 71    | Ex. Problem | Ex. Solution |
 72    | --- | --- |
 73    | Charges A and B and point P are arranged like this: B <-- 7 cm --> P <-- 79 cm --> A, Where A and B have charges of -56 µC and -410 µC, What is the electric field strength at point P? | $-751417824 NC^{-1}$ (to the right) |
 74
 75    """
 76    a_charge = random.randint(-max_charge_uC,max_charge_uC)
 77    b_charge = random.randint(-max_charge_uC,max_charge_uC)
 78    arrangement = [['P'],['A',a_charge],['B',b_charge]] # Arrangement of charge A, B and the point of focus
 79    random.shuffle(arrangement)
 80    seperations = [random.randint(0,max_seperation_cm), random.randint(0,max_seperation_cm)]
 81    total_efs = 0
 82    # Work out how far A and B are from P (vector)
 83    if arrangement[0][0] == 'P':
 84        arrangement[1].append(seperations[0])
 85        arrangement[2].append(seperations[0]+seperations[1])
 86    elif arrangement[1][0] == 'P':
 87        arrangement[0].append(-seperations[0])
 88        arrangement[2].append(seperations[1])
 89    else:
 90        arrangement[0].append(-(seperations[0]+seperations[1]))
 91        arrangement[1].append(-seperations[1])
 92
 93    # Work out the EFS at point P caused by A and B seperatley, then sum them together in `total_efs`
 94    for point in arrangement:
 95        if point[0] == 'P':
 96            continue
 97        else:
 98            efs = ((8.99*10**9)*(point[1]*10**-6))/((point[2]/100)**2) # efs = kQ/r²
 99            if point[2] > 0: efs = -efs
100            point.append(efs)
101            total_efs += efs
102
103    problem = f"Charges A and B and point P are arranged like this:\n{arrangement[0][0]} <-- ${seperations[0]}$ cm --> {arrangement[1][0]} <-- ${seperations[1]}$ cm --> {arrangement[2][0]}\nWhere A and B have charges of ${a_charge}$ µC and ${b_charge}$ µC\nWhat is the electric field strength at point P?"
104    solution = f"${round(total_efs)} NC^{-1}$ (to the right)"
105    return problem, solution

Calculate the total electric field strength at point P with given points A and B, using the equation kQ/r²

Ex. Problem Ex. Solution
Charges A and B and point P are arranged like this: B <-- 7 cm --> P <-- 79 cm --> A, Where A and B have charges of -56 µC and -410 µC, What is the electric field strength at point P? $-751417824 NC^{-1}$ (to the right)
def fringe_spacing(max_screen_distance=30, max_slit_spacing_mm=100):
109def fringe_spacing(max_screen_distance=30, max_slit_spacing_mm=100):
110   r"""Calculate the fringe spacing in a double slit experiment with w=(λD)/s
111    | Ex. Problem | Ex. Solution |
112    | --- | --- |
113    | A laser with a wavelength of $450nm$ is shone through a double slit system to produce an interference pattern on a screen.  The screen is $12m$ from the slits and the slits are $0.30mm$ apart. Calculate the spacing between the bright fringes. | Using the equation $\\frac{{\\lambda D}}{{s}}$, we get a fringe spacing of $0.018m$ |
114    """
115   wavelength_nm = random.randint(380,750)      # Random wavelength between violet and red (nm)
116   screen_distance = random.randint(0, max_screen_distance)    # Random distance between screen and slits (m)
117   slit_spacing_mm = random.randint(0, max_slit_spacing_mm)    # Random slit spacing (mm)
118
119   fringe_spacing = round((((wavelength_nm * 10**-9) * screen_distance) / (slit_spacing_mm * 10**-3)),5)
120
121   problem = f"A laser with a wavelength of ${wavelength_nm}nm$ is shone through a double slit system to produce an interference pattern on a screen.  The screen is ${screen_distance}m$ from the slits and the slits are ${slit_spacing_mm}mm$ apart. Calculate the spacing between the bright fringes."
122   solution = f"Using the equation $\\frac{{\\lambda D}}{{s}}$, we get a fringe spacing of ${fringe_spacing}m$"
123   return problem, solution

Calculate the fringe spacing in a double slit experiment with w=(λD)/s

Ex. Problem Ex. Solution
A laser with a wavelength of $450nm$ is shone through a double slit system to produce an interference pattern on a screen. The screen is $12m$ from the slits and the slits are $0.30mm$ apart. Calculate the spacing between the bright fringes. Using the equation $\frac{{\lambda D}}{{s}}$, we get a fringe spacing of $0.018m$
def diffraction_grating_wavelength(min_slits_per_mm=100, max_slits_per_mm=500, max_order_number=5):
125def diffraction_grating_wavelength(min_slits_per_mm=100, max_slits_per_mm=500, max_order_number=5):
126    r"""Calculate the wavelength when given the number of slits per mm, order number and angle of order using the equation nλ = dsinθ
127
128    | Ex. Problem | Ex. Solution |
129    | --- | --- |
130    | A laser is shone through a diffraction grating which has $293$ lines per mm, the fringe of order number $2$ is at an angle of $0.39$ rad. Calculate the wavelength of the light | $\lambda = 6.487856913364529e-07m = 649nm |
131
132    """
133    slits_per_mm = random.randint(min_slits_per_mm, max_slits_per_mm)
134    slit_spacing = 1/(slits_per_mm * 1000)
135    order_number = random.randint(1, max_order_number)
136    angle_of_order = round(random.uniform(0.2, (math.pi/2)-0.2),2)
137    wavelength = ((slit_spacing * math.sin(angle_of_order)) / order_number)
138
139    problem = f"A laser is shone through a diffraction grating which has ${slits_per_mm}$ lines per mm, the fringe of order number ${order_number}$ is at an angle of ${angle_of_order}$ rad. Calculate the wavelength of the light"
140    solution = f"$\\lambda = {wavelength}m = {round(wavelength / 10**-9)}nm$"
141
142    return problem, solution

Calculate the wavelength when given the number of slits per mm, order number and angle of order using the equation nλ = dsinθ

Ex. Problem Ex. Solution
A laser is shone through a diffraction grating which has $293$ lines per mm, the fringe of order number $2$ is at an angle of $0.39$ rad. Calculate the wavelength of the light $\lambda = 6.487856913364529e-07m = 649nm