If I understand correctly, the problem is in the u = 1/2*… line:
You have to define t as a symbol to use it. See
http://docs.sympy.org/gotchas.html#id2. isympy automatically creates some
variables for you, but t is not one of them. So you need to have var('t'), t =
Symbol('t'), or something like that in there.
Also, on a related note, 1/2 will be evaluated by Python to 0.5 before SymPy
can make it a Rational. This should be fine as far as the plotting goes, but
if you would rather deal with exact fractions, you should do something like
S(1)/2, or just pass the whole thing to sympify (the same thing as S() ) as a
string:
u = sympify("1/2 * (sin(pi*(x+t/6)) + sin(pi*(x-t/6)))")
This will also make t a variable for you (though it will not put it into the
namespace).
I don't have pylab installed, so I can't test to see if there is some other
problem.
Also, apparently range(0, 6.6) is depreciated. Use range(0, 6) instead.
I hope this answers at least part of you question.
Aaron Meurer
On Feb 1, 2010, at 2:34 PM, Chad File wrote:
> I have a question. It's more of a python question than a sympy question...
> but here it is. I have this function that I'm trying to view/model.
> Basically it's a sine function... but a traveling sine function. I'd like to
> plot it at various stages of progression. I thought something of the
> following would be acceptable, but I was wrong.
> ##
> import pylab
> xx = pylab.arange(0,1,1/1000)
> u = 1/2 * (sin(pi*(x+t/6)) + sin(pi*(x-t/6)))
> U = lambdify(x, u, 'numpy')
> pylab.figure(1)
> for t in range(0,6.6):
> pylab.plot(xx, U(xx))
> pylab.show()
> ##
> Here's the output
> ##
> NameError: global name 't' is not defined
> ##
> I'm using isympy as interface, FYI.
>
> I was thinking/hoping that sympy would "see" the t dependence and substitute
> accordingly. However, I believe that numpy is taking over at the point of
> initializing U. Hence my error of an undefined variable t.
>
> I thought I would outsmart numpy by making a substitution of my own and
> modify the for-loop as follows.
> ##
> for i in range(0,6.6):
> pylab.plot(xx, U(xx).subs(t,i/6))
> ##
> However, that failed as well.
>
> Does anybody know of a little trick that I could use to get the original
> function u(x,t) to plot at various values of t... without hard coding each
> interval independently?
>
> ~~archery~~
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sympy?hl=en.