ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 16 Mar 2011 02:14:48 -0500Plotting x, free variable errorhttp://ask.sagemath.org/question/7760/plotting-x-free-variable-error/I was plotting sin(x)/x and also wanted a seperate plot of sin(x) and x so I could visualize those components as well. However I get an error when plotting x saying "free variable x -> x". If I add a coefficient to h(x) such as `h(x) = 1.0001*x` then it works fine.
This is the code I am using:
f(x) = sin(x)/x
g(x) = sin(x)
h(x) = x
fig1 = plot(f,-10,10,rgbcolor='green')
fig2 = plot(g,-10,10,rgbcolor='purple')
fig3 = plot(h,-10,10,rgbcolor='red')
figure = fig1 + fig2 + fig3
show(figure,xmin=-10,xmax=10,ymin=-10,ymax=10,figsize=[8,8],fontsize=20)Tue, 09 Nov 2010 14:50:44 -0600http://ask.sagemath.org/question/7760/plotting-x-free-variable-error/Answer by cswiercz for <p>I was plotting sin(x)/x and also wanted a seperate plot of sin(x) and x so I could visualize those components as well. However I get an error when plotting x saying "free variable x -> x". If I add a coefficient to h(x) such as <code>h(x) = 1.0001*x</code> then it works fine.</p>
<p>This is the code I am using:</p>
<pre><code>f(x) = sin(x)/x
g(x) = sin(x)
h(x) = x
fig1 = plot(f,-10,10,rgbcolor='green')
fig2 = plot(g,-10,10,rgbcolor='purple')
fig3 = plot(h,-10,10,rgbcolor='red')
figure = fig1 + fig2 + fig3
show(figure,xmin=-10,xmax=10,ymin=-10,ymax=10,figsize=[8,8],fontsize=20)
</code></pre>
http://ask.sagemath.org/question/7760/plotting-x-free-variable-error/?answer=11774#post-id-11774The "`(x)`" appended to your function names is unnecessary and is what's causing the problem with the definition of `h`. Try this instead with the rest of your code left unchanged:
sage: f = sin(x)/x
sage: g = sin(x)
sage: h = x
Given that x is predefined as a variable when you load Sage (that is, when Sage is started it immediately runs the code `x = var('x')`) you'll still be able to call the functions like so:
sage: g(0) # or, more explicitly, g(x=0)
0
sage: h(5) # or, more explicitly, h(x=0)
5
It's curious, however, that there is no issue with appending "`(x)`" to the functions `f` and `g`. I wonder why...Tue, 09 Nov 2010 15:27:35 -0600http://ask.sagemath.org/question/7760/plotting-x-free-variable-error/?answer=11774#post-id-11774Comment by kcrisman for <p>The "<code>(x)</code>" appended to your function names is unnecessary and is what's causing the problem with the definition of <code>h</code>. Try this instead with the rest of your code left unchanged:</p>
<pre><code>sage: f = sin(x)/x
sage: g = sin(x)
sage: h = x
</code></pre>
<p>Given that x is predefined as a variable when you load Sage (that is, when Sage is started it immediately runs the code <code>x = var('x')</code>) you'll still be able to call the functions like so:</p>
<pre><code>sage: g(0) # or, more explicitly, g(x=0)
0
sage: h(5) # or, more explicitly, h(x=0)
5
</code></pre>
<p>It's curious, however, that there is no issue with appending "<code>(x)</code>" to the functions <code>f</code> and <code>g</code>. I wonder why...</p>
http://ask.sagemath.org/question/7760/plotting-x-free-variable-error/?comment=21982#post-id-21982Just in case anyone is looking at this in the future - this use of g(0) will cause a deprecation warning. Either use g(x)=sin(x), or use g(x=0).Wed, 16 Mar 2011 02:14:48 -0500http://ask.sagemath.org/question/7760/plotting-x-free-variable-error/?comment=21982#post-id-21982Answer by kcrisman for <p>I was plotting sin(x)/x and also wanted a seperate plot of sin(x) and x so I could visualize those components as well. However I get an error when plotting x saying "free variable x -> x". If I add a coefficient to h(x) such as <code>h(x) = 1.0001*x</code> then it works fine.</p>
<p>This is the code I am using:</p>
<pre><code>f(x) = sin(x)/x
g(x) = sin(x)
h(x) = x
fig1 = plot(f,-10,10,rgbcolor='green')
fig2 = plot(g,-10,10,rgbcolor='purple')
fig3 = plot(h,-10,10,rgbcolor='red')
figure = fig1 + fig2 + fig3
show(figure,xmin=-10,xmax=10,ymin=-10,ymax=10,figsize=[8,8],fontsize=20)
</code></pre>
http://ask.sagemath.org/question/7760/plotting-x-free-variable-error/?answer=11776#post-id-11776Yeah, and the same thing happens with `plot(h,(x,-10,10))`. That is not good - I'm sure we just never had this case show up before.
The exception arises in `sage.symbolic.expression_conversions.FastFloatConverter.symbol`:
try:
return self.ff.fast_float_constant(float(ex))
except TypeError:
raise ValueError, "free variable: %s" % repr(ex)
so that it assumes if a callable expression doesn't have the variable, it must be a constant. However, it before this asks for `if name in vars` and `if name in svars`, but here the `name` is `'x |--> x'`.
So what we need to do is fix it so that if the operator is `None` in the previous traceback in your error message, we check for this case explicitly, where the name isn't just one of the variables.
This is now [Ticket 10246](http://trac.sagemath.org/sage_trac/ticket/10246) .
----------
Edit: Patch at [Trac 10246](http://trac.sagemath.org/sage_trac/ticket/10246) is now ready for review.Wed, 10 Nov 2010 03:48:37 -0600http://ask.sagemath.org/question/7760/plotting-x-free-variable-error/?answer=11776#post-id-11776