使用matplotlib和numpy绘制1 y位置值与2 x位置值的抛射运动

时间:2016-09-13 13:58:48

标签: python numpy matplotlib plot projectile

你好我试图在弹丸运动下得到一个质量轨迹图。一个具有作用于水平轴的力而一个没有(基本上2组x值相对于1组y值绘制)。这是我到目前为止所做的...我是编程的新手,我似乎无法弄清楚这出错的地方。希望你们能帮助我。谢谢!

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()

1 个答案:

答案 0 :(得分:0)

因为我是matplotlib的新手,所以我仔细研究了你的代码。我发现的唯一错误是在for循环中for a in t:,但最终将t传递给函数而不是a

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内联。在我看来,使用错误的变量名称会导致你犯下的错误。因此,我建议您花时间输入那些最终会帮助您和人们阅读代码的额外字符。