Kkl*_*lj8 5 python matplotlib sympy python-2.7
到目前为止,对于任何给定的质量和阻力系数,我都设法找到了该方程式的特定解。但是,我还没有找到绘制解决方案甚至评估特定点解决方案的方法。我真的很想找到一种绘制解决方案的方法。
from sympy import *
m = float(raw_input('Mass:\n> '))
g = 9.8
k = float(raw_input('Drag Coefficient:\n> '))
f = Function('f')
f1 = g * m
t = Symbol('t')
v = Function('v')
equation = dsolve(f1 - k * v(t) - m * Derivative(v(t)), 0)
C1 = Symbol('C1')
C1_ic = solve(equation.rhs.subs({t:0}),C1)[0]
equation = equation.subs({C1:C1_ic})
Run Code Online (Sandbox Code Playgroud)
为了完整起见,您也可以使用 Sympy's plot,如果您想要“快速而肮脏”的情节,这可能更方便。
plot(equation.rhs,(t,0,10))
Run Code Online (Sandbox Code Playgroud)
Import these libraries (seaborn just makes the plots pretty).
from matplotlib import pyplot as plt
import seaborn as sns
import numpy as np
Run Code Online (Sandbox Code Playgroud)
Then tack this onto the end. This will plot time, t, against velocity, v(t).
# make a numpy-ready function from the sympy results
func = lambdify(t, equation.rhs,'numpy')
xvals = np.arange(0,10,.1)
yvals = func(xvals)
# make figure
fig, ax = plt.subplots(1,1,subplot_kw=dict(aspect='equal'))
ax.plot(xvals, yvals)
ax.set_xlabel('t')
ax.set_ylabel('v(t)')
plt.show()
Run Code Online (Sandbox Code Playgroud)
I get a plot like this for a mass of 2 and a drag coefficient of 2.

| 归档时间: |
|
| 查看次数: |
1099 次 |
| 最近记录: |