Skip to content

Commit 9200c64

Browse files
authored
Added Wheatstone Bridge Algorithm (#9872)
* Add files via upload * Update wheatstone_bridge.py * Update wheatstone_bridge.py
1 parent cd684fd commit 9200c64

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Diff for: electronics/wheatstone_bridge.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# https://en.wikipedia.org/wiki/Wheatstone_bridge
2+
from __future__ import annotations
3+
4+
5+
def wheatstone_solver(
6+
resistance_1: float, resistance_2: float, resistance_3: float
7+
) -> float:
8+
"""
9+
This function can calculate the unknown resistance in an wheatstone network,
10+
given that the three other resistances in the network are known.
11+
The formula to calculate the same is:
12+
13+
---------------
14+
|Rx=(R2/R1)*R3|
15+
---------------
16+
17+
Usage examples:
18+
>>> wheatstone_solver(resistance_1=2, resistance_2=4, resistance_3=5)
19+
10.0
20+
>>> wheatstone_solver(resistance_1=356, resistance_2=234, resistance_3=976)
21+
641.5280898876405
22+
>>> wheatstone_solver(resistance_1=2, resistance_2=-1, resistance_3=2)
23+
Traceback (most recent call last):
24+
...
25+
ValueError: All resistance values must be positive
26+
>>> wheatstone_solver(resistance_1=0, resistance_2=0, resistance_3=2)
27+
Traceback (most recent call last):
28+
...
29+
ValueError: All resistance values must be positive
30+
"""
31+
32+
if resistance_1 <= 0 or resistance_2 <= 0 or resistance_3 <= 0:
33+
raise ValueError("All resistance values must be positive")
34+
else:
35+
return float((resistance_2 / resistance_1) * resistance_3)
36+
37+
38+
if __name__ == "__main__":
39+
import doctest
40+
41+
doctest.testmod()

0 commit comments

Comments
 (0)