我创建了程序,该程序通过 this公式,使用matplotlib绘制了人体飞行路线图。该代码似乎是正确的,但是由于某些原因我得到了this,x和y为负,并且从某处出现在右上角。
我做了SciPy的this视频编码。
对于头等舱,一切都很好,因此,只看二等舱,后者必须计算此x和y坐标。
怎么了?谢谢。
from scipy import integrate
from math import sin, cos
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# constants:
g = 9.81
# input:
k = 0.05 # air resistance
angle = 60 # angle of throw
v0 = 5 # start velocity
m = 4 # mass of the body
class GraphTrajectoryAnimation:
def __init__(self, x, y, color, m=1, lw=3, width=6, height=4, dpi=100):
"""
Where x, y - NumPy lists with coordinates, color - color of line (default - choose by Random)
height, width, dpi - hw, dpi of the figure from PyLab.
"""
self.figure = plt.figure(figsize=(width, height), dpi=dpi) # Init figure and his needs to plot
self.x = x
self.y = y
self.m = m
self.color = color
self.lw = lw
def animation(self, i, x, y, m, color):
""" Definition of a matplotlib animation. """
plt.plot(x[:i * self.m], y[:i * self.m], lw=self.lw, color=self.color)
def graph(self, interval=1):
""" Definition of cycle FuncAnimation, which call animation. """
self.graph_id = animation.FuncAnimation(self.figure, self.animation,
fargs=(self.x, self.y, self.m, self.color),
repeat=False, interval=interval)
def showfigure(self):
""" Call the graph function and displays it on the figure. """
self.graph()
plt.show()
class SolveSystemOfADifferentialEquations:
def __init__(self, k, angle, v0, m):
self.k = k
self.angle = angle
self.m = m
self.v0 = v0
self.v0_x = self.v0 * cos(self.angle)
self.v0_y = self.v0 * sin(self.angle)
self.kdivm = self.k / self.m
self.time = np.linspace(0, 10, 100)
def xmodel(self, X, t):
x = X[0]
dx = X[1]
xdot = [ [], [] ]
xdot[0] = dx
xdot[1] = -self.kdivm * x
return xdot
def ymodel(self, Y, t):
y = Y[0]
dy = Y[1]
ydot = [ [], [] ]
ydot[0] = dy
ydot[1] = -g - self.kdivm * dy
return ydot
def solveX(self):
x = integrate.odeint(self.xmodel, [0, self.v0_x], self.time)
return x
def solveY(self):
y = integrate.odeint(self.ymodel, [0, self.v0_y], self.time)
return y
Solver = SolveSystemOfADifferentialEquations(k, angle, v0, m)
x = Solver.solveX()
y = Solver.solveY()
def show():
root = GraphTrajectoryAnimation(x, y, 'blue')
root.showfigure()
if __name__ == "__main__":
show()
我认为这是因为您以度为单位给出角度,但是python中的sin和cos函数将其视为弧度。因此,当您编写sin(60)时,程序会考虑使用sin(60弧度)而不是sin(60度)。将π/ 3弧度而不是60度作为弹丸的初始角度,它应该可以工作。