simulate rocket launch in python

To simulate a rocket launch in python, we need to model the physical laws that govern the motion of the rocket. We can use numerical integration techniques to simulate the motion of the rocket over time. The basic steps involved in simulating a rocket launch are:

  1. Define the initial state of the rocket (position, velocity, acceleration, mass, etc.)

  2. Define the physical parameters of the rocket (thrust, drag, gravitational force, etc.)

  3. Use numerical integration techniques (such as Euler's method or Runge-Kutta methods) to calculate the rocket's motion over time.

  4. Plot the position, velocity, and acceleration of the rocket using a visualization library (such as matplotlib) to see the rocket's behavior.

Here's an example code snippet for simulating a rocket launch:

main.py
import matplotlib.pyplot as plt
import numpy as np

# Define initial state of rocket
x0 = 0      # initial x position
y0 = 0      # initial y position
vx0 = 0     # initial x velocity
vy0 = 0     # initial y velocity
m0 = 1000   # initial mass of rocket
t0 = 0      # initial time

# Define physical parameters of rocket
g = 9.81    # gravitational acceleration
rho = 1     # air density
Cd = 0.5    # drag coefficient
A = 10      # cross-sectional area of rocket
ve = 3600   # exhaust velocity
m_dot = 100 # mass flow rate
angle = np.pi/4   # launch angle

# Define time step and total simulation time
dt = 0.1    # time step
t_max = 100 # total simulation time

# Define function to calculate acceleration of rocket
def acceleration(x, y, vx, vy, m):
    # Calculate gravitational force
    Fg = -m*g
    
    # Calculate drag force
    v = np.sqrt(vx**2 + vy**2)
    Fd = -0.5*rho*Cd*A*v**2
    
    # Calculate thrust force
    if m > m0*0.1:
        Ft = ve*m_dot
    else:
        Ft = 0
    
    # Calculate total force and acceleration
    Fx = Ft*np.cos(angle) + Fd*np.cos(np.arctan2(vy, vx))  # x component of force
    Fy = Ft*np.sin(angle) + Fg + Fd*np.sin(np.arctan2(vy, vx))  # y component of force
    ax = Fx/m  # x component of acceleration
    ay = Fy/m  # y component of acceleration
    
    return ax, ay

# Initialize arrays to store rocket state over time
x = np.zeros(int(t_max/dt)+1)
y = np.zeros(int(t_max/dt)+1)
vx = np.zeros(int(t_max/dt)+1)
vy = np.zeros(int(t_max/dt)+1)
m = np.zeros(int(t_max/dt)+1)
t = np.zeros(int(t_max/dt)+1)

# Set initial state
x[0] = x0
y[0] = y0
vx[0] = vx0
vy[0] = vy0
m[0] = m0
t[0] = t0

# Time loop
for i in range(len(t)-1):
    # Calculate acceleration and update state
    ax, ay = acceleration(x[i], y[i], vx[i], vy[i], m[i])
    vx[i+1] = vx[i] + ax*dt
    vy[i+1] = vy[i] + ay*dt
    x[i+1] = x[i] + vx[i+1]*dt
    y[i+1] = y[i] + vy[i+1]*dt
    
    # Update mass and time
    m[i+1] = m[i] - m_dot*dt
    t[i+1] = t[i] + dt
    
    # If rocket hits ground, stop simulation
    if y[i+1] < 0:
        break

# Plot position, velocity, and acceleration of rocket
fig, axs = plt.subplots(3, 1, figsize=(8, 12))
axs[0].plot(t[:i+2], x[:i+2])
axs[0].set_xlabel('Time (s)')
axs[0].set_ylabel('X Position (m)')
axs[1].plot(t[:i+2], y[:i+2])
axs[1].set_xlabel('Time (s)')
axs[1].set_ylabel('Y Position (m)')
axs[2].plot(t[:i+2], np.sqrt(vx[:i+2]**2 + vy[:i+2]**2))
axs[2].set_xlabel('Time (s)')
axs[2].set_ylabel('Velocity (m/s)')
plt.show()
2502 chars
93 lines

gistlibby LogSnag