【发布时间】:2020-01-27 20:52:45
【问题描述】:
在我的动力学课程中,我的任务是编写一个 Python 代码,该代码将绘制一个位置矢量从它从地面开始到降落到地面的轨迹。我目前让我的代码从我手动计算的两个零值创建一个线性空间,但我想把它编码进去。因为我还需要在轨迹上创建速度向量,所以我将位置向量分解为它的 x 和y 组件。我研究了xlim 和this 线程,但不知道如何实现它们。总的来说,我对 python 和编码还很陌生,所以我仍在努力了解事情是如何工作的。
import numpy as np
import matplotlib.pyplot as plt
#creates a function that returns the x component
def re10(x):
r1 = 0.05*x
return r1
#creates a function that returns the y component
def re20(x):
r2 = -4.91*(x**2) + 30*x + 100
return r2
#Calculates the two zeroes of the trajectory
tmin = (-30 + np.sqrt(30**2 -4*-4.91*100))/(2*-4.91)
tmax = (-30 - np.sqrt(30**2 -4*-4.91*100))/(2*-4.91)
#Initializing time space
t = np.linspace(tmin, tmax, 100)
#Plot
plt.plot(re10(t), re20(t)) #(x, y)
【问题讨论】:
-
你为什么不使用simpy? Python 和符号方程一起使用时真的很强大!
-
我从未听说过 simpy。
-
给我几秒钟 :)
-
我的错误,您可以使用
numpy来查找方程式的零点,而您实际上已经根据您的代码找到了它,所以只需使用 np.roots 来查找您的零点! -
一些未经要求的编码建议:我强烈建议您在代码中避免使用magic numbers。例如,将
g=9.81放在顶部某处,然后在需要时使用0.5*g而不是4.91。在编写代码一段时间后阅读代码时,这将大大帮助您(和其他人)。
标签: python