0

I am attempting to make an animated plot of a theoretical Falcon 9 disaster trajectory due to an engine failure. However, my code, shown below:

import numpy as np
from scipy.integrate import solve_ivp

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

def rocket(t, state):
   # state = [x, y, phi, vx, vy, dphi/dt]
   # dstate = [vx, vy, dphi/dt, ax, ay, ddphi/dt2]

   dstate = np.zeros(6)
   dstate[0:3] = state[3:]
      
   if t > 60:
      dstate[3] = 898.94 * 1000 * 7 * np.cos(state[2]) / 550000
      dstate[4] = (898.94 * 1000 * 7 * np.sin(state[2]) / 550000) - 9.81
      dstate[5] = - 0.01109404141387943 
      # dstate[5] = (2 * 1.5 *  np.cos(np.pi / 8) * 898.94 * 1000) / ((550000 * 70 ** 2) / 12)
   else:
      dstate[3] = 898.94 * 1000 * 9 * np.cos(state[2]) / 550000
      dstate[4] = (898.94 * 1000 * 9 * np.sin(state[2]) / 550000) - 9.81
      dstate[5] = 0

   return dstate

sol = solve_ivp(rocket, [0, 150], [0, 0, np.pi / 2, 0, 0, 0], t_eval = np.linspace(0, 150, 151))
plt.plot(sol.y[0] / 1000, sol.y[1] / 1000)

'''
print(len(sol.y[0]))
print()
print(len(np.linspace(0, 150, 151)))
'''

fig, ax = plt.subplots()
x_pos, y_pos = [], []
ln, = ax.plot([], [])

def init():
    ax.set_xlim(-5, 10)
    ax.set_ylim(0, 25)

    ax.set_xlabel("x Position (km)")
    ax.set_ylabel("y Position (km)")
    ax.set_title("Falcon 9 Asymmetric Engine Failure Trajectory")

    return ln,

def update(frame):
    x_pos.append(sol.y[0][frame])
    y_pos.append(sol.y[1][frame])
    ln.set_data(x_pos, y_pos)
    return ln,

ani = FuncAnimation(fig, update, frames = np.linspace(0, 150, 151), init_func = init, blit = True)

plt.show()

produces the following terminal error:

jacobivanov@Jacob-Ivanovs-MacBook-Air Lab 6 % /opt/homebrew/bin/python3 "/Users/jacobivanov/Desktop
/Falcon 9 Disaster Trajectory.py"
Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.10/site-packages/matplotlib/backend_bases.py", line 1193, in _on_timer
    ret = func(*args, **kwargs)
  File "/opt/homebrew/lib/python3.10/site-packages/matplotlib/animation.py", line 1404, in _step
    still_going = super()._step(*args)
  File "/opt/homebrew/lib/python3.10/site-packages/matplotlib/animation.py", line 1097, in _step
    self._draw_next_frame(framedata, self._blit)
  File "/opt/homebrew/lib/python3.10/site-packages/matplotlib/animation.py", line 1116, in _draw_next_frame
    self._draw_frame(framedata)
  File "/opt/homebrew/lib/python3.10/site-packages/matplotlib/animation.py", line 1743, in _draw_frame
    self._drawn_artists = self._func(framedata, *self._args)
  File "/Users/jacobivanov/Desktop/Falcon 9 Disaster Trajectory.py", line 50, in update

It's noteworthy to say that this block was repeated a likely 151 times in the terminal. It seems that the issue lies in how we can reference a list as part of our update process, because the following substitution results in the same bug:

a = np.linspace(0, 150, 151)
b = np.linspace(0, 150, 151)
def update(frame):
   '''
   x_pos.append(sol.y[0][frame])
   y_pos.append(sol.y[1][frame])
   '''
   x_pos.append(a[frame])
   y_pos.append(b[frame])

   ln.set_data(x_pos, y_pos)
   return ln,

ani = FuncAnimation(fig, update, frames = np.linspace(0, 150, 151), init_func = init, blit = True)

plt.show()

The lengths of frames and the solved positions are of identical index length, so there is no mismatch there. Otherwise, I'm not sure where my error is.

1 Answer 1

0

When you set the evaluated "frames = np.linspace(0, 150, 151)", it sets up an array [0.0, 1.0, 2.0..., 150.0]. An index can only be an integer, not a float, and as a result, it throws an error when you want to do "a[frame]" i.e. a[1.0]. Just add an int() function within the index.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.