Ask Your Question
1

ValueError: free variable x |--> x when plotting the function x

asked 2011-03-15 10:35:34 +0100

updated 2011-04-28 19:04:54 +0100

Kelvin Li gravatar image

Hi all.

I got the following problem:

sage: plot(symbolic_expression(x).function(x))

this raises

ValueError: free variable: x |--> x

If I replace x by anything else (but 1*x) it works fine.

How can I do ?

My rationale behind my question is that I have a class which takes a function as argument and can perform many thinks on it, among other the plot. I made the following :

class MyFunction(object):
    def __init__(self,f):
        self.f=symbolic_expression(f).function(x)
    def plot(self):
        return plot(self.f)

My point in doing so is that I have to accept, as input f expressions like x**2, 2, g.diff(x) (where g is an other function) and so on. In these cases, it turns out that I need to use the symbolic_expression trick in order to be sure that what I have is a function (need for numerical integration for example)

My questions :

  1. Can I do otherwise in __init__ in order to be sure to be able to use numerical integration, derivative, ... on self.f ?

  2. If not, how can I plot when the input is simply "x" ?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2011-03-15 11:41:52 +0100

kcrisman gravatar image

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

This is Trac 10246. I knew I'd seen it before (in fact, I reported it), but it took a little while to find it. Unfortunately, that one has been on the back burner, but it's an easy fix once someone makes time to do it.

You definitely need to make the function callable, and this is one way to do it. So I don't see an obvious way around question 1. As for question 2, you may just have to catch this case explicitly for now (a try/except block where if plot(self.f) doesn't work, you check if the function is the identity and plot that).


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

edit flag offensive delete link more

Comments

In fact, see also http://ask.sagemath.org/question/198/plotting-x-free-variable-error ! Ironically, this does not appear under related questions on the right...

kcrisman gravatar imagekcrisman ( 2011-03-15 11:46:07 +0100 )edit

This is obviously not the answer I was expecting ;) But thanks, I'll do the try ... except.

Laurent Claessens gravatar imageLaurent Claessens ( 2011-03-15 11:56:16 +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: 2011-03-15 10:35:34 +0100

Seen: 1,072 times

Last updated: Mar 16 '11