mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
updated physics/archimedes_principle.py (#10479)
* avg and mps speed formulae added * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * avg and mps speed formulae added * fixed_spacing * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * spacing * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * ws * added amicable numbers * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * added amicable numbers * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * spacing * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * removed * changed name of file and added code improvements * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * issues fixed due to pi * requested changes added * added some doctests for exception handling, imported g from scipy and allowed zero gravity * removed_scipy_import * Update and rename archimedes_principle.py to archimedes_principle_of_buoyant_force.py * Update archimedes_principle_of_buoyant_force.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
68e6d5ad7e
commit
7bdd1cd2be
|
@ -1,49 +0,0 @@
|
|||
"""
|
||||
Calculates buoyant force on object submerged within static fluid.
|
||||
Discovered by greek mathematician, Archimedes. The principle is named after him.
|
||||
|
||||
Equation for calculating buoyant force:
|
||||
Fb = ρ * V * g
|
||||
|
||||
Source:
|
||||
- https://en.wikipedia.org/wiki/Archimedes%27_principle
|
||||
"""
|
||||
|
||||
|
||||
# Acceleration Constant on Earth (unit m/s^2)
|
||||
g = 9.80665
|
||||
|
||||
|
||||
def archimedes_principle(
|
||||
fluid_density: float, volume: float, gravity: float = g
|
||||
) -> float:
|
||||
"""
|
||||
Args:
|
||||
fluid_density: density of fluid (kg/m^3)
|
||||
volume: volume of object / liquid being displaced by object
|
||||
gravity: Acceleration from gravity. Gravitational force on system,
|
||||
Default is Earth Gravity
|
||||
returns:
|
||||
buoyant force on object in Newtons
|
||||
|
||||
>>> archimedes_principle(fluid_density=997, volume=0.5, gravity=9.8)
|
||||
4885.3
|
||||
>>> archimedes_principle(fluid_density=997, volume=0.7)
|
||||
6844.061035
|
||||
"""
|
||||
|
||||
if fluid_density <= 0:
|
||||
raise ValueError("Impossible fluid density")
|
||||
if volume < 0:
|
||||
raise ValueError("Impossible Object volume")
|
||||
if gravity <= 0:
|
||||
raise ValueError("Impossible Gravity")
|
||||
|
||||
return fluid_density * gravity * volume
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
# run doctest
|
||||
doctest.testmod()
|
63
physics/archimedes_principle_of_buoyant_force.py
Normal file
63
physics/archimedes_principle_of_buoyant_force.py
Normal file
|
@ -0,0 +1,63 @@
|
|||
"""
|
||||
Calculate the buoyant force of any body completely or partially submerged in a static
|
||||
fluid. This principle was discovered by the Greek mathematician Archimedes.
|
||||
|
||||
Equation for calculating buoyant force:
|
||||
Fb = ρ * V * g
|
||||
|
||||
https://en.wikipedia.org/wiki/Archimedes%27_principle
|
||||
"""
|
||||
|
||||
|
||||
# Acceleration Constant on Earth (unit m/s^2)
|
||||
g = 9.80665 # Also available in scipy.constants.g
|
||||
|
||||
|
||||
def archimedes_principle(
|
||||
fluid_density: float, volume: float, gravity: float = g
|
||||
) -> float:
|
||||
"""
|
||||
Args:
|
||||
fluid_density: density of fluid (kg/m^3)
|
||||
volume: volume of object/liquid being displaced by the object (m^3)
|
||||
gravity: Acceleration from gravity. Gravitational force on the system,
|
||||
The default is Earth Gravity
|
||||
returns:
|
||||
the buoyant force on an object in Newtons
|
||||
|
||||
>>> archimedes_principle(fluid_density=500, volume=4, gravity=9.8)
|
||||
19600.0
|
||||
>>> archimedes_principle(fluid_density=997, volume=0.5, gravity=9.8)
|
||||
4885.3
|
||||
>>> archimedes_principle(fluid_density=997, volume=0.7)
|
||||
6844.061035
|
||||
>>> archimedes_principle(fluid_density=997, volume=-0.7)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: Impossible object volume
|
||||
>>> archimedes_principle(fluid_density=0, volume=0.7)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: Impossible fluid density
|
||||
>>> archimedes_principle(fluid_density=997, volume=0.7, gravity=0)
|
||||
0.0
|
||||
>>> archimedes_principle(fluid_density=997, volume=0.7, gravity=-9.8)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: Impossible gravity
|
||||
"""
|
||||
|
||||
if fluid_density <= 0:
|
||||
raise ValueError("Impossible fluid density")
|
||||
if volume <= 0:
|
||||
raise ValueError("Impossible object volume")
|
||||
if gravity < 0:
|
||||
raise ValueError("Impossible gravity")
|
||||
|
||||
return fluid_density * gravity * volume
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
Loading…
Reference in New Issue
Block a user