From f07eefde6497edadf369da87a8e2de5836deef55 Mon Sep 17 00:00:00 2001 From: dhruv Date: Wed, 4 Oct 2023 02:13:15 +0530 Subject: [PATCH 1/4] charging_capacitor --- electronics/charging_capacitor.py | 106 ++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 electronics/charging_capacitor.py diff --git a/electronics/charging_capacitor.py b/electronics/charging_capacitor.py new file mode 100644 index 000000000000..93a8dc755088 --- /dev/null +++ b/electronics/charging_capacitor.py @@ -0,0 +1,106 @@ +# source - The ARRL Handbook for Radio Communications + +from math import exp # value of exp = 2.718281828459… + +""" +Description +----------- +when a capacitor is connected with a potential source(AC or DC). It is starts to charge +at a general speed but when a resistor is connected in the circuit with in series to +a capacitor then the capacitor charges slowly means it will take more time than usual. +while the capacitor is being charged, the voltage is in exponential function with time. + +in the this function there is RC which is 'resistance(ohms)*capacitance(farads)'. +also represented as τ (tau). + +with the help of RC-timeconstant we can find the voltage at any time 't' from the +initiation of charging a capacitor with the help of the exponential function +containing RC.Both at charging and discharging of a capacitor. +""" + + +def charging_capacitor( + source_voltage: float, # voltage in volts. + resistance: float, # resistance in ohms. + capacitance: float, # capacitance in farads. + time_sec: float, # time in seconds after charging initiation of capacitor. +) -> float: + """ + find voltage of capacitor at any nth second after the initiation of it's charging. + + Parameters + ---------- + source_voltage : float + it is going to multiply with rest of the function. + resistance : float + it is using in RC function. + capacitance : float + it is multiplying with resistance_ohms to yield output. + time_sec : float + it is dividing by RC.To find the voltage at nth second. + + Examples + -------- + >>> charging_capacitor(source_voltage=15,resistance=200,capacitance=20,time_sec=2) + 0.007 + + >>> charging_capacitor(source_voltage=0,resistance=1000,capacitance=30,time_sec=3) + Traceback (most recent call last): + ... + ValueError: source voltage cannot be zero. + + >>> charging_capacitor(20,2000,30*pow(10,-5),4) + 19.975 + + >>> charging_capacitor(source_voltage=20,resistance=-2000,capacitance=30,time_sec=4) + Traceback (most recent call last): + ... + ValueError: Resistance cannot be negative. + + >>> charging_capacitor(source_voltage=-2,resistance=20,capacitance=30,time_sec=4) + Traceback (most recent call last): + ... + ValueError: source voltage cannot be negative. + + >>> charging_capacitor(source_voltage=8,resistance=0,capacitance=30,time_sec=4) + Traceback (most recent call last): + ... + ValueError: Resistance cannot be zero. + + >>> charging_capacitor(source_voltage=30,resistance=1500,capacitance=0,time_sec=4) + Traceback (most recent call last): + ... + ValueError: Capacitance cannot be zero. + + >>> charging_capacitor(source_voltage=30,resistance=23,capacitance=-40,time_sec=5) + Traceback (most recent call last): + ... + ValueError: Capacitance cannot be negative. + """ + + if source_voltage <= 0: + if source_voltage < 0: + raise ValueError("source voltage cannot be negative.") + elif source_voltage == 0: + raise ValueError("source voltage cannot be zero.") + elif resistance <= 0: + if resistance < 0: + raise ValueError("Resistance cannot be negative.") + elif resistance == 0: + raise ValueError("Resistance cannot be zero.") + elif capacitance <= 0: + if capacitance < 0: + raise ValueError("Capacitance cannot be negative.") + elif capacitance == 0: + raise ValueError("Capacitance cannot be zero.") + else: + return round( + source_voltage * (1 - exp(-time_sec / (resistance * capacitance))), + 3, + ) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From fa89e7b3305a557817da8c7d7bcc2b55f8cd4ac8 Mon Sep 17 00:00:00 2001 From: dhruv Date: Wed, 4 Oct 2023 02:13:15 +0530 Subject: [PATCH 2/4] charging_capacitor --- electronics/charging_capacitor.py | 106 ++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 electronics/charging_capacitor.py diff --git a/electronics/charging_capacitor.py b/electronics/charging_capacitor.py new file mode 100644 index 000000000000..93a8dc755088 --- /dev/null +++ b/electronics/charging_capacitor.py @@ -0,0 +1,106 @@ +# source - The ARRL Handbook for Radio Communications + +from math import exp # value of exp = 2.718281828459… + +""" +Description +----------- +when a capacitor is connected with a potential source(AC or DC). It is starts to charge +at a general speed but when a resistor is connected in the circuit with in series to +a capacitor then the capacitor charges slowly means it will take more time than usual. +while the capacitor is being charged, the voltage is in exponential function with time. + +in the this function there is RC which is 'resistance(ohms)*capacitance(farads)'. +also represented as τ (tau). + +with the help of RC-timeconstant we can find the voltage at any time 't' from the +initiation of charging a capacitor with the help of the exponential function +containing RC.Both at charging and discharging of a capacitor. +""" + + +def charging_capacitor( + source_voltage: float, # voltage in volts. + resistance: float, # resistance in ohms. + capacitance: float, # capacitance in farads. + time_sec: float, # time in seconds after charging initiation of capacitor. +) -> float: + """ + find voltage of capacitor at any nth second after the initiation of it's charging. + + Parameters + ---------- + source_voltage : float + it is going to multiply with rest of the function. + resistance : float + it is using in RC function. + capacitance : float + it is multiplying with resistance_ohms to yield output. + time_sec : float + it is dividing by RC.To find the voltage at nth second. + + Examples + -------- + >>> charging_capacitor(source_voltage=15,resistance=200,capacitance=20,time_sec=2) + 0.007 + + >>> charging_capacitor(source_voltage=0,resistance=1000,capacitance=30,time_sec=3) + Traceback (most recent call last): + ... + ValueError: source voltage cannot be zero. + + >>> charging_capacitor(20,2000,30*pow(10,-5),4) + 19.975 + + >>> charging_capacitor(source_voltage=20,resistance=-2000,capacitance=30,time_sec=4) + Traceback (most recent call last): + ... + ValueError: Resistance cannot be negative. + + >>> charging_capacitor(source_voltage=-2,resistance=20,capacitance=30,time_sec=4) + Traceback (most recent call last): + ... + ValueError: source voltage cannot be negative. + + >>> charging_capacitor(source_voltage=8,resistance=0,capacitance=30,time_sec=4) + Traceback (most recent call last): + ... + ValueError: Resistance cannot be zero. + + >>> charging_capacitor(source_voltage=30,resistance=1500,capacitance=0,time_sec=4) + Traceback (most recent call last): + ... + ValueError: Capacitance cannot be zero. + + >>> charging_capacitor(source_voltage=30,resistance=23,capacitance=-40,time_sec=5) + Traceback (most recent call last): + ... + ValueError: Capacitance cannot be negative. + """ + + if source_voltage <= 0: + if source_voltage < 0: + raise ValueError("source voltage cannot be negative.") + elif source_voltage == 0: + raise ValueError("source voltage cannot be zero.") + elif resistance <= 0: + if resistance < 0: + raise ValueError("Resistance cannot be negative.") + elif resistance == 0: + raise ValueError("Resistance cannot be zero.") + elif capacitance <= 0: + if capacitance < 0: + raise ValueError("Capacitance cannot be negative.") + elif capacitance == 0: + raise ValueError("Capacitance cannot be zero.") + else: + return round( + source_voltage * (1 - exp(-time_sec / (resistance * capacitance))), + 3, + ) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From f509927563b9e0d6ef98ce8d3bd9a6ae2b1e8eef Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 7 Oct 2023 21:01:26 +0200 Subject: [PATCH 3/4] Final edits --- electronics/charging_capacitor.py | 72 ++++++++----------------------- 1 file changed, 17 insertions(+), 55 deletions(-) diff --git a/electronics/charging_capacitor.py b/electronics/charging_capacitor.py index 89eb048a6f11..4029b0ecf267 100644 --- a/electronics/charging_capacitor.py +++ b/electronics/charging_capacitor.py @@ -1,6 +1,5 @@ # source - The ARRL Handbook for Radio Communications - -from math import exp # value of exp = 2.718281828459… +# https://en.wikipedia.org/wiki/RC_time_constant """ Description @@ -10,13 +9,12 @@ a capacitor then the capacitor charges slowly means it will take more time than usual. while the capacitor is being charged, the voltage is in exponential function with time. -in the this function there is RC which is 'resistance(ohms)*capacitance(farads)'. -also represented as τ (tau). - -with the help of RC-timeconstant we can find the voltage at any time 't' from the -initiation of charging a capacitor with the help of the exponential function -containing RC.Both at charging and discharging of a capacitor. +'resistance(ohms) * capacitance(farads)' is called RC-timeconstant which may also be +represented as τ (tau). By using this RC-timeconstant we can find the voltage at any +time 't' from the initiation of charging a capacitor with the help of the exponential +function containing RC. Both at charging and discharging of a capacitor. """ +from math import exp # value of exp = 2.718281828459… def charging_capacitor( @@ -39,68 +37,32 @@ def charging_capacitor( >>> charging_capacitor(source_voltage=15,resistance=200,capacitance=20,time_sec=2) 0.007 - >>> charging_capacitor(source_voltage=0,resistance=10.0,capacitance=.30,time_sec=3) - Traceback (most recent call last): - ... - ValueError: source voltage cannot be zero. - >>> charging_capacitor(20, 2000, 30*pow(10,-5), 4) 19.975 - >>> charging_capacitor(source_voltage=20,resistance=-2000,capacitance=30,time_sec=4) - Traceback (most recent call last): - ... - ValueError: Resistance cannot be negative. - - >>> charging_capacitor(source_voltage=-2,resistance=20,capacitance=30,time_sec=4) + >>> charging_capacitor(source_voltage=0,resistance=10.0,capacitance=.30,time_sec=3) Traceback (most recent call last): ... - ValueError: source voltage cannot be negative. + ValueError: Source voltage must be positive. - >>> charging_capacitor(source_voltage=8,resistance=0,capacitance=30,time_sec=4) + >>> charging_capacitor(source_voltage=20,resistance=-2000,capacitance=30,time_sec=4) Traceback (most recent call last): ... - ValueError: Resistance cannot be zero. + ValueError: Resistance must be positive. >>> charging_capacitor(source_voltage=30,resistance=1500,capacitance=0,time_sec=4) Traceback (most recent call last): ... - ValueError: Capacitance cannot be zero. - - >>> charging_capacitor(source_voltage=30,resistance=23,capacitance=-40,time_sec=5) - Traceback (most recent call last): - ... - ValueError: Capacitance cannot be negative. + ValueError: Capacitance must be positive. """ if source_voltage <= 0: - if source_voltage < 0: - raise ValueError("source voltage cannot be negative.") - elif source_voltage == 0: - raise ValueError("source voltage cannot be zero.") - else: - return 0 - - elif resistance <= 0: - if resistance < 0: - raise ValueError("Resistance cannot be negative.") - elif resistance == 0: - raise ValueError("Resistance cannot be zero.") - else: - return 0 - - elif capacitance <= 0: - if capacitance < 0: - raise ValueError("Capacitance cannot be negative.") - elif capacitance == 0: - raise ValueError("Capacitance cannot be zero.") - else: - return 0 - - else: - return round( - source_voltage * (1 - exp(-time_sec / (resistance * capacitance))), 3 - ) + raise ValueError("Source voltage must be positive.") + if resistance <= 0: + raise ValueError("Resistance must be positive.") + if capacitance <= 0: + raise ValueError("Capacitance must be positive.") + return round(source_voltage * (1 - exp(-time_sec / (resistance * capacitance))), 3) if __name__ == "__main__": From 5fc64e619e1d2cf13150f57dba9f0cef45a5921c Mon Sep 17 00:00:00 2001 From: dhruv Date: Sat, 14 Oct 2023 15:30:12 +0530 Subject: [PATCH 4/4] charging_inductor --- electronics/charging_inductor.py | 96 ++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 electronics/charging_inductor.py diff --git a/electronics/charging_inductor.py b/electronics/charging_inductor.py new file mode 100644 index 000000000000..e5c0126c248a --- /dev/null +++ b/electronics/charging_inductor.py @@ -0,0 +1,96 @@ +# source - The ARRL Handbook for Radio Communications +# https://en.wikipedia.org/wiki/RL_circuit + +""" +Description +----------- +Inductor is a passive electronic device which stores energy but unlike capacitor, it +stores energy in its 'magnetic field' or 'magnetostatic field'. + +When inductor is connected to 'DC' current source nothing happens it just works like a +wire because it's real effect cannot be seen while 'DC' is connected, its not even +going to store energy. Inductor stores energy only when it is working on 'AC' current. + +Connecting a inductor in series with a resistor(when R = 0) to a 'AC' potential source, +from zero to a finite value causes a sudden voltage to induced in inductor which +opposes the current. which results in initially slowly current rise. However it would +cease if there is no further changes in current. With resistance zero current will never +stop rising. + +'Resistance(ohms) / Inductance(henrys)' is known as RL-timeconstant. It also represents +as τ (tau). While the charging of a inductor with a resistor results in +a exponential function. + +when inductor is connected across 'AC' potential source. It starts to store the energy +in its 'magnetic field'.with the help 'RL-time-constant' we can find current at any time +in inductor while it is charging. +""" +from math import exp # value of exp = 2.718281828459… + + +def charging_inductor( + source_voltage: float, # source_voltage should be in volts. + resistance: float, # resistance should be in ohms. + inductance: float, # inductance should be in henrys. + time: float, # time should in seconds. +) -> float: + """ + Find inductor current at any nth second after initiating its charging. + + Examples + -------- + >>> charging_inductor(source_voltage=5.8,resistance=1.5,inductance=2.3,time=2) + 2.817 + + >>> charging_inductor(source_voltage=8,resistance=5,inductance=3,time=2) + 1.543 + + >>> charging_inductor(source_voltage=8,resistance=5*pow(10,2),inductance=3,time=2) + 0.016 + + >>> charging_inductor(source_voltage=-8,resistance=100,inductance=15,time=12) + Traceback (most recent call last): + ... + ValueError: Source voltage must be positive. + + >>> charging_inductor(source_voltage=80,resistance=-15,inductance=100,time=5) + Traceback (most recent call last): + ... + ValueError: Resistance must be positive. + + >>> charging_inductor(source_voltage=12,resistance=200,inductance=-20,time=5) + Traceback (most recent call last): + ... + ValueError: Inductance must be positive. + + >>> charging_inductor(source_voltage=0,resistance=200,inductance=20,time=5) + Traceback (most recent call last): + ... + ValueError: Source voltage must be positive. + + >>> charging_inductor(source_voltage=10,resistance=0,inductance=20,time=5) + Traceback (most recent call last): + ... + ValueError: Resistance must be positive. + + >>> charging_inductor(source_voltage=15, resistance=25, inductance=0, time=5) + Traceback (most recent call last): + ... + ValueError: Inductance must be positive. + """ + + if source_voltage <= 0: + raise ValueError("Source voltage must be positive.") + if resistance <= 0: + raise ValueError("Resistance must be positive.") + if inductance <= 0: + raise ValueError("Inductance must be positive.") + return round( + source_voltage / resistance * (1 - exp((-time * resistance) / inductance)), 3 + ) + + +if __name__ == "__main__": + import doctest + + doctest.testmod()