# You must make sure to run all cells in sequence using shift + enter or you might encounter errors
from pykubegrader.initialize import initialize_assignment
responses = initialize_assignment("9_dodgeball_sympy_q", "week_3", "lecture", assignment_points = 5.0, assignment_tag = 'week3-lecture')
# Initialize Otter
import otter
grader = otter.Notebook("9_dodgeball_sympy_q.ipynb")
โ Dodgeball: Simple Symbolic Projectile Motion#
Instructions:#
In the dodgeball arena, a player throws the ball with an initial velocity. Your task is to:
Use SymPy to solve the height equation \(h(t) = v_0t - \frac{1}{2}gt^2\) for the time when the ball hits the ground.
Assume \(v_0 = 15\) m/s and \(g = 9.8\) m/sยฒ.
Your solution must include:
A function solve_projectile_time()
that (we have provided this function for you):
Define the symbols
t
,v0
, andg
.Define the height equation
h
symbolically.Solve the height equation for time when height is zero. To do this, use the
solve()
function, which takes two arguments: the equation to solve and the variable to solve for, in this caset
.Build the dictionaries with the values to substitute. We have provided the code for you to filter positive times - it would not make sense to have a negative time.
Compute the time by substituting the values of
v0
andg
into the equation. We did this using thesubs()
function, which takes a dictionary of symbols and values to substitute. We didnโt want you to do this step because it requires a loop which you have not learned yet.
from sympy import symbols, solve, N # Import N for numeric evaluation
def solve_projectile_time():
# 1. Define symbols
...
# 2. Height equation
...
# 3. Solve for time when h = 0
...
# 4. Define the values dictionaries
...
# Substitute numerical values for v0 and g to filter positive times
substituted_solutions = [sol.subs(values_dictionary) for sol in time_solutions]
# Filter positive numerical solutions
positive_time = [sol for sol in substituted_solutions if N(sol) > 0][0]
# Return the positive time as a numeric value
return N(positive_time)
# Example usage
result = solve_projectile_time()
print(f"Time when the ball hits the ground: {result:.2f} seconds")
grader.check("Dodgeball-w-sympy")
Submitting Assignment#
Please run the following block of code using shift + enter
to submit your assignment, you should see your score.
from pykubegrader.submit.submit_assignment import submit_assignment
submit_assignment("week3-lecture", "9_dodgeball_sympy_q")