# Demonstrating Newton’s laws of motion using Python code

Newton’s laws of motion are a set of three laws that describe the relationship between a body and the forces acting upon it. These laws were developed by Sir Isaac Newton in the 17th century and are still widely used in classical mechanics to describe the motion of objects.

Newton’s laws of motion provide a fundamental framework for understanding the motion of objects and the forces that act upon them.

Here is an example of how you could demonstrate Newton’s laws of motion using Python code:

## Newton’s First law of motion with Python code

An object at rest tends to stay at rest, and an object in motion tends to stay in motion with the same speed and in the same direction, unless acted upon by a force.

```# define a class to represent an object
class Object:
def __init__(self, mass, velocity=0):
self.mass = mass
self.velocity = velocity

def apply_force(self, force, time):
# calculate the acceleration of the object
acceleration = force / self.mass
# update the velocity of the object based on the acceleration and time elapsed
self.velocity += acceleration * time

# create an object with a mass of 10 kg and initial velocity of 0 m/s
obj = Object(10)
print("Initial velocity:", obj.velocity) # should print 0

# apply a force of 50 N for 1 second
obj.apply_force(50, 1)
print("Velocity after applying force:", obj.velocity) # should print 5 (50 N / 10 kg = 5 m/s^2)

# apply no force for 1 second
obj.apply_force(0, 1)
print("Velocity after applying no force:", obj.velocity) # should still print 5
```

## Newton’s Second law of motion with Python code

Newton’s second law of motion states that the force acting on an object is equal to the mass of the object times its acceleration. In equation form, this is represented as:

```F = ma
```

Where `F` is the force acting on the object, `m` is the mass of the object, and `a` is the acceleration of the object.

Python code that calculates the mass and acceleration of an object based on the given variables:

```# Define the initial and final velocities of the object
initial_velocity = 10  # m/s
final_velocity = 20  # m/s

# Define the time elapsed
time_elapsed = 5  # s

# Calculate the change in velocity
delta_v = final_velocity - initial_velocity

# Calculate the acceleration of the object
acceleration = delta_v / time_elapsed

# Print the calculated acceleration
print("Acceleration:", acceleration, "m/s^2")

# Define the force acting on the object
force = 50  # N

# Calculate the mass of the object
mass = force / acceleration

# Print the calculated mass
print("Mass:", mass, "kg")
```

This code will output the following values:

• Acceleration: 4 m/s^2
• Mass: 12.5 kg

The variables `initial_velocity`, `final_velocity`, and `time_elapsed` can be changed to different values to calculate the acceleration and mass for different scenarios.

Now the below Python code demonstrates this principle by calculating the force required to accelerate a 1 kilogram object at a rate of 1 meter per second squared:

```# Define the mass of the object in kilograms
mass = 1

# Define the acceleration of the object in meters per second squared
acceleration = 1

# Calculate the force required to accelerate the object
force = mass * acceleration

# Print the calculated force
print(force)
```

This code will output the result `1`, which represents the force required to accelerate a 1 kilogram object at a rate of 1 meter per second squared.

## Newton’s Third law of motion with Python code

For every action, there is an equal and opposite reaction.

```import matplotlib.pyplot as plt
import numpy as np

# Constants
GRAVITY = 9.81  # m/s^2
MASS = 1.0      # kg

# Initial conditions
y_0 = 0         # m
v_0 = 10        # m/s
t_0 = 0         # s
dt = 0.01       # s

# Create lists to store the position and velocity of the ball at each time step
y_positions = [y_0]
v_velocities = [v_0]
times = [t_0]

# Loop through time steps and calculate the position and velocity of the ball at each step
while y_positions[-1] >= 0:
# Calculate the acceleration of the ball due to gravity
acceleration = -GRAVITY

# Calculate the velocity at the current time step
v_current = v_velocities[-1] + acceleration * dt

# Calculate the position at the current time step
y_current = y_positions[-1] + v_current * dt

# Append the current position, velocity, and time to the lists
y_positions.append(y_current)
v_velocities.append(v_current)
times.append(times[-1] + dt)

# Plot the position of the ball over time
plt.plot(times, y_positions)
plt.xlabel('Time (s)')
plt.ylabel('Position (m)')
plt.show()
```

This code simulates the motion of a ball being dropped from a height of `y_0` meters with an initial velocity of `v_0` meters per second. The ball is subjected to the force of gravity, which is represented by the acceleration `GRAVITY` meters per second squared. The code uses a while loop to step through the simulation, calculating the position and velocity of the ball at each time step using the equations of motion:

```acceleration = force / mass
velocity = velocity + acceleration * dt
position = position + velocity * dt
```

At each time step, the ball experiences a force equal to its mass times the acceleration due to gravity `(F = ma)`. This force is the action, and the reaction is the equal and opposite force that the ground exerts on the ball. The code plots the position of the ball over time, showing how it bounces back up after hitting the ground due to the equal and opposite reaction force.

## Would you like to connect & have a talk?

My daily life involves interacting with different people in order to understand their perspectives on Climate Change, Technology, and Digital Transformation.

If you have a thought to share, then let’s connect!