user2610827
user2610827

Reputation: 9

Ipython sympy plot error

I am trying to plot an equation that was the result of a solve block using sympy here is my code and the error message that follows:

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

then I get the following error:

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

If I retype the corect equation for h then I get the correct plot.

thanks for your help I am trying to develop this for my physics students to use next year

Upvotes: 0

Views: 922

Answers (1)

IanH
IanH

Reputation: 10700

The way you are trying to set the values of vi, g and theta doesn't work. The symbolic expression h is still made of the sympy symbolic objects you defined, while the variable names now point to the numbers you defined. To fix this replace the lines

vi=5
g=9.8
theta=0.707

with

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

or

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

I'd go with whichever you find clearer.

Upvotes: 3

Related Questions