๐Ÿ  Homework The Flight of the Angry Bird ๐Ÿš€๐Ÿฆ

# 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\)):

  1. Assume the spring is linear and obeys Hookeโ€™s Law: \(F = k d\), where \(F\) is the force applied.

  2. The slingshot launches the bird with no air resistance and at an optimal launch angle \(\theta\) for the given conditions.

  3. 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#

  1. Import SymPy using the alias sp.

  2. Define the variables as SymPy symbols: m, k, d, theta, g, v_0, t, xtarget, and ytarget. Use the sp.symbols() function to define multiple symbols at once.

  3. Define the values for gravity g_value, xtarget xtarget_value, ytarget ytarget_value, bird mass m_value, spring constant k_value, and angle theta_value. Ensure that all values are in SI units, and the angle theta_value is in radians. Make sure to use the sp.pi constant for \(\pi\).

  4. 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 called energy_eq.

  5. Solve the equation for \(v_0\) using the sp.solve() function. Save the result in a variable called v0_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.

  1. Define the equations for horizontal and vertical motion using the expressions for velocity. Use sp.cos() and sp.sin() functions for the trigonometric functions, and the variable for velocity v0_expr. Save the horizontal and vertical motion equations in variables called vx_eq and vy_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.

  1. 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 called xpos_eq and ypos_eq.

  2. Set up the equation for the time when the bird reaches the target x-position xpos_eq to xtarget. Save the equation in a variable called targetx_eq. Use the sp.Eq() function to set up the equation, since we do not have an explicit expression for time t.

  3. Solve the equation targetx_eq for time t using the sp.solve() function. Save the result in a variable called t_eq. Select the valid solution for t (e.g., the positive value). This is index 0 in the list of solutions.

  4. Define the equation for the vertical position of the bird at time t. Substitute the expression t_eq into the vertical motion equation ypos_eq. Save the resulting expression in a variable called y_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.

  1. Set up the equation for the target y-position by equating y_eq to ytarget using the sp.Eq() function. Save the equation in a variable called ytarget_eq.

  2. Solve the equation ytarget_eq for the pull distance d using the sp.solve() function. Save the result in a variable called d_expr.

  3. 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 called d_solution.

  4. 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.

  1. Substitute the values into the d_solution expression using the subs() function, passing the dictionary subs_dict as an argument. Evaluate the result numerically using the evalf() function. Save the result in a variable called final_result. You should also print the final result for the pull distance d.

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")