mathgenerator.physics

 1import random
 2import math
 3
 4# Generic
 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
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
67# Waves
68def fringe_spacing(max_screen_distance=30, max_slit_spacing_mm=100):
69   r"""Calculate the fringe spacing in a double slit experiment with w=(λD)/s
70    | Ex. Problem | Ex. Solution |
71    | --- | --- |
72    | 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$ |
73    """
74   wavelength_nm = random.randint(380,750)      # Random wavelength between violet and red (nm)
75   screen_distance = random.randint(0, max_screen_distance)    # Random distance between screen and slits (m)
76   slit_spacing_mm = random.randint(0, max_slit_spacing_mm)    # Random slit spacing (mm)
77
78   fringe_spacing = round((((wavelength_nm * 10**-9) * screen_distance) / (slit_spacing_mm * 10**-3)),5)
79
80   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."
81   solution = f"Using the equation $\\frac{{\\lambda D}}{{s}}$, we get a fringe spacing of ${fringe_spacing}m$"
82   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 fringe_spacing(max_screen_distance=30, max_slit_spacing_mm=100):
69def fringe_spacing(max_screen_distance=30, max_slit_spacing_mm=100):
70   r"""Calculate the fringe spacing in a double slit experiment with w=(λD)/s
71    | Ex. Problem | Ex. Solution |
72    | --- | --- |
73    | 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$ |
74    """
75   wavelength_nm = random.randint(380,750)      # Random wavelength between violet and red (nm)
76   screen_distance = random.randint(0, max_screen_distance)    # Random distance between screen and slits (m)
77   slit_spacing_mm = random.randint(0, max_slit_spacing_mm)    # Random slit spacing (mm)
78
79   fringe_spacing = round((((wavelength_nm * 10**-9) * screen_distance) / (slit_spacing_mm * 10**-3)),5)
80
81   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."
82   solution = f"Using the equation $\\frac{{\\lambda D}}{{s}}$, we get a fringe spacing of ${fringe_spacing}m$"
83   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$