我怎样才能用scipy图书馆获得距离?

2024-09-28 03:22:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始学习python,老师让我模拟模型火箭的轨迹,知道发动机的推力

我已经用odeint函数得到了火箭的速度和加速度。但是,我不知道如何使用速度和时间

我已经得到了火箭的飞行距离,因为速度是用odeint函数求出来的

下面是我用来计算速度的代码:

def getforce(t):
    if 0<=t<0.15:
        F = 40*t
    elif 0.15<=t<0.7:
        F = -9.09*t+7.36
    elif 0.7<=t<1.25:
        F = 1
    elif 1.25<=t<1.65:
        F = 7.5*t-8.375
    elif 1.65<=t<1.8:        
        F = -26.6*t+48
    else:
        F = 0
    return F

def getspeed(x,t):
    Ft = getforce(t)
    y0,y1 = x
    dy0 = y1
    dy1 = (Ft-0.0001277422*y1**2*np.sign(y1)-0.174)/0.0177
    return dy0,dy1

t = np.linspace(0,10,100)
sol = si.odeint(getspeed,(0,0),t)
plt.plot(t,sol[:,0])
plt.show()

Tags: 函数returndefnp速度ftelif火箭
1条回答
网友
1楼 · 发布于 2024-09-28 03:22:25

假设其他一切都是正确的,您只需手动积分速度

(我无法轻松检查整体正确性的原因是,您在速度部分使用了一个非正统(给定)表达式,而不是求解包含质量损失F=ma>;d(m*v)/dt=dm/dt*v+m*dv/dt.)

import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate as si
%matplotlib inline

def getforce(t):
    if 0<=t<0.15:
        F = 40*t
    elif 0.15<=t<0.7:
        F = -9.09*t+7.36
    elif 0.7<=t<1.25:
        F = 1
    elif 1.25<=t<1.65:
        F = 7.5*t-8.375
    elif 1.65<=t<1.8:        
        F = -26.6*t+48
    else:
        F = 0
    return F

def getspeed(x,t):
    Ft = getforce(t)
    y0,y1 = x
    dy0 = y1
    dy1 = (Ft-0.0001277422*y1**2*np.sign(y1)-0.174)/0.0177
    return dy0,dy1

t = np.linspace(0,10,100)
sol = si.odeint(getspeed,(0,0),t)
v=sol[:,0]

x=0
xs=[]
dt=t[1]-t[0] # use linspace with 101 to get the sample distance you'd normally expect
for i in range(len(v)):
    x=x+v[i]*dt
    xs.append(x)
plt.subplot(121)
plt.plot(t,v)
plt.subplot(122)
plt.plot(t,xs)
plt.show()

enter image description here

我没有使用numpy或lambda表达式来集成以保持它的可读性,因为执行速度对于本例来说无关紧要

相关问题 更多 >

    热门问题