MCPcopy
hub / github.com/TheAlgorithms/Python / couloumbs_law

Function couloumbs_law

electronics/coulombs_law.py:8–79  ·  view source on GitHub ↗

Apply Coulomb's Law on any three given values. These can be force, charge1, charge2, or distance, and then in a Python dict return name/value pair of the zero value. Coulomb's Law states that the magnitude of the electrostatic force of attraction or repulsion between two point

(
    force: float, charge1: float, charge2: float, distance: float
)

Source from the content-addressed store, hash-verified

6
7
8def couloumbs_law(
9 force: float, charge1: float, charge2: float, distance: float
10) -> dict[str, float]:
11 """
12 Apply Coulomb's Law on any three given values. These can be force, charge1,
13 charge2, or distance, and then in a Python dict return name/value pair of
14 the zero value.
15
16 Coulomb's Law states that the magnitude of the electrostatic force of
17 attraction or repulsion between two point charges is directly proportional
18 to the product of the magnitudes of charges and inversely proportional to
19 the square of the distance between them.
20
21 Reference
22 ----------
23 Coulomb (1785) "Premier mémoire sur l'électricité et le magnétisme,"
24 Histoire de l'Académie Royale des Sciences, pp. 569-577.
25
26 Parameters
27 ----------
28 force : float with units in Newtons
29
30 charge1 : float with units in Coulombs
31
32 charge2 : float with units in Coulombs
33
34 distance : float with units in meters
35
36 Returns
37 -------
38 result : dict name/value pair of the zero value
39
40 >>> couloumbs_law(force=0, charge1=3, charge2=5, distance=2000)
41 {'force': 33705.0}
42
43 >>> couloumbs_law(force=10, charge1=3, charge2=5, distance=0)
44 {'distance': 116112.01488218177}
45
46 >>> couloumbs_law(force=10, charge1=0, charge2=5, distance=2000)
47 {'charge1': 0.0008900756564307966}
48
49 >>> couloumbs_law(force=0, charge1=0, charge2=5, distance=2000)
50 Traceback (most recent call last):
51 ...
52 ValueError: One and only one argument must be 0
53
54 >>> couloumbs_law(force=0, charge1=3, charge2=5, distance=-2000)
55 Traceback (most recent call last):
56 ...
57 ValueError: Distance cannot be negative
58
59 """
60
61 charge_product = abs(charge1 * charge2)
62
63 if (force, charge1, charge2, distance).count(0) != 1:
64 raise ValueError("One and only one argument must be 0")
65 if distance < 0:

Callers

nothing calls this directly

Calls 1

countMethod · 0.80

Tested by

no test coverage detected