-
-
Notifications
You must be signed in to change notification settings - Fork 47.1k
Resonant Frequency & Electrical Impedance #6983
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
Changes from all commits
cfcec2c
75d4311
83fbe1e
63e739d
bfae159
0ae3830
ed30f58
ab0d6eb
c571d56
da21dcf
d4a1a5b
aec3c38
e45e47c
0d72928
524cf41
bba6342
bf1538c
5122597
a737543
75bf82a
3429519
b3af034
7aeebc7
bb292dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tuple contains the values of inductance and capacitance that are required for calculating the resonant frequency of the LC Circuit. |
||
""" | ||
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() | ||
SKVKPandey marked this conversation as resolved.
Show resolved
Hide resolved
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as before, perhaps add a brief explanation of LC circuits.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added the description for LC Circuit.