Skip to content

Commit bd50a30

Browse files
SKVKPandeypre-commit-ci[bot]CaedenPHZeroDayOwl
authored
Resonant Frequency & Electrical Impedance (TheAlgorithms#6983)
* Resonant Frequency * Resonant Frequency of LC Circuit * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update electronics/resonant_frequency.py Co-authored-by: Caeden <caedenperelliharris@gmail.com> * Update electronics/resonant_frequency.py Co-authored-by: Caeden <caedenperelliharris@gmail.com> * Update electronics/resonant_frequency.py Co-authored-by: Caeden <caedenperelliharris@gmail.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Updated resonant_frequency.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update electronics/resonant_frequency.py Co-authored-by: Paul <56065602+ZeroDayOwl@users.noreply.github.com> * Fixed doctest issues in resonant_frequency.py * Algorithm for Electrical Impedance * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Updated Algorithm for Electrical Impedance * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update resonant_frequency.py * Update electrical_impedance.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update resonant_frequency.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update electronics/electrical_impedance.py Co-authored-by: Paul <56065602+ZeroDayOwl@users.noreply.github.com> * Update electronics/electrical_impedance.py Co-authored-by: Paul <56065602+ZeroDayOwl@users.noreply.github.com> * Update electronics/resonant_frequency.py Co-authored-by: Paul <56065602+ZeroDayOwl@users.noreply.github.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Caeden <caedenperelliharris@gmail.com> Co-authored-by: Paul <56065602+ZeroDayOwl@users.noreply.github.com>
1 parent d844523 commit bd50a30

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

electronics/electrical_impedance.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""Electrical impedance is the measure of the opposition that a
2+
circuit presents to a current when a voltage is applied.
3+
Impedance extends the concept of resistance to alternating current (AC) circuits.
4+
Source: https://en.wikipedia.org/wiki/Electrical_impedance
5+
"""
6+
7+
from __future__ import annotations
8+
9+
from math import pow, sqrt
10+
11+
12+
def electrical_impedance(
13+
resistance: float, reactance: float, impedance: float
14+
) -> dict[str, float]:
15+
"""
16+
Apply Electrical Impedance formula, on any two given electrical values,
17+
which can be resistance, reactance, and impedance, and then in a Python dict
18+
return name/value pair of the zero value.
19+
20+
>>> electrical_impedance(3,4,0)
21+
{'impedance': 5.0}
22+
>>> electrical_impedance(0,4,5)
23+
{'resistance': 3.0}
24+
>>> electrical_impedance(3,0,5)
25+
{'reactance': 4.0}
26+
>>> electrical_impedance(3,4,5)
27+
Traceback (most recent call last):
28+
...
29+
ValueError: One and only one argument must be 0
30+
"""
31+
if (resistance, reactance, impedance).count(0) != 1:
32+
raise ValueError("One and only one argument must be 0")
33+
if resistance == 0:
34+
return {"resistance": sqrt(pow(impedance, 2) - pow(reactance, 2))}
35+
elif reactance == 0:
36+
return {"reactance": sqrt(pow(impedance, 2) - pow(resistance, 2))}
37+
elif impedance == 0:
38+
return {"impedance": sqrt(pow(resistance, 2) + pow(reactance, 2))}
39+
else:
40+
raise ValueError("Exactly one argument must be 0")
41+
42+
43+
if __name__ == "__main__":
44+
import doctest
45+
46+
doctest.testmod()

electronics/resonant_frequency.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# https://en.wikipedia.org/wiki/LC_circuit
2+
3+
"""An LC circuit, also called a resonant circuit, tank circuit, or tuned circuit,
4+
is an electric circuit consisting of an inductor, represented by the letter L,
5+
and a capacitor, represented by the letter C, connected together.
6+
The circuit can act as an electrical resonator, an electrical analogue of a
7+
tuning fork, storing energy oscillating at the circuit's resonant frequency.
8+
Source: https://en.wikipedia.org/wiki/LC_circuit
9+
"""
10+
11+
from __future__ import annotations
12+
13+
from math import pi, sqrt
14+
15+
16+
def resonant_frequency(inductance: float, capacitance: float) -> tuple:
17+
"""
18+
This function can calculate the resonant frequency of LC circuit,
19+
for the given value of inductance and capacitnace.
20+
21+
Examples are given below:
22+
>>> resonant_frequency(inductance=10, capacitance=5)
23+
('Resonant frequency', 0.022507907903927652)
24+
>>> resonant_frequency(inductance=0, capacitance=5)
25+
Traceback (most recent call last):
26+
...
27+
ValueError: Inductance cannot be 0 or negative
28+
>>> resonant_frequency(inductance=10, capacitance=0)
29+
Traceback (most recent call last):
30+
...
31+
ValueError: Capacitance cannot be 0 or negative
32+
"""
33+
34+
if inductance <= 0:
35+
raise ValueError("Inductance cannot be 0 or negative")
36+
37+
elif capacitance <= 0:
38+
raise ValueError("Capacitance cannot be 0 or negative")
39+
40+
else:
41+
return (
42+
"Resonant frequency",
43+
float(1 / (2 * pi * (sqrt(inductance * capacitance)))),
44+
)
45+
46+
47+
if __name__ == "__main__":
48+
import doctest
49+
50+
doctest.testmod()

0 commit comments

Comments
 (0)