Skip to content

Create energy_mass_equivalent.py #1

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 1 commit into from
Oct 1, 2023
Merged
Changes from all commits
Commits
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
55 changes: 55 additions & 0 deletions physics/energy_mass_equivalent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import scipy.constants as const
import math


def energy_equivalent_for_stationary_mass(mass_kg):
"""
Calculate the energy equivalent for a given mass using E=mc^2.

Parameters:
mass_kg (float): Mass in kilograms.(must be positive)

Returns:
float: Energy equivalent in joules.

>>>energy_equivalent_for_stationary_mass(4)
3.5950207149472704e+17

"""
if mass_kg<0:
ValueError("mass of object cannot be negative")

speed_of_light = const.speed_of_light # Speed of light in meters/second
energy_joules = mass_kg * (speed_of_light ** 2)
return energy_joules


def energy_equivalent_for_moving_mass(mass_kg, velocity_m_s):
"""
Calculate the energy equivalent for a moving mass using relativistic energy-momentum relation(E^2= m^2*c^4 +p^2*c^2).

Parameters:
mass_kg (float): Mass in kilograms.(must be positive)
velocity_m_s (float): Velocity in meters per second.(can be negative as well as positive)

Returns:
float: Energy equivalent in joules.

>>>energy_equivalent_for_moving_mass(1,5675)
1701322225868.2546
"""
if mass_kg<0:
ValueError("mass of object cannot be negative")

speed_of_light = const.speed_of_light # Speed of light in meters/second

# Calculating momentum
momentum = mass_kg * velocity_m_s / math.sqrt(1 - (velocity_m_s**2 / speed_of_light**2))

# Calculating energy using the relativistic energy-momentum relation
energy_joules = math.sqrt((mass_kg * speed_of_light)**2 + (momentum * speed_of_light)**2)
return energy_joules

if __name__ == "__main__":
import doctest
doctest.testmod()