1
1
# 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
4
3
5
4
"""
6
5
Description
10
9
a capacitor then the capacitor charges slowly means it will take more time than usual.
11
10
while the capacitor is being charged, the voltage is in exponential function with time.
12
11
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.
19
16
"""
17
+ from math import exp # value of exp = 2.718281828459…
20
18
21
19
22
20
def charging_capacitor (
@@ -39,68 +37,32 @@ def charging_capacitor(
39
37
>>> charging_capacitor(source_voltage=15,resistance=200,capacitance=20,time_sec=2)
40
38
0.007
41
39
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
-
47
40
>>> charging_capacitor(20, 2000, 30*pow(10,-5), 4)
48
41
19.975
49
42
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)
56
44
Traceback (most recent call last):
57
45
...
58
- ValueError: source voltage cannot be negative .
46
+ ValueError: Source voltage must be positive .
59
47
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)
61
49
Traceback (most recent call last):
62
50
...
63
- ValueError: Resistance cannot be zero .
51
+ ValueError: Resistance must be positive .
64
52
65
53
>>> charging_capacitor(source_voltage=30,resistance=1500,capacitance=0,time_sec=4)
66
54
Traceback (most recent call last):
67
55
...
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.
74
57
"""
75
58
76
59
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 )
104
66
105
67
106
68
if __name__ == "__main__" :
0 commit comments