Skip to content

Commit 32562f9

Browse files
committed
charging_capacitor_algo
2 parents f07eefd + fa89e7b commit 32562f9

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

Diff for: electronics/charging_capacitor.py

+20-17
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66
Description
77
-----------
8-
when a capacitor is connected with a potential source(AC or DC). It is starts to charge
8+
When a capacitor is connected with a potential source (AC or DC). It starts to charge
99
at a general speed but when a resistor is connected in the circuit with in series to
1010
a capacitor then the capacitor charges slowly means it will take more time than usual.
1111
while the capacitor is being charged, the voltage is in exponential function with time.
@@ -26,30 +26,25 @@ def charging_capacitor(
2626
time_sec: float, # time in seconds after charging initiation of capacitor.
2727
) -> float:
2828
"""
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.
29+
Find capacitor voltage at any nth second after initiating its charging.
4130
4231
Examples
4332
--------
33+
>>> charging_capacitor(source_voltage=.2,resistance=.9,capacitance=8.4,time_sec=.5)
34+
0.013
35+
36+
>>> charging_capacitor(source_voltage=2.2,resistance=3.5,capacitance=2.4,time_sec=9)
37+
1.446
38+
4439
>>> charging_capacitor(source_voltage=15,resistance=200,capacitance=20,time_sec=2)
4540
0.007
4641
47-
>>> charging_capacitor(source_voltage=0,resistance=1000,capacitance=30,time_sec=3)
42+
>>> charging_capacitor(source_voltage=0,resistance=10.0,capacitance=.30,time_sec=3)
4843
Traceback (most recent call last):
4944
...
5045
ValueError: source voltage cannot be zero.
5146
52-
>>> charging_capacitor(20,2000,30*pow(10,-5),4)
47+
>>> charging_capacitor(20, 2000, 30*pow(10,-5), 4)
5348
19.975
5449
5550
>>> charging_capacitor(source_voltage=20,resistance=-2000,capacitance=30,time_sec=4)
@@ -83,20 +78,28 @@ def charging_capacitor(
8378
raise ValueError("source voltage cannot be negative.")
8479
elif source_voltage == 0:
8580
raise ValueError("source voltage cannot be zero.")
81+
else:
82+
return 0
83+
8684
elif resistance <= 0:
8785
if resistance < 0:
8886
raise ValueError("Resistance cannot be negative.")
8987
elif resistance == 0:
9088
raise ValueError("Resistance cannot be zero.")
89+
else:
90+
return 0
91+
9192
elif capacitance <= 0:
9293
if capacitance < 0:
9394
raise ValueError("Capacitance cannot be negative.")
9495
elif capacitance == 0:
9596
raise ValueError("Capacitance cannot be zero.")
97+
else:
98+
return 0
99+
96100
else:
97101
return round(
98-
source_voltage * (1 - exp(-time_sec / (resistance * capacitance))),
99-
3,
102+
source_voltage * (1 - exp(-time_sec / (resistance * capacitance))), 3
100103
)
101104

102105

0 commit comments

Comments
 (0)