0% found this document useful (0 votes)
15 views6 pages

Assignment 3

Uploaded by

genenaomar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

Assignment 3

Uploaded by

genenaomar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment 3

📝 Worth: 8%
📅 Due: November 28, @Midnight
🕑 Late submissions: 10% penalty per late day. Maximum of 3 late days allowed.
📥 Submission on Gradescope:
Include:

All .py files,

All your answers should by typed in .pdf format

All graphs in .png format

Objectives
Create and access lists

Create basic plots

Simulate a physics problem using turtle graphics

Modules
Ensure that you have installed (if necessary) the following modules:

matplotlib

A bouncing ball
Context:

In this exercise we will be dropping a bouncy ball and observe it bounce back up virtually. The goal
is to use a computer program to simulate the motion of the ball as it repeatedly hits the ground
and bounces back up repeatedly. In this simulation, we will use a timestamp (one frame at a time)
to calculate the position and velocity and storing them in list!
Physics concepts in relations to programming variable

t the time in seconds called time

Δt the time interval called delta_t

v(t) the velocity vector at each moment t , it is positive when the ball is moving up and
negative when the ball is moving down current_velocity

v(t - Δt) the velocity vector at the moment t- Δt will be called previous_velocity

Physics equations to implement:

For each given time period Δt you can obtain the current position and velocity using the previous
position and velocity:

v(t) = v(t - Δt) - g * Δt (Equation 1)

p(t) = p(t - Δt) - v(t) * Δt (Equation 2)

If the ball hits the ground, we will simulate the kinetic energy loss by multiplying the velocity by a
constant k:

v+(t) = -kv-(t) (Equation 3)

You can assume that k= 0.84, you can test out different values if you prefer later on.

In python this can be coded as such:

current_velocity = -k * current_velocity
2.1 Write a function position() (2pts)
Implement equation (1.2) provided with

Input parameters:

previous_position

current_position

delta_t

Returns:

current_position

2.2 Write a function has_hit_ground() : (2 pts)


This function will help detect a ground collisions. Since the simulation uses finite time steps, it
might "miss" the exact moment the ball hits the ground. We will start with a simple algorithm only
using the current_position p(t) and improve it later if needed.

Input parameters:

the position p(t)

Returns:

True if the ball hit the ground or False if it didn't

Hint: After completing 2.4, detecting when the ball touches the ground may required more
careful consideration and adding the velocity and time. This is part of the bonus.

2.3 Write a function velocity() (4 pts)


Implement equation (1.1), then if the ball hits the ground ( has_hit_ground() ) implement
equation (1.2)

Input parameters:

the previous_position ( p(t - Δt) ): (required to detect ground collision)

the previous_velocity ( v(t - Δt))

the delta_t the time interval ( Δt)

Returns:

the current_velocity of the ball v(t)


2.4 Write a function called simulate() (12 pts)
Input parameter:

start_velocity v(t=0)

start_position p(t=0)

total_duration which is the total duration of the simulation in seconds

Returns:

a list of lists [ time_list , velocity_list , position_list ]

To type hint that it's list[list[float]]

Initialization:

Initialize three lists: time_list , velocity_list and position_list , starting with the first
values provided as parameters and set t0=0:

time_list = [0]
velocity_list = [start_velocity]
position_list = [start_position]

Create the ball (ensure turtle is imported):

ball = turtle.Turtle()
ball.penup()
ball.shape('circle')

Use multiple samples per second for smoother simulation, starting with Δt = 0.1 (you can
improve it later to get a more realistic simulation)

Calculate the number of iterations needed:

n = total_duration / delta_t

Motion

Loop ( for _ in range(n): )

Compute the current time t from the previous value ti-1 (Hint: use time_list[-1] to get
the previous time:

Calculate the current_velocity using the function defined in 2.2 provided with the
previous_position and current_velocity

Calculate the current_position using the function defined in 2.1

Move the ball to the current_position using ball.sety()

Append the current time , current_velocity and current_position to time_list ,


velocity_list , position_list respectively.

Returns the lists


Testing

In the main() call simulate() :

start_velocity = 0

start_position = 220.0

total_duration = 90

If the doesn't behave correctly, check:

All the equations above

has_hit_ground() isn't detecting ground collisions. Print the position of the ball at
each iteration and use the debugger to help.

Once, you got the simulation working you can try to do the bonus to improve it.

2.5 Plotting the simulated data (5 pts)

Write a new function plot_motion()

Use time_list , the velocity_list to graph the velocity over time.

On a separate graph, use time_list and position_list to graph the position over time.
Add a horizontal line at y=0 using plt.axhline(0,color='red',linestyle='--')

Are you observing at which height is the ball bouncing back up?

Relate what you observe on this graph with the strategy hit collision algorithm and possibly
improve it (bonus - below). Put your answer in a submitted pdf.

Save graphs and submit them with your work.

2.6 Bonus (2 pts)

In this bonus question, you are tasked with improving the algorithm to make it appear more
realistic.

Modify the function has_hit_ground to include more parameters. It must still return a
boolean.

Hint: We know that the if the ball was able to go through the ground, it's position would be
negative in the next time frame.
Use break to stop the loop once the velocity stabilizes (doesn't significantly change)

Hint: you can use the abs() function or math.isclose()

You might also like