Trying to plot Lissajous curve to learn sympy but "TypeError: can't convert expression to float"

418 views
Skip to first unread message

Lawrence Tattrie

unread,
Mar 7, 2016, 5:55:09 PM3/7/16
to sympy
I am trying to learn sympy by plotting Lissajous curves. I am familiar with python and know some math. I downloaded and installed the Anaconda package for Windows. I have been able to get a simple curve plotted but the attached text file shows the few statements and a plot command which gives a "TypeError: can't convert expression to float" I have tried defining the symbols as real=true but that did not help. Is the plotting converting a float to a complex then complaining that it cannot convert a complex to float?  What is the problem with my code?

Attached is a short text file with statements and error.  Thanks for your help.

https://en.wikipedia.org/wiki/Lissajous_curve
sympy_type_error.txt

Amit Saha

unread,
Mar 7, 2016, 6:08:28 PM3/7/16
to sy...@googlegroups.com
You basically had couple of things which were not right. The first was
the dictionaries:

varx = {ampx:1.0, freqx:3.0, theta:pi/4.0, frictionx: frictionx}
vary = {ampy:2.0, freqy:1.0, frictionx: frictionx}


If you compare these to what you tried, you will see that the keys in
the dictionary are the symbol objects themselves, not the symbol
strings. So, the substitution you were attempting didn't work
correctly.

The second was you needed the following earlier in the code before you
were using them:

tmin = 0.0
tmax = 4.0 * pi

The complete working code is as follows:


from sympy import *
x, y, z, t = symbols('x y z t')
k, m, n = symbols('k m n', integer=True)
f, g, h = symbols('f g h', cls=Function)
init_printing()
from sympy.plotting import (plot, plot_parametric,
plot3d_parametric_surface, plot3d_parametric_line,
plot3d)
from sympy.polys.polyfuncs import interpolate


ampx, ampy, freqx, freqy, theta = symbols('ampx ampy freqx freqy
theta', real=true)
frictionx, frictiony = symbols('frictionx, frictiony', real=true)
tmin, tmax = symbols('tmin, tmax', real=true)

tmin = 0.0
tmax = 4.0 * pi

frictionx = interpolate([(tmin, 0.0), (tmax, ln(0.5))], t)
frictiony = interpolate([(tmin, 0.0), (tmax, ln(0.5))], t)


x = ampx * frictionx * sin(freqx * t + theta)
y = ampy * frictiony * sin(freqy * t)


varx = {ampx:1.0, freqx:3.0, theta:pi/4.0, frictionx: frictionx}
vary = {ampy:2.0, freqy:1.0, frictionx: frictionx}
print x.subs(varx)

plot_parametric(x.subs(varx), y.subs(vary), (t, tmin, tmax))



Hope that helps you.

Best Wishes,
Amit.









>
> https://en.wikipedia.org/wiki/Lissajous_curve
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/f4965185-4dc6-4299-858e-8f36ba452eb3%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
http://echorand.me

Lawrence Tattrie

unread,
Mar 7, 2016, 6:39:05 PM3/7/16
to sympy
Thank you. The plot works. I should have seen the tmin and tmax. That correction seemed to do the trick. The frictionx and frictiony do not seem to be needed in the dictionaries varx, vary.
Thank you again.
- Lawrence

Aaron Meurer

unread,
Mar 7, 2016, 7:17:31 PM3/7/16
to sy...@googlegroups.com
In general, don't define symbols for variables and then later on
reassign the same variable to something else, like

x = symbols('x')
# Some stuff
x = 1

This is a relatively common mistake that people make. There is no need
to create a symbol for a variable if you never intend to use that
variable as a SymPy symbol, but are going to just assign the Python
variable directly to some value. It's worth making sure you understand
the difference between SymPy symbols and Python variables (see
http://docs.sympy.org/latest/tutorial/gotchas.html for a writeup about
it).

Aaron Meurer
> https://groups.google.com/d/msgid/sympy/c25365a3-49a2-478e-a690-3f972a26188b%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages