Skip to content

Commit fa89e7b

Browse files
committed
charging_capacitor
1 parent 3fd3497 commit fa89e7b

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

Diff for: electronics/charging_capacitor.py

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# source - The ARRL Handbook for Radio Communications
2+
3+
from math import exp # value of exp = 2.718281828459…
4+
5+
"""
6+
Description
7+
-----------
8+
when a capacitor is connected with a potential source(AC or DC). It is starts to charge
9+
at a general speed but when a resistor is connected in the circuit with in series to
10+
a capacitor then the capacitor charges slowly means it will take more time than usual.
11+
while the capacitor is being charged, the voltage is in exponential function with time.
12+
13+
in the this function there is RC which is 'resistance(ohms)*capacitance(farads)'.
14+
also represented as τ (tau).
15+
16+
with the help of RC-timeconstant we can find the voltage at any time 't' from the
17+
initiation of charging a capacitor with the help of the exponential function
18+
containing RC.Both at charging and discharging of a capacitor.
19+
"""
20+
21+
22+
def charging_capacitor(
23+
source_voltage: float, # voltage in volts.
24+
resistance: float, # resistance in ohms.
25+
capacitance: float, # capacitance in farads.
26+
time_sec: float, # time in seconds after charging initiation of capacitor.
27+
) -> float:
28+
"""
29+
find voltage of capacitor at any nth second after the initiation of it's charging.
30+
31+
Parameters
32+
----------
33+
source_voltage : float
34+
it is going to multiply with rest of the function.
35+
resistance : float
36+
it is using in RC function.
37+
capacitance : float
38+
it is multiplying with resistance_ohms to yield output.
39+
time_sec : float
40+
it is dividing by RC.To find the voltage at nth second.
41+
42+
Examples
43+
--------
44+
>>> charging_capacitor(source_voltage=15,resistance=200,capacitance=20,time_sec=2)
45+
0.007
46+
47+
>>> charging_capacitor(source_voltage=0,resistance=1000,capacitance=30,time_sec=3)
48+
Traceback (most recent call last):
49+
...
50+
ValueError: source voltage cannot be zero.
51+
52+
>>> charging_capacitor(20,2000,30*pow(10,-5),4)
53+
19.975
54+
55+
>>> charging_capacitor(source_voltage=20,resistance=-2000,capacitance=30,time_sec=4)
56+
Traceback (most recent call last):
57+
...
58+
ValueError: Resistance cannot be negative.
59+
60+
>>> charging_capacitor(source_voltage=-2,resistance=20,capacitance=30,time_sec=4)
61+
Traceback (most recent call last):
62+
...
63+
ValueError: source voltage cannot be negative.
64+
65+
>>> charging_capacitor(source_voltage=8,resistance=0,capacitance=30,time_sec=4)
66+
Traceback (most recent call last):
67+
...
68+
ValueError: Resistance cannot be zero.
69+
70+
>>> charging_capacitor(source_voltage=30,resistance=1500,capacitance=0,time_sec=4)
71+
Traceback (most recent call last):
72+
...
73+
ValueError: Capacitance cannot be zero.
74+
75+
>>> charging_capacitor(source_voltage=30,resistance=23,capacitance=-40,time_sec=5)
76+
Traceback (most recent call last):
77+
...
78+
ValueError: Capacitance cannot be negative.
79+
"""
80+
81+
if source_voltage <= 0:
82+
if source_voltage < 0:
83+
raise ValueError("source voltage cannot be negative.")
84+
elif source_voltage == 0:
85+
raise ValueError("source voltage cannot be zero.")
86+
elif resistance <= 0:
87+
if resistance < 0:
88+
raise ValueError("Resistance cannot be negative.")
89+
elif resistance == 0:
90+
raise ValueError("Resistance cannot be zero.")
91+
elif capacitance <= 0:
92+
if capacitance < 0:
93+
raise ValueError("Capacitance cannot be negative.")
94+
elif capacitance == 0:
95+
raise ValueError("Capacitance cannot be zero.")
96+
else:
97+
return round(
98+
source_voltage * (1 - exp(-time_sec / (resistance * capacitance))),
99+
3,
100+
)
101+
102+
103+
if __name__ == "__main__":
104+
import doctest
105+
106+
doctest.testmod()

0 commit comments

Comments
 (0)