-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
/
Copy pathelectric_potential_point_charge.py
47 lines (37 loc) · 1.4 KB
/
electric_potential_point_charge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from __future__ import annotations
COULOMB_CONSTANT = 8.99e9 # N·m²/C², Coulomb's constant
"""reference: https://en.wikipedia.org/wiki/Electric_potential"""
def electric_potential_point_charge(charge: float, distance: float) -> float:
"""
Calculate the electric potential at a point due to a point charge.
Parameters:
- charge (float): The charge in Coulombs.
- distance (float): The distance from the charge in meters.
Returns:
- float: The electric potential at the point.
Raises:
- ValueError: If both charge and distance are zero, or if distance is negative.
Examples:
>>> electric_potential_point_charge(1e-6, 0.05)
179800.0
>>> electric_potential_point_charge(0, 0.05)
0
>>> electric_potential_point_charge(1e-6, 0)
inf
>>> electric_potential_point_charge(1e-6, -0.05)
Traceback (most recent call last):
...
ValueError: Distance cannot be negative.
"""
if distance < 0:
raise ValueError("Distance cannot be negative.")
elif distance == 0 and charge == 0:
raise ValueError("Charge and distance cannot both be zero.")
elif distance == 0:
return float("inf") # Potential is infinity when distance is zero
elif charge == 0:
return 0 # Zero potential for zero charge
return (COULOMB_CONSTANT * charge) / distance
if __name__ == "__main__":
import doctest
doctest.testmod()