我已经在给定初始条件的情况下绘制了弹丸运动。
我坚持尝试使tmax取决于初始条件,因此,如果您更改初始条件,则tmax会更改为合理的值。其次,我如何找到撞击的距离,飞行时间和撞击速度。这些都将取决于y何时变为零,但取决于IDK如何使用该信息。这是我的代码。谢谢。
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
c=0.65 #Constant friction term
m=0.1 #Object's mass
g=9.81 #Gravitational acceleration
theta=50 #Angle
V0=10 #Initial Velocity
Vx0=np.cos(np.deg2rad(theta))*V0 #Calculates initial speed along the x-axis
Vy0=np.sin(np.deg2rad(theta))*V0 #Calculates initial speed along the y-axis
t0 = 0.0 #Initial time
tmax=1 #Final time
steps=20 #Number of time step
tAF = np.linspace(t0, tmax, steps)# Creates a 1-D array of time values
y0AF = [0.0, Vx0, 0.0, Vy0] #Initial condition for x-position, V along x, y-position, V along y
def derivative(yAF,tF): #Function which calculates the derivatives
Vx = yAF[1] #Element in array for the velocity along x axis
Vy = yAF[3] #Element in array for the velocity along y axis
return [Vx, -c*Vx/m, Vy, -g-c*Vy/m] #Function outputs [dx/dt, dVx/dt, dy/dt, dVy/dt]
yM = odeint(derivative, y0AF, tAF) #Array with the solution for the differential equation
plt.plot(yM[:,0],yM[:,2], '.') #Plots y over x
plt.xlabel('x-distance'), plt.ylabel('y-distance')
plt.show()
MAXy=np.amax(yM[:,2])
print('Maximum height reached is',MAXy)
[而不是试图一次使用odeint()
来积分所有微分方程,您可以使用多个步骤,更改初始条件以继续积分,例如通过替换该行:
yM = odeint(derivative, y0AF, tAF)
...类似:
y = np.array([y0AF])
tLargeStep = 0.3
i = 0
while 1:
tAF = np.linspace(i*tLargeStep, (i+1)*tLargeStep, steps)
yM = odeint(derivative, y[-1,:], tAF)
y = np.concatenate((y,yM[1:,:]))
if y[-1,2] < 0:
break
i += 1
plt.plot(y[:,0],y[:,2], '.') #Plots y over x
plt.xlabel('x-distance'), plt.ylabel('y-distance')
plt.show()
在这种情况下,积分将逐步逐步进行,直到大步长结束时y
小于零为止。
获得了y
中的数据之后,您可以在这些数据上使用numpy.interpolate()
来查找撞击距离,飞行时间和撞击速度。
或者,如果您使用scipy.integrate.solve_ivp()
而不是odeint()
,则solve_ivp()
允许您指定要跟踪的事件,并使某些事件终止,即,使求解器在发生指定事件时停止计算。] >