Assignment If you drop an anvil from a great height, it accelerates downward...
ID: 3888013 • Letter: A
Question
Assignment
If you drop an anvil from a great height, it accelerates downward... Write a program containing two functions: one that returns v(t) and another that returns y(t) for the anvil. Your program should plot both these functions on one graph, for time ranging from 0 to 5 seconds. Have the program label the axes appropriately and put ANVIL in the title.
Hint
Actually, everything you need code-wise is on this page, except the physics. Don't forget to import matplotlib.pyplot and numpy! Here's a sample program that puts together much of this example code.
Explanation / Answer
import numpy as np
import matplotlib.pyplot as plt
def time_to_hit_ground(y0, v0, a):
discriminant = np.sqrt(v0**2 - 2*a*y0)
t1 = (-v0 - discriminant) / a
t2 = (-v0 + discriminant) / a
if t1 >=0:
return t1
return t2
def drop(y0, v0=0.0, dt=0.1, g=-9.8):
if y0 < 0:
print('Object is underground.')
return
# if you also allow the user to change `dt` and `g` from the arguments,
t_ground = time_to_hit_ground(y0, v0, g)
t = np.arange(0, t_ground+dt, dt)
v = v0 + g * t
y = y0 + v0 * t + g * t**2 / 2.
plt.plot(t, y, '.')
plt.axvline(t_ground, color='g')
plt.axhline(0, color='g')
plt.xlabel('Time (s)')
plt.ylabel('Height (m)')
plt.show()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.