Find capacitor voltage at any nth second after initiating its charging. Examples -------- >>> charging_capacitor(source_voltage=.2,resistance=.9,capacitance=8.4,time_sec=.5) 0.013 >>> charging_capacitor(source_voltage=2.2,resistance=3.5,capacitance=2.4,time_sec=9) 1.44
(
source_voltage: float, # voltage in volts.
resistance: float, # resistance in ohms.
capacitance: float, # capacitance in farads.
time_sec: float, # time in seconds after charging initiation of capacitor.
)
| 19 | |
| 20 | |
| 21 | def charging_capacitor( |
| 22 | source_voltage: float, # voltage in volts. |
| 23 | resistance: float, # resistance in ohms. |
| 24 | capacitance: float, # capacitance in farads. |
| 25 | time_sec: float, # time in seconds after charging initiation of capacitor. |
| 26 | ) -> float: |
| 27 | """ |
| 28 | Find capacitor voltage at any nth second after initiating its charging. |
| 29 | |
| 30 | Examples |
| 31 | -------- |
| 32 | >>> charging_capacitor(source_voltage=.2,resistance=.9,capacitance=8.4,time_sec=.5) |
| 33 | 0.013 |
| 34 | |
| 35 | >>> charging_capacitor(source_voltage=2.2,resistance=3.5,capacitance=2.4,time_sec=9) |
| 36 | 1.446 |
| 37 | |
| 38 | >>> charging_capacitor(source_voltage=15,resistance=200,capacitance=20,time_sec=2) |
| 39 | 0.007 |
| 40 | |
| 41 | >>> charging_capacitor(20, 2000, 30*pow(10,-5), 4) |
| 42 | 19.975 |
| 43 | |
| 44 | >>> charging_capacitor(source_voltage=0,resistance=10.0,capacitance=.30,time_sec=3) |
| 45 | Traceback (most recent call last): |
| 46 | ... |
| 47 | ValueError: Source voltage must be positive. |
| 48 | |
| 49 | >>> charging_capacitor(source_voltage=20,resistance=-2000,capacitance=30,time_sec=4) |
| 50 | Traceback (most recent call last): |
| 51 | ... |
| 52 | ValueError: Resistance must be positive. |
| 53 | |
| 54 | >>> charging_capacitor(source_voltage=30,resistance=1500,capacitance=0,time_sec=4) |
| 55 | Traceback (most recent call last): |
| 56 | ... |
| 57 | ValueError: Capacitance must be positive. |
| 58 | """ |
| 59 | |
| 60 | if source_voltage <= 0: |
| 61 | raise ValueError("Source voltage must be positive.") |
| 62 | if resistance <= 0: |
| 63 | raise ValueError("Resistance must be positive.") |
| 64 | if capacitance <= 0: |
| 65 | raise ValueError("Capacitance must be positive.") |
| 66 | return round(source_voltage * (1 - exp(-time_sec / (resistance * capacitance))), 3) |
| 67 | |
| 68 | |
| 69 | if __name__ == "__main__": |
nothing calls this directly
no outgoing calls
no test coverage detected