Skip to content

Commit 10e7044

Browse files
Add Faraday-Lenz Law implementation
1 parent 28959e0 commit 10e7044

1 file changed

Lines changed: 87 additions & 0 deletions

File tree

physics/faraday_lenz_law.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
"""
2+
According to Faraday's law, the emergence of electric currents depends on the change in
3+
magnetic flux. Therefore, we write that the time variation of the magnetic flux is
4+
equivalent to an electric potential measured in volts (V), which, for historical
5+
reasons, is called the induced electromotive force (ε). This relationship is expressed
6+
by the following formula:
7+
8+
---------------
9+
| ε = ΔΦ / Δt |
10+
---------------
11+
12+
ε --> induced electromotive force (V - volts)
13+
14+
ΔΦ = ΦF - Φi - variation in magnetic flux (Wb)
15+
16+
Δt - time interval (s)
17+
18+
Furthermore, due to the principle of conservation of energy, we need to add a negative
19+
sign to Faraday's law. This signal was introduced by Lenz's Law, which allows us to
20+
determine the direction of the electric current:
21+
22+
An electric current will always be formed in a direction such that the magnetic flux it
23+
produces opposes the magnetic flux that induced it.
24+
25+
The combination of these pieces of information gives rise to the Faraday-Lenz Law.
26+
Check it out:
27+
28+
-----------------
29+
| ε = - ΔΦ / Δt |
30+
-----------------
31+
32+
(Description adapted from https://en.wikipedia.org/wiki/Faraday%27s_law_of_induction )
33+
"""
34+
35+
36+
37+
def check_args(final_flux: float, initinal_flux: float, time_interval: float) -> None:
38+
"""
39+
Check that the arguments are valid
40+
"""
41+
42+
# Ensure valid instance
43+
if not isinstance(final_flux, (int, float)):
44+
raise TypeError("Invalid final flux. Should be an integer or float.")
45+
46+
if not isinstance(initinal_flux, (int, float)):
47+
raise TypeError("Invalid final flux. Should be an integer or float.")
48+
49+
if not isinstance(time_interval, (int, float)):
50+
raise TypeError("Invalid time interval. Should be an integer or float.")
51+
52+
# Ensure valid time interval
53+
if time_interval < 0:
54+
raise ValueError("Invalid time interval. Should be a positive number.")
55+
56+
57+
def induced_electromotive_force(
58+
final_flux: float,
59+
initinal_flux: float,
60+
time_interval: float
61+
) -> float:
62+
"""
63+
>>> induced_electromotive_force(50.0, 20, 3.0)
64+
-10.0
65+
>>> induced_electromotive_force(40, 30, 10.0)
66+
-1.0
67+
>>> induced_electromotive_force(30.0, 50.0, 10)
68+
2.0
69+
>>> induced_electromotive_force(100, 100.0, 20.0)
70+
0.0
71+
>>> induced_electromotive_force(10.0, 2.0, -2.0)
72+
Traceback (most recent call last):
73+
...
74+
Invalid time interval. Should be a positive number.
75+
>>> induced_electromotive_force(11.0, 'a', 5.0)
76+
Traceback (most recent call last):
77+
...
78+
TypeError: Invalid final flux. Should be an integer or float.
79+
"""
80+
check_args(final_flux, initinal_flux, time_interval)
81+
return round((final_flux - initinal_flux) / time_interval, 1)
82+
83+
84+
if __name__ == "__main__":
85+
from doctest import testmod
86+
87+
testmod()

0 commit comments

Comments
 (0)