Skip to content

Commit f509927

Browse files
authored
Final edits
1 parent 32562f9 commit f509927

File tree

1 file changed

+17
-55
lines changed

1 file changed

+17
-55
lines changed

Diff for: electronics/charging_capacitor.py

+17-55
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# source - The ARRL Handbook for Radio Communications
2-
3-
from math import exp # value of exp = 2.718281828459…
2+
# https://en.wikipedia.org/wiki/RC_time_constant
43

54
"""
65
Description
@@ -10,13 +9,12 @@
109
a capacitor then the capacitor charges slowly means it will take more time than usual.
1110
while the capacitor is being charged, the voltage is in exponential function with time.
1211
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.
12+
'resistance(ohms) * capacitance(farads)' is called RC-timeconstant which may also be
13+
represented as τ (tau). By using this RC-timeconstant we can find the voltage at any
14+
time 't' from the initiation of charging a capacitor with the help of the exponential
15+
function containing RC. Both at charging and discharging of a capacitor.
1916
"""
17+
from math import exp # value of exp = 2.718281828459…
2018

2119

2220
def charging_capacitor(
@@ -39,68 +37,32 @@ def charging_capacitor(
3937
>>> charging_capacitor(source_voltage=15,resistance=200,capacitance=20,time_sec=2)
4038
0.007
4139
42-
>>> charging_capacitor(source_voltage=0,resistance=10.0,capacitance=.30,time_sec=3)
43-
Traceback (most recent call last):
44-
...
45-
ValueError: source voltage cannot be zero.
46-
4740
>>> charging_capacitor(20, 2000, 30*pow(10,-5), 4)
4841
19.975
4942
50-
>>> charging_capacitor(source_voltage=20,resistance=-2000,capacitance=30,time_sec=4)
51-
Traceback (most recent call last):
52-
...
53-
ValueError: Resistance cannot be negative.
54-
55-
>>> charging_capacitor(source_voltage=-2,resistance=20,capacitance=30,time_sec=4)
43+
>>> charging_capacitor(source_voltage=0,resistance=10.0,capacitance=.30,time_sec=3)
5644
Traceback (most recent call last):
5745
...
58-
ValueError: source voltage cannot be negative.
46+
ValueError: Source voltage must be positive.
5947
60-
>>> charging_capacitor(source_voltage=8,resistance=0,capacitance=30,time_sec=4)
48+
>>> charging_capacitor(source_voltage=20,resistance=-2000,capacitance=30,time_sec=4)
6149
Traceback (most recent call last):
6250
...
63-
ValueError: Resistance cannot be zero.
51+
ValueError: Resistance must be positive.
6452
6553
>>> charging_capacitor(source_voltage=30,resistance=1500,capacitance=0,time_sec=4)
6654
Traceback (most recent call last):
6755
...
68-
ValueError: Capacitance cannot be zero.
69-
70-
>>> charging_capacitor(source_voltage=30,resistance=23,capacitance=-40,time_sec=5)
71-
Traceback (most recent call last):
72-
...
73-
ValueError: Capacitance cannot be negative.
56+
ValueError: Capacitance must be positive.
7457
"""
7558

7659
if source_voltage <= 0:
77-
if source_voltage < 0:
78-
raise ValueError("source voltage cannot be negative.")
79-
elif source_voltage == 0:
80-
raise ValueError("source voltage cannot be zero.")
81-
else:
82-
return 0
83-
84-
elif resistance <= 0:
85-
if resistance < 0:
86-
raise ValueError("Resistance cannot be negative.")
87-
elif resistance == 0:
88-
raise ValueError("Resistance cannot be zero.")
89-
else:
90-
return 0
91-
92-
elif capacitance <= 0:
93-
if capacitance < 0:
94-
raise ValueError("Capacitance cannot be negative.")
95-
elif capacitance == 0:
96-
raise ValueError("Capacitance cannot be zero.")
97-
else:
98-
return 0
99-
100-
else:
101-
return round(
102-
source_voltage * (1 - exp(-time_sec / (resistance * capacitance))), 3
103-
)
60+
raise ValueError("Source voltage must be positive.")
61+
if resistance <= 0:
62+
raise ValueError("Resistance must be positive.")
63+
if capacitance <= 0:
64+
raise ValueError("Capacitance must be positive.")
65+
return round(source_voltage * (1 - exp(-time_sec / (resistance * capacitance))), 3)
10466

10567

10668
if __name__ == "__main__":

0 commit comments

Comments
 (0)