mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
Resonant Frequency & Electrical Impedance (#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>
This commit is contained in:
parent
d84452344a
commit
bd50a30682
46
electronics/electrical_impedance.py
Normal file
46
electronics/electrical_impedance.py
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
"""Electrical impedance is the measure of the opposition that a
|
||||||
|
circuit presents to a current when a voltage is applied.
|
||||||
|
Impedance extends the concept of resistance to alternating current (AC) circuits.
|
||||||
|
Source: https://en.wikipedia.org/wiki/Electrical_impedance
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from math import pow, sqrt
|
||||||
|
|
||||||
|
|
||||||
|
def electrical_impedance(
|
||||||
|
resistance: float, reactance: float, impedance: float
|
||||||
|
) -> dict[str, float]:
|
||||||
|
"""
|
||||||
|
Apply Electrical Impedance formula, on any two given electrical values,
|
||||||
|
which can be resistance, reactance, and impedance, and then in a Python dict
|
||||||
|
return name/value pair of the zero value.
|
||||||
|
|
||||||
|
>>> electrical_impedance(3,4,0)
|
||||||
|
{'impedance': 5.0}
|
||||||
|
>>> electrical_impedance(0,4,5)
|
||||||
|
{'resistance': 3.0}
|
||||||
|
>>> electrical_impedance(3,0,5)
|
||||||
|
{'reactance': 4.0}
|
||||||
|
>>> electrical_impedance(3,4,5)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: One and only one argument must be 0
|
||||||
|
"""
|
||||||
|
if (resistance, reactance, impedance).count(0) != 1:
|
||||||
|
raise ValueError("One and only one argument must be 0")
|
||||||
|
if resistance == 0:
|
||||||
|
return {"resistance": sqrt(pow(impedance, 2) - pow(reactance, 2))}
|
||||||
|
elif reactance == 0:
|
||||||
|
return {"reactance": sqrt(pow(impedance, 2) - pow(resistance, 2))}
|
||||||
|
elif impedance == 0:
|
||||||
|
return {"impedance": sqrt(pow(resistance, 2) + pow(reactance, 2))}
|
||||||
|
else:
|
||||||
|
raise ValueError("Exactly one argument must be 0")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
50
electronics/resonant_frequency.py
Normal file
50
electronics/resonant_frequency.py
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
# https://en.wikipedia.org/wiki/LC_circuit
|
||||||
|
|
||||||
|
"""An LC circuit, also called a resonant circuit, tank circuit, or tuned circuit,
|
||||||
|
is an electric circuit consisting of an inductor, represented by the letter L,
|
||||||
|
and a capacitor, represented by the letter C, connected together.
|
||||||
|
The circuit can act as an electrical resonator, an electrical analogue of a
|
||||||
|
tuning fork, storing energy oscillating at the circuit's resonant frequency.
|
||||||
|
Source: https://en.wikipedia.org/wiki/LC_circuit
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from math import pi, sqrt
|
||||||
|
|
||||||
|
|
||||||
|
def resonant_frequency(inductance: float, capacitance: float) -> tuple:
|
||||||
|
"""
|
||||||
|
This function can calculate the resonant frequency of LC circuit,
|
||||||
|
for the given value of inductance and capacitnace.
|
||||||
|
|
||||||
|
Examples are given below:
|
||||||
|
>>> resonant_frequency(inductance=10, capacitance=5)
|
||||||
|
('Resonant frequency', 0.022507907903927652)
|
||||||
|
>>> resonant_frequency(inductance=0, capacitance=5)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Inductance cannot be 0 or negative
|
||||||
|
>>> resonant_frequency(inductance=10, capacitance=0)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Capacitance cannot be 0 or negative
|
||||||
|
"""
|
||||||
|
|
||||||
|
if inductance <= 0:
|
||||||
|
raise ValueError("Inductance cannot be 0 or negative")
|
||||||
|
|
||||||
|
elif capacitance <= 0:
|
||||||
|
raise ValueError("Capacitance cannot be 0 or negative")
|
||||||
|
|
||||||
|
else:
|
||||||
|
return (
|
||||||
|
"Resonant frequency",
|
||||||
|
float(1 / (2 * pi * (sqrt(inductance * capacitance)))),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
Loading…
Reference in New Issue
Block a user