Skip to content

Commit a16a4e0

Browse files
Add relativistic kinetic energy calculation with doctests and type hints
1 parent 68473af commit a16a4e0

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""
2+
Find the relativistic kinetic energy of a particle, given its rest mass and velocity.
3+
4+
Description: In special relativity, the kinetic energy of a particle is the extra
5+
energy it has due to its motion beyond the energy associated with its rest mass.
6+
It is defined as the difference between the total relativistic energy and the rest
7+
energy of the particle. After a force does work to accelerate a particle from rest
8+
to some high speed comparable to the speed of light, the particle carries this
9+
relativistic kinetic energy as long as its speed stays the same. The same amount of
10+
energy must be removed (for example, by an opposite force) to slow the particle
11+
back down to rest. Formally, relativistic kinetic energy appears in the relativistic
12+
energy momentum relation and depends on the Lorentz factor, which encodes how time
13+
and space change at high speeds.
14+
15+
In relativistic mechanics, the kinetic energy K of a particle with rest mass m
16+
moving at speed v is
17+
18+
K = (y - 1) m c^2,
19+
20+
where c is the speed of light in vacuum and
21+
22+
y = 1 / sqrt(1 - v^2 / c^2)
23+
24+
is the Lorentz factor. At speeds much smaller than c, this expression reduces to the
25+
classical formula K ≈ (1/2) m v^2, so the relativistic result agrees with Newtonian
26+
kinetic energy in the low velocity limit.The standard unit of kinetic energy is the
27+
joule, while the English unit of kinetic energy is the foot-pound.
28+
29+
Reference : https://en.wikipedia.org/wiki/Kinetic_energy
30+
"""
31+
32+
from math import sqrt
33+
34+
from scipy.constants import c # speed of light in vacuum (299792458 m/s)
35+
36+
37+
def relativistic_kinetic_energy(mass: float, velocity: float) -> float:
38+
"""
39+
Calculate relativistic kinetic energy.
40+
mass --- kg
41+
velocity ---- m/s
42+
K.E ---- j
43+
44+
>>> relativistic_kinetic_energy(10,10)
45+
598.6912157608277
46+
>>> relativistic_kinetic_energy(40,200000)
47+
800000266962.5057
48+
>>> relativistic_kinetic_energy(50,100000)
49+
250000021062.11475
50+
>>> relativistic_kinetic_energy(0,0)
51+
0.0
52+
>>> relativistic_kinetic_energy(100,0)
53+
0.0
54+
55+
"""
56+
57+
if (mass < 0 ):
58+
raise ValueError("The mass of a body cannot be negative")
59+
else:
60+
gamma = 1/sqrt(1 - (velocity**2 / c**2))
61+
return (gamma-1) * mass * c**2
62+
63+
64+
if __name__ == "__main__":
65+
import doctest
66+
67+
doctest.testmod()

0 commit comments

Comments
 (0)