输入常量变量的积分和绘图函数



我正在做一个python项目,需要计算摩托艇的速度及其位置。
速度的公式为v=v0*e^-bt/m,位置为p=(mv0/b)(1-e^-bt/m)其中b为14,m(质量)和v0(初始速度)需要由用户输入。在所有这些之后,我还需要绘制函数。

程序要求为:

  1. 请求用户输入摩托艇的初始速度0和质量
  2. 沿横截面设置足够的离散/网格点域,t=[0,21]。(最终值指定为21,因为我们希望20也包括在内)
  3. 基于方程(1)计算船的速度,基于方程(2)计算船位置
  4. 将船的速度和位置绘制在同一轴上
  5. 在图形中设置合适的图例、标题、轴标签和线条颜色
  6. 输出速度和位置的值

到目前为止,我只编写了以下代码:

%matplotlib inline
import numpy as np
from scipy import integrate
from scipy.integrate import quad
import matplotlib.pyplot as plt
def f(t):
return v0*math.exp(-b*t/m) #v = intial velocity, b = constant, t = time, m = mass
def p(t):
return (m*v)/b*(1-math.exp(-b*t/m))#v = intial velocity, b = constant, t = time, m = mass
b = int(14)
v0 = float(input("What is the initial velocity of the motorboat in m/s? "))
m = float(input("what is the mass of the motorboat in kg? "))
i=[0]
velocity,err = quad(f,0,21)
position,err = quad(p,0,21)
print("The velocity of the boat moving across a lake is ",format(velocity,".2f"),"m/s.")
print("The position of the boat after moving across a lake is ",format(position,".2f"),"m.")

你有几个错误:

  • 速度是位置的导数,而不是其积分
  • 位置是积分,但你正在积分位置本身
  • 从";沿横截面域设置足够的离散/网格点,t=[0,21]"他们似乎希望你自己创建导数和积分,而不是使用numpy的函数。
    • 使用具有有限dt的导数的定义
    • 例如,使用矩形方法进行积分:https://firsttimeprogrammer.blogspot.com/2015/03/numerical-integration-with-python.html
  • 使用matplotlib可以轻松绘制,请参阅:https://matplotlib.org/stable/tutorials/index.html

最新更新