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