Skip to content
40 changes: 40 additions & 0 deletions physics/hookes_law.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Hookes Law
"""
Hookes Law states that the Force is directly proportional to the extension

Check failure on line 3 in physics/hookes_law.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

physics/hookes_law.py:3:75: W291 Trailing whitespace
or compression of an elastic object, provided the limit of proportionality

Check failure on line 4 in physics/hookes_law.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

physics/hookes_law.py:4:75: W291 Trailing whitespace
is not exceeded.

Formulae : F = -k*x

F: Force
k: Spring constant
x: displacement from the equilibrium position

The negative sign indicates that the restoring force acts in the opposite

Check failure on line 13 in physics/hookes_law.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

physics/hookes_law.py:13:74: W291 Trailing whitespace
direction to the displacement, always working to bring the object back to

Check failure on line 14 in physics/hookes_law.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

physics/hookes_law.py:14:74: W291 Trailing whitespace
its original state.

Reference: https://en.wikipedia.org/wiki/Hooke%27s_law

"""


def hookes_law(k: float, x: float) -> float:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: k

Please provide descriptive name for the parameter: x

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest me

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: k

Please provide descriptive name for the parameter: x

"""
Calculate the Hookes law from the given values of spring constant 'k'
and the displacement 'x' from the equilibrium position.

>>> hookes_law(200, 0.05)
-10.0
>>> hookes_law(50, 5)
-250
>>> hookes_law(300, 3)
-900
"""
return round(-k * x, 2)


if __name__ == "__main__":
import doctest

doctest.testmod()
Loading