Skip to content

Commit abc3909

Browse files
Added the algorithm to compute the terminal velocity of an object fal… (TheAlgorithms#10237)
* added the algorithm to compute the terminal velocity of an object falling in a fluid * fixed spelling mistake * fixed issues in topic description * imported the value of g from scipy and changed the doctests accordingly * fixed formatting * Apply suggestions from code review --------- Co-authored-by: Tianyi Zheng <[email protected]>
1 parent 68faebe commit abc3909

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

physics/terminal_velocity.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
Title : Computing the terminal velocity of an object falling
3+
through a fluid.
4+
5+
Terminal velocity is defined as the highest velocity attained by an
6+
object falling through a fluid. It is observed when the sum of drag force
7+
and buoyancy is equal to the downward gravity force acting on the
8+
object. The acceleration of the object is zero as the net force acting on
9+
the object is zero.
10+
11+
Vt = ((2 * m * g)/(ρ * A * Cd))^0.5
12+
13+
where :
14+
Vt = Terminal velocity (in m/s)
15+
m = Mass of the falling object (in Kg)
16+
g = Acceleration due to gravity (value taken : imported from scipy)
17+
ρ = Density of the fluid through which the object is falling (in Kg/m^3)
18+
A = Projected area of the object (in m^2)
19+
Cd = Drag coefficient (dimensionless)
20+
21+
Reference : https://byjus.com/physics/derivation-of-terminal-velocity/
22+
"""
23+
24+
from scipy.constants import g
25+
26+
27+
def terminal_velocity(
28+
mass: float, density: float, area: float, drag_coefficient: float
29+
) -> float:
30+
"""
31+
>>> terminal_velocity(1, 25, 0.6, 0.77)
32+
1.3031197996044768
33+
>>> terminal_velocity(2, 100, 0.45, 0.23)
34+
1.9467947148674276
35+
>>> terminal_velocity(5, 50, 0.2, 0.5)
36+
4.428690551393267
37+
>>> terminal_velocity(-5, 50, -0.2, -2)
38+
Traceback (most recent call last):
39+
...
40+
ValueError: mass, density, area and the drag coefficient all need to be positive
41+
>>> terminal_velocity(3, -20, -1, 2)
42+
Traceback (most recent call last):
43+
...
44+
ValueError: mass, density, area and the drag coefficient all need to be positive
45+
>>> terminal_velocity(-2, -1, -0.44, -1)
46+
Traceback (most recent call last):
47+
...
48+
ValueError: mass, density, area and the drag coefficient all need to be positive
49+
"""
50+
if mass <= 0 or density <= 0 or area <= 0 or drag_coefficient <= 0:
51+
raise ValueError(
52+
"mass, density, area and the drag coefficient all need to be positive"
53+
)
54+
return ((2 * mass * g) / (density * area * drag_coefficient)) ** 0.5
55+
56+
57+
if __name__ == "__main__":
58+
import doctest
59+
60+
doctest.testmod()

0 commit comments

Comments
 (0)