置换代码非常慢。我正在寻找一种更有效的方法来获取s_soln的值。我已经尝试将tn3数组作为四元组的上限传递,但这似乎不起作用。
from matplotlib import pyplot as plt
import numpy as np
from scipy.integrate import odeint, quad
from scipy.interpolate import interp1d
def fun(v, t, m, k, g):
return (g*m - k*v**2)/m
#Velocity Plot
print 'Velocity Plot\n'
tn = np.linspace(0, 100, 1001)
v_soln = odeint(fun, 0, tn, args=(96, 0.2, 9.80665))
#Displacement Plot
tn3 = np.linspace(0, 1, 11)
v = np.asarray(v_soln).squeeze()
v = interp1d(tn, v)
s_soln = []
for i in tn3:
s_soln.append(list(quad(v, 0, i))[0])
s_soln = np.array(s_soln)
xvals = tn3
yvals = s_soln
fig, ax = plt.subplots(1,1,subplot_kw=dict(aspect='equal'))
ax.plot(xvals, yvals)
ax.set_xlabel('t')
ax.set_ylabel('s(t)')
plt.show()