Ipython sympy情节错误

时间:2013-07-24 02:13:24

标签: plot ipython sympy

我试图绘制一个方程式,它是使用sympy求解块的结果 这是我的代码和后面的错误消息:

%pylab inline
from sympy import init_printing;init_printing()
from sympy import *
d,vf,a,vi,t,x,h,g,theta=symbols('d vf a vi t x h g theta')
equations=[Eq(sin(theta),(0.5*g*t**2+h)/(vi*t)),Eq(cos(theta),x/(vi*t))]
ans=solve(equations,[h,t],dict=True)
h=ans[0][h]
vi=5
g=9.8
theta=0.707
plot(h,(x,0,5))

然后我收到以下错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-11-f388e50e21e7> in <module>()
----> 1 plot(h,(x,0,5))

C:\Anaconda\lib\site-packages\sympy\plotting\plot.pyc in plot(*args, **kwargs)
   1158     show = kwargs.pop('show', True)
   1159     series = []
-> 1160     plot_expr = check_arguments(args, 1, 1)
   1161     series = [LineOver1DRangeSeries(*arg, **kwargs) for arg in plot_expr]
   1162 

C:\Anaconda\lib\site-packages\sympy\plotting\plot.pyc in check_arguments(args, expr_len, nb_of_free_symbols)
   1620         if len(free_symbols) > nb_of_free_symbols:
   1621             raise ValueError("The number of free_symbols in the expression"
-> 1622                                 "is greater than %d" % nb_of_free_symbols)
   1623         if len(args) == i + nb_of_free_symbols and isinstance(args[i], Tuple):
   1624             ranges = Tuple(*[range_expr for range_expr in args[i:i + nb_of_free_symbols]])

ValueError: The number of free_symbols in the expressionis greater than 1

如果我重新键入h的核心方程,那么我得到正确的图。

感谢您的帮助我正在努力为我的物理专业学生开发明年使用

1 个答案:

答案 0 :(得分:3)

您尝试设置vigtheta的值的方式不起作用。 符号表达式h仍然由您定义的同情符号对象组成,而变量名称现在指向您定义的数字。要解决此问题,请更换行

vi=5
g=9.8
theta=0.707

h = h.subs({vi:5, g:9.8, theta:.707})

h = h.subs(vi,5).subs(g,9.8).subs(theta,.707)

我会选择你认为更清楚的。