# 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("angry_bird", "week_3", "homework", assignment_points = 29.0, assignment_tag = 'week3-homework')
# Initialize Otter
import otter
grader = otter.Notebook("angry_bird.ipynb")
๐ Homework The Flight of the Angry Bird ๐๐ฆ#
Youโre helping an angry bird hit its target with a slingshot! The slingshot uses a spring with spring constant \(k\) (in \(\mathrm{N/m}\)) and launches a bird of mass \(x\) (in \(\mathrm{kg}\)). The target is 1 km away horizontally and 10 m high vertically.
To find the distance to pull the spring (\(d\)):
Assume the spring is linear and obeys Hookeโs Law: \(F = k d\), where \(F\) is the force applied.
The slingshot launches the bird with no air resistance and at an optimal launch angle \(\theta\) for the given conditions.
The acceleration due to gravity is \(g = 9.8 \, \mathrm{m/s^2}\).
Use the following physics principles:
Horizontal motion: \(x_{\text{target}} = v_x t\)
Vertical motion: \(y_{\text{target}} = v_y t - \frac{1}{2} g t^2\)
Initial velocities:
\(v_x = v_0 \cos(\theta)\)
\(v_y = v_0 \sin(\theta)\)
The total velocity \(v_0\) is related to the spring compression distance \(d\) by energy conservation: $\( \frac{1}{2} k d^2 = \frac{1}{2} m v_0^2 \)$
Find \(d\), the distance the spring must be pulled back, symbolically.
Bird mass: 1 , \mathrm{kg}$,
Spring constant: 500000 , \mathrm{N/m}$,
Target distance: \(1 \, \mathrm{km}\),
Target height: \(5 \, \mathrm{m}\),
\(\theta = \frac{\pi}{4}\), 45 degrees in radians.
Python Implementation#
Import SymPy using the alias
sp
.Define the variables as SymPy symbols:
m
,k
,d
,theta
,g
,v_0
,t
,xtarget
, andytarget
. Use thesp.symbols()
function to define multiple symbols at once.Define the values for gravity
g_value
, xtargetxtarget_value
, ytargetytarget_value
, bird massm_value
, spring constantk_value
, and angletheta_value
. Ensure that all values are in SI units, and the angletheta_value
is in radians. Make sure to use thesp.pi
constant for \(\pi\).Define the equation for conservation of energy: \(\frac{1}{2} k d^2 = \frac{1}{2} m v_0^2\). Use the
sp.Eq()
function to define the equation and save it in a variable calledenergy_eq
.Solve the equation for \(v_0\) using the
sp.solve()
function. Save the result in a variable calledv0_expr
.
Note: The solution will return a list of potential solutions. Extract the positive solution, which is the valid initial velocity, by indexing the first element of the list. Recall that the 1st element of a list in Python has an index of 0.
Define the equations for horizontal and vertical motion using the expressions for velocity. Use
sp.cos()
andsp.sin()
functions for the trigonometric functions, and the variable for velocityv0_expr
. Save the horizontal and vertical motion equations in variables calledvx_eq
andvy_eq
.
Note: You do not need to use the sp.Eq() function for these equations, as you already have the expressions in terms of the one unknown variable vx_eq
and vy_eq
.
Define the equations for the instantaneous position of the bird in the x and y directions as functions of time
t
. Use the provided equations for motion. Save the equations in variables calledxpos_eq
andypos_eq
.Set up the equation for the time when the bird reaches the target x-position
xpos_eq
toxtarget
. Save the equation in a variable calledtargetx_eq
. Use thesp.Eq()
function to set up the equation, since we do not have an explicit expression for timet
.Solve the equation
targetx_eq
for timet
using thesp.solve()
function. Save the result in a variable calledt_eq
. Select the valid solution fort
(e.g., the positive value). This is index 0 in the list of solutions.Define the equation for the vertical position of the bird at time
t
. Substitute the expressiont_eq
into the vertical motion equationypos_eq
. Save the resulting expression in a variable calledy_eq
.
Hint: your answer should be exactly the same as the provided equation for vertical motion in part 7. You just need to replace t
with the solution t_eq
.
Set up the equation for the target y-position by equating
y_eq
toytarget
using thesp.Eq()
function. Save the equation in a variable calledytarget_eq
.Solve the equation
ytarget_eq
for the pull distanced
using thesp.solve()
function. Save the result in a variable calledd_expr
.The solution
d_expr
will return a list of potential solutions. Extract the positive solution, which is the valid pull distance, by indexing the second element of the list. Save it in a variable calledd_solution
.Construct a dictionary called
subs_dict
containing the variable names as keys and their corresponding values as values. Use the values defined earlier for the dictionary.
Note: SymPy symbols are valid keys for the dictionary. SymPy will expect that the key is a symbol, and it will substitute the value accordingly. Do not use the string representation of the variable names as the keys.
Substitute the values into the
d_solution
expression using thesubs()
function, passing the dictionarysubs_dict
as an argument. Evaluate the result numerically using theevalf()
function. Save the result in a variable calledfinal_result
. You should also print the final result for the pull distanced
.
For example:
final_result = d_solution.subs(subs_dict).evalf()
Now you are an Angry Bird physics expert! ๐๐ฆ
# 1. import sympy using the alias sp
...
# 2. Define variables as described in the problem statement
...
# 3. Define the values of the variables as described in the problem statement
...
# 4. Energy conservation: Spring energy = Kinetic energy
...
# 5. Solve for v0
...
# 6. Horizontal and vertical motion equations
...
# 7. Define the equations for the x and y position of the bird at time t
...
# 8. Set up the equation for the time the bird reaches the x-target distance, assign this to the variable targetx_eq
...
# 9. Solve for t to reach a given x - position
...
# 10. Solve for the y-position at time t
...
# 11. Define the equation for target y-position in terms of the target height.
...
# 12. solve ytarget_eq for d
...
# 13. Get the positive solution for d
...
# 14. Construct the dictionary of the values to substitute into the solution for d
...
# 15. Substitute the values of the variables into the solution for d
...
t_eq_final = t_eq.subs(d, final_result).subs(subs_dict).evalf()
# Evaluate vx and vy numerically
vx_num_expr_final = vx_eq.subs(d, final_result).subs(subs_dict).evalf()
vy_num_expr_final = vy_eq.subs(d, final_result).subs(subs_dict).evalf()
# Step 13: Generate time and trajectory data
t_vals = np.linspace(0, float(t_eq_final), 500)
x_vals = [float(vx_num_expr_final * t) for t in t_vals]
y_vals = [float(vy_num_expr_final * t - 0.5 * g_value * t**2) for t in t_vals]
# Step 14: Plot the trajectory
plt.figure(figsize=(10, 6))
plt.plot(x_vals, y_vals, label="Trajectory of the bird")
plt.scatter([xtarget_value], [ytarget_value], color="red", label="Target Position")
plt.title("Bird's Trajectory")
plt.xlabel("Horizontal Distance (m)")
plt.ylabel("Vertical Distance (m)")
plt.axhline(0, color="black", linewidth=0.5, linestyle="--")
plt.legend()
plt.grid()
plt.show()
grader.check("Angry-birds-sympy-trajectory")
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-homework", "angry_bird")