我有一个函数,旨在模拟飞盘的轨迹,并基本考虑升力和阻力。飞盘上的力是升力、阻力和重力。它具有预定的发射角度“theta_0”(确定速度角度的角度)和描述飞盘相对于水平“β”的角度的角度。然后,它使用 for 循环来计算每个时间步的位置、加速度和速度。它将这些内容编译到它们自己的单独列表中。如果飞盘到达 gorund (y[-1]<0), it stops the simulation. At the end, I want to return the lists for x and y position, simply titled x and y respectively. However, this does not seem to be working. I have omitted some variables which are defined earlier on in my code to keep the post a bit lighter. Every variable seen in the function should be accounted for.
def trajectory(x_0,y_0,theta_0,v_0, r):
vx_0 = v_0*(math.cos(theta_0))
vy_0 = v_0*(math.sin(theta_0))
A = (math.pi*r**2)
ax_0 = 0
ay_0 = -1*g
t = [0]
x = [x_0]
y = [y_0]
vx = [vx_0]
vy = [vy_0]
ax = [ax_0]
ay = [ay_0]
for i in range(STEPS):
theta = math.atan(vy[i]/vx[i])
alpha = beta_0 - theta
CD = (0.085 + 3.30*(alpha - (-0.052))**2)
CL = (0.13 + 3.09*alpha)
t.append(t[i]+DT)
x.append(x[i]+vx[i]*DT)
y.append(y[i]+vy[i]*DT)
vx.append(vx[i]+ax[i]*DT)
vy.append(vy[i]+ay[i]*DT)
drag = float(((-1/2)*RHO*A*CD*alpha*vx[i]**2))
drag_x = float((drag*math.cos(theta)))
drag_y = float((drag*math.sin(theta)))
lift = float(((-1/2)*RHO*A*CL*alpha*vx[i]**2))
lift_x = float((lift*math.cos(theta)))
lift_y = float((lift*math.sin(theta)))
ax.append((1/m)*(-1*lift_x - drag_x))
ay.append((1/m)*(-m*g + lift_y - drag_y))
if y[-1]<0:
del(y[-1])
del(x[-1])
del(vx[-1])
del(vy[-1])
del(ax[-1])
del(ay[-1])
break
return x,y
trajectory(x_0,y_0,theta_0,v_0,r)
print (x)
上面的代码在尝试打印时返回错误“'x'未定义”。如果我将“x”和“y”的打印语句放在函数内,它们就会打印,但我想绘制结果,所以我需要函数为我返回列表。我知道代码还有其他问题,但我现在并不担心这些问题,我只是想知道是什么导致了这个错误。谢谢!
您没有为函数调用分配任何内容。
尝试:
x,y = trajectory(x_0,y_0,theta_0,v_0,r)
print (x)