Skip to content

Create electric-potential-point-charge.py for electronics branch #11922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
47 changes: 47 additions & 0 deletions electronics/electric_potential_point_charge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()