Skip to content

Commit fc92cad

Browse files
authored
Merge pull request #1 from autobotx343/add-collisions
Add collisions
2 parents 6c04620 + d56039a commit fc92cad

1 file changed

Lines changed: 128 additions & 0 deletions

File tree

physics/collisions.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
"""
2+
Finding the type of collision and calculating final velocities after collisions are fundamental concepts in physics.
3+
This module provides functions to compute the final velocities of two masses after both inelastic and elastic collisions,
4+
as well as a function to determine the type of collision based on initial and final velocities.
5+
6+
Description: Collisions in physics refers to the interaction between two masses when they collide head-on. There are 2 types of
7+
collisions: inelastic and elastic. In an inelastic collision, the two masses stick together and move with a common velocity
8+
after the collision. In an elastic collision, both momentum and kinetic energy are conserved, and the masses bounce off each other without sticking together.
9+
Momentum is the product of mass and velocity, while kinetic energy is given by the formula (1/2) * mass * velocity^2.
10+
The type of collision can be determined by comparing the initial and final momentum and kinetic energy of the system.
11+
12+
Reference: https://en.wikipedia.org/wiki/Collision
13+
"""
14+
15+
16+
def inelastic_collisions(
17+
mass1: float, mass2: float, velocity1: float, velocity2: float
18+
) -> float:
19+
"""Calculate final velocity after a perfectly inelastic collision.
20+
21+
The two objects stick together and share a common final velocity.
22+
23+
Parameters:
24+
mass1: Mass of the first object.
25+
mass2: Mass of the second object.
26+
velocity1: Initial velocity of the first object.
27+
velocity2: Initial velocity of the second object.
28+
29+
Returns:
30+
The final combined velocity of the two objects.
31+
32+
Examples:
33+
>>> inelastic_collisions(2.0, 3.0, 5.0, 6.0)
34+
5.6
35+
>>> inelastic_collisions(9.0, 8.1, -3.2, 3.1)
36+
-0.22
37+
"""
38+
initial_momentum = (mass1 * velocity1) + (mass2 * velocity2)
39+
total_mass = mass2 + mass1
40+
final_velocity = round((initial_momentum / total_mass), 2)
41+
42+
return final_velocity
43+
44+
45+
def elastic_collisions(
46+
mass1: float, mass2: float, velocity1: float, velocity2: float
47+
) -> str:
48+
"""Calculate final velocities after a perfectly elastic collision.
49+
50+
This assumes the collision is head-on and conserves both momentum and kinetic energy.
51+
52+
Parameters:
53+
mass1: Mass of the first object.
54+
mass2: Mass of the second object.
55+
velocity1: Initial velocity of the first object.
56+
velocity2: Initial velocity of the second object.
57+
58+
Returns:
59+
A formatted string containing the final velocities of both objects.
60+
61+
Examples:
62+
>>> elastic_collisions(1.0, 2.0, -3.0, -1.0)
63+
'-0.34 ; -2.34'
64+
>>> elastic_collisions(9.0, 8.1, -3.2, 3.1)
65+
'2.76 ; -3.54'
66+
"""
67+
com_velocity = inelastic_collisions(mass1, mass2, velocity1, velocity2)
68+
initial_velocities = [velocity1, velocity2]
69+
final_velocities = []
70+
71+
for vel in initial_velocities:
72+
new_vel = -1 * (vel - com_velocity)
73+
final_vel = com_velocity + new_vel
74+
final_velocities.append(round(final_vel, 2))
75+
76+
return f"{final_velocities[0]} ; {final_velocities[1]}"
77+
78+
79+
def type_collision(
80+
mass1: float,
81+
mass2: float,
82+
velocity_initial1: float,
83+
velocity_initial2: float,
84+
velocity_final1: float,
85+
velocity_final2: float,
86+
) -> str:
87+
"""Determine the collision type from initial and final velocities.
88+
89+
Compares initial and final momentum and kinetic energy to classify the collision.
90+
91+
Parameters:
92+
mass1: Mass of the first object.
93+
mass2: Mass of the second object.
94+
velocity_initial1: Initial velocity of the first object.
95+
velocity_initial2: Initial velocity of the second object.
96+
velocity_final1: Final velocity of the first object.
97+
velocity_final2: Final velocity of the second object.
98+
99+
Returns:
100+
A string describing the collision type.
101+
102+
Examples:
103+
>>> type_collision(1.0, 1.0, 2.0, 3.0, 2.0, 3.0)
104+
'Perfectly Elastic Collision'
105+
>>> type_collision(1.0, 1.0, 2.0, 3.0, 2.5, 2.5)
106+
'Perfectly Inelastic Collision'
107+
>>> type_collision(1.0, 1.0, 2.0, 3.0, 0.0, 0.0)
108+
'Inelastic Collision'
109+
"""
110+
momentum_initial = (mass1 * velocity_initial1) + (mass2 * velocity_initial2)
111+
momentum_final = (mass1 * velocity_final1) + (mass2 * velocity_final2)
112+
kinetic_initial = 0.5 * (
113+
(mass1 * velocity_initial1**2) + (mass2 * velocity_initial2**2)
114+
)
115+
kinetic_final = 0.5 * ((mass1 * velocity_final1**2) + (mass2 * velocity_final2**2))
116+
117+
if kinetic_final == kinetic_initial and momentum_initial == momentum_final:
118+
return f"Perfectly Elastic Collision"
119+
elif not (kinetic_final == kinetic_initial) and momentum_initial == momentum_final:
120+
return f"Perfectly Inelastic Collision"
121+
else:
122+
return f"Inelastic Collision"
123+
124+
125+
if __name__ == "__main__":
126+
import doctest
127+
128+
doctest.testmod()

0 commit comments

Comments
 (0)