|
| 1 | +# source - The ARRL Handbook for Radio Communications |
| 2 | +# https://en.wikipedia.org/wiki/RL_circuit |
| 3 | + |
| 4 | +""" |
| 5 | +Description |
| 6 | +----------- |
| 7 | +Inductor is a passive electronic device which stores energy but unlike capacitor, it |
| 8 | +stores energy in its 'magnetic field' or 'magnetostatic field'. |
| 9 | +
|
| 10 | +When inductor is connected to 'DC' current source nothing happens it just works like a |
| 11 | +wire because it's real effect cannot be seen while 'DC' is connected, its not even |
| 12 | +going to store energy. Inductor stores energy only when it is working on 'AC' current. |
| 13 | +
|
| 14 | +Connecting a inductor in series with a resistor(when R = 0) to a 'AC' potential source, |
| 15 | +from zero to a finite value causes a sudden voltage to induced in inductor which |
| 16 | +opposes the current. which results in initially slowly current rise. However it would |
| 17 | +cease if there is no further changes in current. With resistance zero current will never |
| 18 | +stop rising. |
| 19 | +
|
| 20 | +'Resistance(ohms) / Inductance(henrys)' is known as RL-timeconstant. It also represents |
| 21 | +as τ (tau). While the charging of a inductor with a resistor results in |
| 22 | +a exponential function. |
| 23 | +
|
| 24 | +when inductor is connected across 'AC' potential source. It starts to store the energy |
| 25 | +in its 'magnetic field'.with the help 'RL-time-constant' we can find current at any time |
| 26 | +in inductor while it is charging. |
| 27 | +""" |
| 28 | +from math import exp # value of exp = 2.718281828459… |
| 29 | + |
| 30 | + |
| 31 | +def charging_inductor( |
| 32 | + source_voltage: float, # source_voltage should be in volts. |
| 33 | + resistance: float, # resistance should be in ohms. |
| 34 | + inductance: float, # inductance should be in henrys. |
| 35 | + time: float, # time should in seconds. |
| 36 | +) -> float: |
| 37 | + """ |
| 38 | + Find inductor current at any nth second after initiating its charging. |
| 39 | +
|
| 40 | + Examples |
| 41 | + -------- |
| 42 | + >>> charging_inductor(source_voltage=5.8,resistance=1.5,inductance=2.3,time=2) |
| 43 | + 2.817 |
| 44 | +
|
| 45 | + >>> charging_inductor(source_voltage=8,resistance=5,inductance=3,time=2) |
| 46 | + 1.543 |
| 47 | +
|
| 48 | + >>> charging_inductor(source_voltage=8,resistance=5*pow(10,2),inductance=3,time=2) |
| 49 | + 0.016 |
| 50 | +
|
| 51 | + >>> charging_inductor(source_voltage=-8,resistance=100,inductance=15,time=12) |
| 52 | + Traceback (most recent call last): |
| 53 | + ... |
| 54 | + ValueError: Source voltage must be positive. |
| 55 | +
|
| 56 | + >>> charging_inductor(source_voltage=80,resistance=-15,inductance=100,time=5) |
| 57 | + Traceback (most recent call last): |
| 58 | + ... |
| 59 | + ValueError: Resistance must be positive. |
| 60 | +
|
| 61 | + >>> charging_inductor(source_voltage=12,resistance=200,inductance=-20,time=5) |
| 62 | + Traceback (most recent call last): |
| 63 | + ... |
| 64 | + ValueError: Inductance must be positive. |
| 65 | +
|
| 66 | + >>> charging_inductor(source_voltage=0,resistance=200,inductance=20,time=5) |
| 67 | + Traceback (most recent call last): |
| 68 | + ... |
| 69 | + ValueError: Source voltage must be positive. |
| 70 | +
|
| 71 | + >>> charging_inductor(source_voltage=10,resistance=0,inductance=20,time=5) |
| 72 | + Traceback (most recent call last): |
| 73 | + ... |
| 74 | + ValueError: Resistance must be positive. |
| 75 | +
|
| 76 | + >>> charging_inductor(source_voltage=15, resistance=25, inductance=0, time=5) |
| 77 | + Traceback (most recent call last): |
| 78 | + ... |
| 79 | + ValueError: Inductance must be positive. |
| 80 | + """ |
| 81 | + |
| 82 | + if source_voltage <= 0: |
| 83 | + raise ValueError("Source voltage must be positive.") |
| 84 | + if resistance <= 0: |
| 85 | + raise ValueError("Resistance must be positive.") |
| 86 | + if inductance <= 0: |
| 87 | + raise ValueError("Inductance must be positive.") |
| 88 | + return round( |
| 89 | + source_voltage / resistance * (1 - exp((-time * resistance) / inductance)), 3 |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + import doctest |
| 95 | + |
| 96 | + doctest.testmod() |
0 commit comments