On Tue, Mar 8, 2016 at 9:54 AM, Lawrence Tattrie
<[email protected]> wrote:
> 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.
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 [email protected].
> To post to this group, send email to [email protected].
> 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
--
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 [email protected].
To post to this group, send email to [email protected].
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/CANODV3mSkzi8G%2BJo85bhOPNzGxEVPNzbLmOq877tNSyNXAj2AQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.