From 6e4f6520eed86a9d2564b59d065ae38b2c314040 Mon Sep 17 00:00:00 2001 From: pluto-tofu Date: Tue, 10 Oct 2023 22:16:29 +0530 Subject: [PATCH 1/6] added the algorithm to compute the terminal velocity of an object falling in a fluid --- physics/terminal_velocity.py | 53 ++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 physics/terminal_velocity.py diff --git a/physics/terminal_velocity.py b/physics/terminal_velocity.py new file mode 100644 index 000000000000..5d33b9a4ba52 --- /dev/null +++ b/physics/terminal_velocity.py @@ -0,0 +1,53 @@ +""" +Terminal velocity is defined as the highest velocity attained by an +object falling through a fluid. It is observed when the sum of drag force +and buoyancy is equal to the downward gravity force acting on the +object. The acceleration of the object is zero as the net force acting on +the object is zero. + +Vt = ((2 * m * g)/(ρ * A * Cd))^0.5 + +where : +Vt = Terminal velocity (in m/s) +m = Mass of the falling object (in Kg) +g = Acceleration due to gravity (value taken : 9.8 m/s^2) +ρ = Density of the fluid through which the object is falling (in Kg/m^3) +A = Projected area of the object (in m^2) +Cd = Drag coefficient (dimensionless) +""" + + +def terminal_velocity( + mass: float, density: float, area: float, drag_coefficient: float +) -> float: + """ + >>> terminal_velocity(1, 25, 0.6, 0.77) + 1.3026778945578592 + >>> terminal_velocity(2, 100, 0.45, 0.23) + 1.9461345311993645 + >>> terminal_velocity(5, 50, 0.2, 0.5) + 4.427188724235731 + >>> terminal_velocity(-5, 50, -0.2, -2) + Traceback (most recent call last): + ... + ValueError: mass, density, area and the drag coeffiecient all need to be positive + >>> terminal_velocity(3, -20, -1, 2) + Traceback (most recent call last): + ... + ValueError: mass, density, area and the drag coeffiecient all need to be positive + >>> terminal_velocity(-2, -1, -0.44, -1) + Traceback (most recent call last): + ... + ValueError: mass, density, area and the drag coeffiecient all need to be positive + """ + if mass <= 0 or density <= 0 or area <= 0 or drag_coefficient <= 0: + raise ValueError( + "mass, density, area and the drag coeffiecient all need to be positive" + ) + return ((2 * mass * 9.8) / (density * area * drag_coefficient)) ** 0.5 + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 71f3534bfb8db08157d64946b5c8b374fff55201 Mon Sep 17 00:00:00 2001 From: pluto-tofu Date: Tue, 10 Oct 2023 22:23:42 +0530 Subject: [PATCH 2/6] fixed spelling mistake --- physics/terminal_velocity.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/physics/terminal_velocity.py b/physics/terminal_velocity.py index 5d33b9a4ba52..d04ad478ebc6 100644 --- a/physics/terminal_velocity.py +++ b/physics/terminal_velocity.py @@ -30,19 +30,19 @@ def terminal_velocity( >>> terminal_velocity(-5, 50, -0.2, -2) Traceback (most recent call last): ... - ValueError: mass, density, area and the drag coeffiecient all need to be positive + ValueError: mass, density, area and the drag coefficient all need to be positive >>> terminal_velocity(3, -20, -1, 2) Traceback (most recent call last): ... - ValueError: mass, density, area and the drag coeffiecient all need to be positive + ValueError: mass, density, area and the drag coefficient all need to be positive >>> terminal_velocity(-2, -1, -0.44, -1) Traceback (most recent call last): ... - ValueError: mass, density, area and the drag coeffiecient all need to be positive + ValueError: mass, density, area and the drag coefficient all need to be positive """ if mass <= 0 or density <= 0 or area <= 0 or drag_coefficient <= 0: raise ValueError( - "mass, density, area and the drag coeffiecient all need to be positive" + "mass, density, area and the drag coefficient all need to be positive" ) return ((2 * mass * 9.8) / (density * area * drag_coefficient)) ** 0.5 From e21bbaad95a64cd54d719095a3dd5cfffedb977d Mon Sep 17 00:00:00 2001 From: pluto-tofu Date: Wed, 11 Oct 2023 05:11:19 +0530 Subject: [PATCH 3/6] fixed issues in topic description --- physics/terminal_velocity.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/physics/terminal_velocity.py b/physics/terminal_velocity.py index d04ad478ebc6..d41eb413fdfe 100644 --- a/physics/terminal_velocity.py +++ b/physics/terminal_velocity.py @@ -1,4 +1,7 @@ """ +Title : Computing the terminal velocity of an object falling + through a fluid. + Terminal velocity is defined as the highest velocity attained by an object falling through a fluid. It is observed when the sum of drag force and buoyancy is equal to the downward gravity force acting on the @@ -14,6 +17,8 @@ ρ = Density of the fluid through which the object is falling (in Kg/m^3) A = Projected area of the object (in m^2) Cd = Drag coefficient (dimensionless) + +Reference : https://byjus.com/physics/derivation-of-terminal-velocity/ """ From 0ca5e820c0fcebd8815752c9786f21283f6f67d7 Mon Sep 17 00:00:00 2001 From: pluto-tofu Date: Mon, 23 Oct 2023 10:49:24 +0530 Subject: [PATCH 4/6] imported the value of g from scipy and changed the doctests accordingly --- physics/terminal_velocity.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/physics/terminal_velocity.py b/physics/terminal_velocity.py index d41eb413fdfe..8ec8535310b5 100644 --- a/physics/terminal_velocity.py +++ b/physics/terminal_velocity.py @@ -1,3 +1,4 @@ +from scipy.constants import g """ Title : Computing the terminal velocity of an object falling through a fluid. @@ -13,7 +14,7 @@ where : Vt = Terminal velocity (in m/s) m = Mass of the falling object (in Kg) -g = Acceleration due to gravity (value taken : 9.8 m/s^2) +g = Acceleration due to gravity (value taken : imported from scipy) ρ = Density of the fluid through which the object is falling (in Kg/m^3) A = Projected area of the object (in m^2) Cd = Drag coefficient (dimensionless) @@ -27,11 +28,11 @@ def terminal_velocity( ) -> float: """ >>> terminal_velocity(1, 25, 0.6, 0.77) - 1.3026778945578592 + 1.3031197996044768 >>> terminal_velocity(2, 100, 0.45, 0.23) - 1.9461345311993645 + 1.9467947148674276 >>> terminal_velocity(5, 50, 0.2, 0.5) - 4.427188724235731 + 4.428690551393267 >>> terminal_velocity(-5, 50, -0.2, -2) Traceback (most recent call last): ... @@ -49,7 +50,7 @@ def terminal_velocity( raise ValueError( "mass, density, area and the drag coefficient all need to be positive" ) - return ((2 * mass * 9.8) / (density * area * drag_coefficient)) ** 0.5 + return ((2 * mass * g) / (density * area * drag_coefficient)) ** 0.5 if __name__ == "__main__": From 2cd825f33fa9d3f0997d551dee51399d13229705 Mon Sep 17 00:00:00 2001 From: pluto-tofu Date: Mon, 23 Oct 2023 11:04:11 +0530 Subject: [PATCH 5/6] fixed formatting --- physics/terminal_velocity.py | 1 + 1 file changed, 1 insertion(+) diff --git a/physics/terminal_velocity.py b/physics/terminal_velocity.py index 8ec8535310b5..586426f514b7 100644 --- a/physics/terminal_velocity.py +++ b/physics/terminal_velocity.py @@ -1,4 +1,5 @@ from scipy.constants import g + """ Title : Computing the terminal velocity of an object falling through a fluid. From c806605f74fd38400f8131e7e651e40004010fd4 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Mon, 23 Oct 2023 01:38:50 -0400 Subject: [PATCH 6/6] Apply suggestions from code review --- physics/terminal_velocity.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/physics/terminal_velocity.py b/physics/terminal_velocity.py index 586426f514b7..cec54162e2b4 100644 --- a/physics/terminal_velocity.py +++ b/physics/terminal_velocity.py @@ -1,5 +1,3 @@ -from scipy.constants import g - """ Title : Computing the terminal velocity of an object falling through a fluid. @@ -23,6 +21,8 @@ Reference : https://byjus.com/physics/derivation-of-terminal-velocity/ """ +from scipy.constants import g + def terminal_velocity( mass: float, density: float, area: float, drag_coefficient: float