TheAlgorithms-Python

Форк
0
/
charging_inductor.py 
97 строк · 3.4 Кб
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

29
from math import exp  # value of exp = 2.718281828459…
30

31

32
def charging_inductor(
33
    source_voltage: float,  # source_voltage should be in volts.
34
    resistance: float,  # resistance should be in ohms.
35
    inductance: float,  # inductance should be in henrys.
36
    time: float,  # time should in seconds.
37
) -> float:
38
    """
39
    Find inductor current at any nth second after initiating its charging.
40

41
    Examples
42
    --------
43
    >>> charging_inductor(source_voltage=5.8,resistance=1.5,inductance=2.3,time=2)
44
    2.817
45

46
    >>> charging_inductor(source_voltage=8,resistance=5,inductance=3,time=2)
47
    1.543
48

49
    >>> charging_inductor(source_voltage=8,resistance=5*pow(10,2),inductance=3,time=2)
50
    0.016
51

52
    >>> charging_inductor(source_voltage=-8,resistance=100,inductance=15,time=12)
53
    Traceback (most recent call last):
54
        ...
55
    ValueError: Source voltage must be positive.
56

57
    >>> charging_inductor(source_voltage=80,resistance=-15,inductance=100,time=5)
58
    Traceback (most recent call last):
59
        ...
60
    ValueError: Resistance must be positive.
61

62
    >>> charging_inductor(source_voltage=12,resistance=200,inductance=-20,time=5)
63
    Traceback (most recent call last):
64
        ...
65
    ValueError: Inductance must be positive.
66

67
    >>> charging_inductor(source_voltage=0,resistance=200,inductance=20,time=5)
68
    Traceback (most recent call last):
69
        ...
70
    ValueError: Source voltage must be positive.
71

72
    >>> charging_inductor(source_voltage=10,resistance=0,inductance=20,time=5)
73
    Traceback (most recent call last):
74
        ...
75
    ValueError: Resistance must be positive.
76

77
    >>> charging_inductor(source_voltage=15, resistance=25, inductance=0, time=5)
78
    Traceback (most recent call last):
79
        ...
80
    ValueError: Inductance must be positive.
81
    """
82

83
    if source_voltage <= 0:
84
        raise ValueError("Source voltage must be positive.")
85
    if resistance <= 0:
86
        raise ValueError("Resistance must be positive.")
87
    if inductance <= 0:
88
        raise ValueError("Inductance must be positive.")
89
    return round(
90
        source_voltage / resistance * (1 - exp((-time * resistance) / inductance)), 3
91
    )
92

93

94
if __name__ == "__main__":
95
    import doctest
96

97
    doctest.testmod()
98

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.