Skip to content

updated physics/archimedes_principle.py #10479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 34 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
1427fe3
avg and mps speed formulae added
Baron105 Oct 10, 2023
0608c40
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2023
7457b25
avg and mps speed formulae added
Baron105 Oct 10, 2023
b1ef5a3
fixed_spacing
Baron105 Oct 10, 2023
c957bd7
.
Baron105 Oct 10, 2023
07502c8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2023
ead4f47
spacing
Baron105 Oct 10, 2023
c18b39d
.
Baron105 Oct 10, 2023
cb006c3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2023
ab04734
ws
Baron105 Oct 10, 2023
69487f3
Merge branch 'master' of https://github.com/Baron105/Python
Baron105 Oct 10, 2023
6a741dc
added amicable numbers
Baron105 Oct 10, 2023
d13c09d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2023
18df9ba
Merge branch 'TheAlgorithms:master' into master
Baron105 Oct 10, 2023
63734b9
added amicable numbers
Baron105 Oct 10, 2023
aa5f176
descriptive
Baron105 Oct 10, 2023
9779c1a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2023
aa4d4e8
spacing
Baron105 Oct 10, 2023
7bb663f
Merge branch 'master' of https://github.com/Baron105/Python
Baron105 Oct 10, 2023
52c2cb8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2023
643958b
removed
Baron105 Oct 10, 2023
65f5851
Merge branch 'master' of https://github.com/Baron105/Python
Baron105 Oct 10, 2023
7f14ab9
Merge branch 'TheAlgorithms:master' into master
Baron105 Oct 12, 2023
c05ed7c
changed name of file and added code improvements
Baron105 Oct 12, 2023
886fb5d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 12, 2023
97b89cd
issues fixed due to pi
Baron105 Oct 12, 2023
a1c783a
Merge branch 'master' of https://github.com/Baron105/Python
Baron105 Oct 12, 2023
1c2b7b2
requested changes added
Baron105 Oct 12, 2023
5aa65b8
Merge branch 'TheAlgorithms:master' into master
Baron105 Oct 13, 2023
59e9c01
Merge branch 'TheAlgorithms:master' into master
Baron105 Oct 15, 2023
37cc5a4
added some doctests for exception handling, imported g from scipy and…
Baron105 Oct 15, 2023
cf56671
removed_scipy_import
Baron105 Oct 15, 2023
802a381
Update and rename archimedes_principle.py to archimedes_principle_of_…
cclauss Oct 15, 2023
208932d
Update archimedes_principle_of_buoyant_force.py
cclauss Oct 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 0 additions & 49 deletions physics/archimedes_principle.py

This file was deleted.

63 changes: 63 additions & 0 deletions physics/archimedes_principle_of_buoyant_force.py
Original file line number Diff line number Diff line change
@@ -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()