|
| 1 | +# https://en.wikipedia.org/wiki/Coulomb%27s_law |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +COULOMBS_CONSTANT = 8.988e9 # units = N * m^s * C^-2 |
| 6 | + |
| 7 | + |
| 8 | +def couloumbs_law( |
| 9 | + force: float, charge1: float, charge2: float, distance: float |
| 10 | +) -> dict[str, float]: |
| 11 | + |
| 12 | + """ |
| 13 | + Apply Coulomb's Law on any three given values. These can be force, charge1, |
| 14 | + charge2, or distance, and then in a Python dict return name/value pair of |
| 15 | + the zero value. |
| 16 | +
|
| 17 | + Coulomb's Law states that the magnitude of the electrostatic force of |
| 18 | + attraction or repulsion between two point charges is directly proportional |
| 19 | + to the product of the magnitudes of charges and inversely proportional to |
| 20 | + the square of the distance between them. |
| 21 | +
|
| 22 | + Reference |
| 23 | + ---------- |
| 24 | + Coulomb (1785) "Premier mémoire sur l’électricité et le magnétisme," |
| 25 | + Histoire de l’Académie Royale des Sciences, pp. 569–577. |
| 26 | +
|
| 27 | + Parameters |
| 28 | + ---------- |
| 29 | + force : float with units in Newtons |
| 30 | +
|
| 31 | + charge1 : float with units in Coulombs |
| 32 | +
|
| 33 | + charge2 : float with units in Coulombs |
| 34 | +
|
| 35 | + distance : float with units in meters |
| 36 | +
|
| 37 | + Returns |
| 38 | + ------- |
| 39 | + result : dict name/value pair of the zero value |
| 40 | +
|
| 41 | + >>> couloumbs_law(force=0, charge1=3, charge2=5, distance=2000) |
| 42 | + {'force': 33705.0} |
| 43 | +
|
| 44 | + >>> couloumbs_law(force=10, charge1=3, charge2=5, distance=0) |
| 45 | + {'distance': 116112.01488218177} |
| 46 | +
|
| 47 | + >>> couloumbs_law(force=10, charge1=0, charge2=5, distance=2000) |
| 48 | + {'charge1': 0.0008900756564307966} |
| 49 | +
|
| 50 | + >>> couloumbs_law(force=0, charge1=0, charge2=5, distance=2000) |
| 51 | + Traceback (most recent call last): |
| 52 | + ... |
| 53 | + ValueError: One and only one argument must be 0 |
| 54 | +
|
| 55 | + >>> couloumbs_law(force=0, charge1=3, charge2=5, distance=-2000) |
| 56 | + Traceback (most recent call last): |
| 57 | + ... |
| 58 | + ValueError: Distance cannot be negative |
| 59 | +
|
| 60 | + """ |
| 61 | + |
| 62 | + charge_product = abs(charge1 * charge2) |
| 63 | + |
| 64 | + if (force, charge1, charge2, distance).count(0) != 1: |
| 65 | + raise ValueError("One and only one argument must be 0") |
| 66 | + if distance < 0: |
| 67 | + raise ValueError("Distance cannot be negative") |
| 68 | + if force == 0: |
| 69 | + force = COULOMBS_CONSTANT * charge_product / (distance ** 2) |
| 70 | + return {"force": force} |
| 71 | + elif charge1 == 0: |
| 72 | + charge1 = abs(force) * (distance ** 2) / (COULOMBS_CONSTANT * charge2) |
| 73 | + return {"charge1": charge1} |
| 74 | + elif charge2 == 0: |
| 75 | + charge2 = abs(force) * (distance ** 2) / (COULOMBS_CONSTANT * charge1) |
| 76 | + return {"charge2": charge2} |
| 77 | + elif distance == 0: |
| 78 | + distance = (COULOMBS_CONSTANT * charge_product / abs(force)) ** 0.5 |
| 79 | + return {"distance": distance} |
| 80 | + raise ValueError("Exactly one argument must be 0") |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + import doctest |
| 85 | + |
| 86 | + doctest.testmod() |
0 commit comments