Ask Your Question

Plotting x, free variable error

asked 2010-11-09 14:50:44 -0500

AaronLS gravatar image

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
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2010-11-09 15:27:35 -0500

The "(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)
sage: h(5)    # or, more explicitly, h(x=0)

It's curious, however, that there is no issue with appending "(x)" to the functions f and g. I wonder why...

edit flag offensive delete link more


Just 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).

kcrisman gravatar imagekcrisman ( 2011-03-16 02:14:48 -0500 )edit

answered 2010-11-10 03:48:37 -0500

kcrisman gravatar image

updated 2011-03-16 02:12:41 -0500

Yeah, 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:

            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 .

Edit: Patch at Trac 10246 is now ready for review.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2010-11-09 14:50:44 -0500

Seen: 634 times

Last updated: Mar 16 '11