|
| 1 | +# source - The ARRL Handbook for Radio Communications |
| 2 | +# https://en.wikipedia.org/wiki/RC_time_constant |
| 3 | + |
| 4 | +""" |
| 5 | +Description |
| 6 | +----------- |
| 7 | +When a capacitor is connected with a potential source (AC or DC). It starts to charge |
| 8 | +at a general speed but when a resistor is connected in the circuit with in series to |
| 9 | +a capacitor then the capacitor charges slowly means it will take more time than usual. |
| 10 | +while the capacitor is being charged, the voltage is in exponential function with time. |
| 11 | +
|
| 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. |
| 16 | +""" |
| 17 | +from math import exp # value of exp = 2.718281828459… |
| 18 | + |
| 19 | + |
| 20 | +def charging_capacitor( |
| 21 | + source_voltage: float, # voltage in volts. |
| 22 | + resistance: float, # resistance in ohms. |
| 23 | + capacitance: float, # capacitance in farads. |
| 24 | + time_sec: float, # time in seconds after charging initiation of capacitor. |
| 25 | +) -> float: |
| 26 | + """ |
| 27 | + Find capacitor voltage at any nth second after initiating its charging. |
| 28 | +
|
| 29 | + Examples |
| 30 | + -------- |
| 31 | + >>> charging_capacitor(source_voltage=.2,resistance=.9,capacitance=8.4,time_sec=.5) |
| 32 | + 0.013 |
| 33 | +
|
| 34 | + >>> charging_capacitor(source_voltage=2.2,resistance=3.5,capacitance=2.4,time_sec=9) |
| 35 | + 1.446 |
| 36 | +
|
| 37 | + >>> charging_capacitor(source_voltage=15,resistance=200,capacitance=20,time_sec=2) |
| 38 | + 0.007 |
| 39 | +
|
| 40 | + >>> charging_capacitor(20, 2000, 30*pow(10,-5), 4) |
| 41 | + 19.975 |
| 42 | +
|
| 43 | + >>> charging_capacitor(source_voltage=0,resistance=10.0,capacitance=.30,time_sec=3) |
| 44 | + Traceback (most recent call last): |
| 45 | + ... |
| 46 | + ValueError: Source voltage must be positive. |
| 47 | +
|
| 48 | + >>> charging_capacitor(source_voltage=20,resistance=-2000,capacitance=30,time_sec=4) |
| 49 | + Traceback (most recent call last): |
| 50 | + ... |
| 51 | + ValueError: Resistance must be positive. |
| 52 | +
|
| 53 | + >>> charging_capacitor(source_voltage=30,resistance=1500,capacitance=0,time_sec=4) |
| 54 | + Traceback (most recent call last): |
| 55 | + ... |
| 56 | + ValueError: Capacitance must be positive. |
| 57 | + """ |
| 58 | + |
| 59 | + if source_voltage <= 0: |
| 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) |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == "__main__": |
| 69 | + import doctest |
| 70 | + |
| 71 | + doctest.testmod() |
0 commit comments