0
$\begingroup$

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

enter image description here

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.

$\endgroup$

1 Answer 1

2
$\begingroup$

Well, you "overwrite" the table at every loop... As a result when the for loop is terminated the table has only the t,X,Y columns for theta=75.

Try this:

columns = []

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)
    
    columns += [t,X,Y]
                          
np.savetxt("projectile.txt", np.column_stack(columns), fmt='%.3e', delimiter="  ")
$\endgroup$
6
  • $\begingroup$ Very nice! But instead of 9 columns this saves 12 columns, I think there must be an error; I could not figure out the reason why instead of 9 columns there exists 12 columns. could you help me please? $\endgroup$
    – Sha
    Feb 17, 2021 at 12:39
  • $\begingroup$ 3 columns(t,X,Y) * 4 theta cases(30,45,60,75) = 12. $\endgroup$ Feb 17, 2021 at 12:46
  • $\begingroup$ I understand now! and I can not thank you enough for your help. $\endgroup$
    – Sha
    Feb 17, 2021 at 13:01
  • $\begingroup$ If you want to have only one column for the time, maybe it would be a good idea to change your impementation, so that there will be the same t for all angles. Otherwise, the data that you will save will not make any sense... $\endgroup$ Feb 17, 2021 at 13:23
  • $\begingroup$ Yes that would be nice to have only one column for the time, but that is not possible in our case, because the time interval is itself a function of the angle. With each theta case the $tmax.$ changes, which in turn changes the value of the 600 time coordinates within the np.linspace time interval. $\endgroup$
    – Sha
    Feb 17, 2021 at 13:35

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.