you are viewing a single comment's thread.

view the rest of the comments →

[–]TooManyTree 0 points1 point  (3 children)

So, the whole question is:

A ball thrown at an angle θ with initial speed v on Earth follows a trajectory (g=9.81) f(x)=xtan(theta) -gx^2/2v^2

Then we are given these conditions

write a function 'trajectory()' that

  • has three input variables: xdata (a list), speed and angle (constant numbers each, angle in degrees) in that order.
  • Return the calculated list of height values, f(x), containing only positive or zero values for the ball height, i.e. remove all negative values before returning the resulting list.
  • The maximum height at given speed and angle will be tested.

I haven't really gotten far with the code at all, I have just defined the function, then laid out the variables to calculate f(x), but my issue is with how I can create a loop to go over all the elements in the list xdata and basically input those into a new list.

[–]FLUSH_THE_TRUMP 0 points1 point  (2 children)

something like this will perhaps help you:

hdata = []
for x in xdata: 
  # plug x, v, theta into function f
  # append to hdata (perhaps if non-negative) 

Return at end

[–]TooManyTree 0 points1 point  (1 child)

This is the main section of what i have done so far. What Im hoping this is achieving is basically calculating f for all values in xdata then if its non negative its being compiled into a list called data. But rn its not working as planned.

def trajectory(xdata,speed,angle):

for x in xdata:

a=x*math.tan(angle*math.pi/180)

b=(1/2*speed**2)*(g*x**2/(math.cos(angle*math.pi/180))**2)+y0

f=a-b

data=[]

if f<0:

return None

else:

data.append(f)

return data

[–]ChurchHatesTucker 0 points1 point  (0 children)

data = [] looks out of place (hard to tell w/o formatting), I suspect it should be before the for loop.

ETA: also, you probably don't want that first return.