使用 matplotlib 和 numpy 绘制 1 个 y 位置值与 2 个 x 位置值的弹丸运动

Plotting projectile motion of 1 y-position values vs. 2 x-position values using matplotlib and numpy

您好,我正在尝试绘制抛射运动下质量的轨迹图。一个有作用在水平轴上的力,一个没有(基本上是针对一组 y 值绘制的 2 组 x 值)。这是我到目前为止所拥有的..我是编程新手,我似乎无法弄清楚哪里出了问题。希望你们能帮助我。谢谢!

import numpy as np
import matplotlib.pyplot as pl

def position(y0, v0, theta, g, t):
    y= y0 + v0*np.sin(theta)*t + (g*t**2)/2
    return y

def position2(x0, v0, theta, c, e, alpha, t):
    x1 = x0 + v0*(np.cos(theta))*t + c*(t*(e-1)+(2-2*e)/alpha)
    return x1

def position3(x0, v0, theta, t):
    x2 = x0 + v0*(np.cos(theta))*t
    return x2

t = np.linspace(0,10,1000)

#part1
m = 1
theta = 45
y0 = 2
x0 = 0
v0 = 3
k = 1
alpha = 0.5
g = -9.8
c = (-k/m)*(1/alpha**2)
e = -(np.e**(-alpha*t))

x1 = []
x2 = []
y = []

for a in t:
    x1_data = position2(x0, v0, theta, c, e, alpha, t)
    x1.append(x1_data)
    x2_data = position3(x0, v0, theta, t)
    x2.append(x2_data)    
    y_data =  position(y0, v0, theta, g, t)
    y.append(y_data)

print x1_data
print x2_data
print y_data

pl.title('Constant and Time-Dependent Forces')
pl.xlabel(b'x-position')
pl.ylabel(b'y-position')

x1label = 'projectile 1'
x2label = "'normal' projectile"
plot1 = pl.plot(x1_data, y, 'r')
plot2 = pl.plot(x2_data, y, 'b')

pl.legend()
pl.show()

由于我是 matplotlib 的新手,所以我仔细阅读了您的代码并想尝试一下。我发现的唯一错误是在你执行 for a in t: 但最终将 t 传递给函数而不是 a.

的 for 循环中
import numpy as np
import matplotlib.pyplot as pl

sin = np.sin
cos = np.cos
pi = np.pi


def y_position(y0, v0, phi, g, t):
    y_t = y0 + v0 * sin(phi) * t + (g * t**2) / 2
    return y_t


def x_position_force(x0, v0, phi, k, m, alpha, t):
    term1 = (-k / m) * (1 / alpha ** 2)
    term2 = -np.e ** (-alpha * t)
    x_t = x0 + v0 * cos(phi) * t + term1 * (t * (term2 - 1) + (2 - 2 * term2) / alpha)
    return x_t


def x_position_no_force(x0, v0, phi, t):
    x_t = x0 + v0 * cos(phi) * t
    return x_t


time = np.linspace(0, 10, 100)

#-------------  I N P U T  -------------#
x_init  = 0
y_init  = 2
v_init  = 3
theta   = 45
gravity = -9.8
m = 1
k = 1
alpha = 0.5
#-------------  I N P U T  -------------#
x_with_force = []
x_with_no_force = []
y = []

for time_i in time:
    x_with_force.append(x_position_force(x_init, v_init, theta, k, m, alpha, time_i))
    x_with_no_force.append(x_position_no_force(x_init, v_init, theta, time_i))
    y.append(y_position(y_init, v_init, theta, gravity, time_i))

# print(x1_data)
# print(x2_data)
# print(y_data)

pl.subplot(211)
pl.title('Constant and Time-Dependent Forces')
pl.xlabel('time')
plot1 = pl.plot(time, x_with_force, 'r', label='x_coord_dynamicF')
plot2 = pl.plot(time, x_with_no_force, 'g', label='x_coord_staticF')
plot3 = pl.plot(time, y, 'b', label='y_coord')
pl.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=2, mode="expand", borderaxespad=0.)

pl.subplot(212)
pl.title('Trajectory (x,y)')
pl.xlabel('X')
pl.ylabel('Y')
plot4 = pl.plot(x_with_force, y, 'r^')
plot5 = pl.plot(x_with_no_force, y, 'b*')
pl.show()

我更改了一些内容,使代码与 PEP8 内联。在我看来,使用错误的变量名会导致您犯下错误。因此,我建议您花些时间输入那些最终能帮助您和阅读您的代码的人的额外字符。