# 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)

edit retag close merge delete

Sort by » oldest newest most voted

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:

        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 .

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

more

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

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