I'm working on learning python more in depth than from my previous questions now that my internship is over and I hit an issue
I'm using a book "Doing Math With Python" by Amit Saha which what I decided to jump to was 'Animating a Projectile's Trajectory. I spent an hour trying to figure it out on my own then another 2 days on the internet and I still can't understand why I'm getting the error I'm getting
AttributeError: 'float' object has no attribute 'append'
if I don't have the float in the code then it doesn't work at all and I get this
TypeError: a float is required
I want to get this project done hopefully before we leave the projectile motion unit in my high school physics just as a cool thing I learned to do. please help. I can get it to draw the trajectory, just not animate it :(
from matplotlib import pyplot as plt
from matplotlib import animation
import math
g = 9.8
def get_intervals(u, theta):
t_flight = 2*u*math.sin(theta)/g
intervals = []
start = 0
intervals = 0.005
while start < t_flight:
intervals.append(start)
start = start + interval
return intervals
def update_position(i, circle, intervals, u, theta):
t = intervals[i]
x = u*math.cos(theta)*t
y = u*math.sin(theta)*t - 0.5*g*t*t
circle.center = x, y
return circle,
def create_animation(u, theta):
intervals = get_intervals(u,theta)
xmin = 0
xmax = u*math.cos(theta)*intervals[-1]
ymin = 0
t_max = u*math.sin(theta)/g
ymax = u*math.sin(theta)*t_max - 0.5*g*t_max**2
fig = plt.gcf()
ax = plt.axes(xlim=(xmin, xmax), ylim=(ymin, ymax))
circle = plt.Circle((xmin, ymin), 1.0)
ax.add_patch(circle)
anim = animation.FuncAnimation(fig, update_position,
fargs=(circle, intervals, u, theta),
frames=len(intervals), interval=1,
repeat=False)
plt.title('Projectile Motion')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
if __name__ == '__main__':
try:
u = float(input('Enter the initial velocity (m/s): '))
theta = float(input('Enter the angle of projection (degrees): '))
except ValueError:
print('You Entered an invalid input')
else:
theta = (math.radians(theta))
create_animation(u, theta)
get_intervalsyou write:intervals=0.005thenintervals.append(.... No wonder this does not work. Just remove thesinintervals=0.005.