I have managed to write the following code for the following problem:
Projectile's horizontal and vertical displacement are given by:
$$ x = v_0 \, t \cos(\theta) $$
$$ y = v_0 \, t \sin(\theta) - \frac{1}{2}gt^2$$
- Consider the time interval to be $t \in [0, 60] seconds$. Write a Python code to plot horizontal and maximum vertical displacements (meaning $y_{max.}$ as a function of $x$) with the initial velocity $40 m/sec$ for four angles (30, 45, 60, and 75 degrees ) on the same graph. Then save your data in a text file.
My code is:
import numpy as np
import matplotlib.pyplot as plt
v = 40
g = 9.8
def x(theta):
return ((v*t)*np.cos(theta*np.pi/180))
def y(theta):
return ((v*t)*np.sin(theta*np.pi/180))-((0.5*g)*(t**2))
for theta in range(30, 90, 15):
tmax = v * np.sin(theta*np.pi/180)/g #Time to reach maximum height
t = np.linspace(0, tmax,600)
X = x(theta) #Maximum horizontal displacement at tmax
Y = y(theta) #Maximum vertical displacement at tmax
plt.plot(X, Y, label=theta)
table=np.column_stack((t,X, Y))
np.savetxt("projectile.txt", table, fmt='%.3e', delimiter=" ") # just saves 3 columns :(
plt.xlabel("x")
plt.ylabel("y")
plt.title("Maximum Height as a Function of Angle")
plt.legend()
plt.show()
I get the plot successfully; however, the data saved consistes of 3 columns only, whereas it should be 9 columns.
How to save all the data please? Your help is much appreciated.