Ask Your Question
3

Plotting x, free variable error

asked 2010-11-09 21:50:44 +0100

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
show(figure,xmin=-10,xmax=10,ymin=-10,ymax=10,figsize=[8,8],fontsize=20)
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
3

answered 2010-11-10 10:48:37 +0100

kcrisman gravatar image

updated 2011-03-16 08:12:41 +0100

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.

edit flag offensive delete link more
3

answered 2010-11-09 22:27:35 +0100

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

edit flag offensive delete link more

Comments

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 08:14:48 +0100 )edit

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2010-11-09 21:50:44 +0100

Seen: 1,022 times

Last updated: Mar 16 '11