Ask Your Question

TypeError in creating ODE variable

asked 2015-01-30 08:07:49 +0200

r.jay.hutchinson gravatar image

updated 2023-01-09 23:59:36 +0200

tmonteil gravatar image

I've just started using Sage for my ODE class in which my professor is using Maple. I don't have any experience using either system, so I'm just trying to figure it out using online tutorials. I don't understand what the problem here is:


Can anyone tell me how to fix this?

edit retag flag offensive close merge delete


You should better copy the content of the worksheet in the question, so that it is easier to copy/paste, and so that the information will not disappear when the image will be removed from remote website.

tmonteil gravatar imagetmonteil ( 2015-01-30 09:15:36 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2015-01-30 11:50:56 +0200

tmonteil gravatar image

updated 2015-01-30 12:19:24 +0200

It seems indeed that you hit a bug:

sage: var('x')
sage: function('y', x)
sage: de = x*e^y*diff(y,x) == 2*(e^y+x^3*e^(2*x))

When you write var('x'), two actions are done : it returns the symbolic variable (or "symbol") x and it gives to the Python variable (or 'name') x this value. This process of both returning a value and creating a Python variable with the same name is called variable injection, and it is a big source of confusion.

When you write function('y', x), it is supposed to be the same : it should return the symbolic function y(x) and it give to the Python variable (or 'name') y this value.

The problem comes from the fact that function() does not inject its result into the Python variable y correctly:

sage: z = function('y', x)
sage: y
sage: z
sage: y == z
sage: type(y)
<class 'sage.symbolic.function_factory.NewSymbolicFunction'>
sage: type(z)
<type 'sage.symbolic.expression.Expression'>

As you can see, function('y', x) returned a symbolic expression y(x), while the Python variable y got some different stuff, more precisely a function (with no variable name specifies, somehow similar to cos or sin).

So, a first workaround is to overwrite the variable injection:

sage: x = var('x')
sage: y = function('y', x)
sage: de = x*e^y*diff(y,x) == 2*(e^y+x^3*e^(2*x))
sage: de
x*e^y(x)*D[0](y)(x) == 2*x^3*e^(2*x) + 2*e^y(x)

Another workaround is to consider y as a function (whose variable name is not specified), and use the symbolic expression y(x) in your equation:

sage: var('x')
sage: function('y')
sage: de = x*e^y(x)*diff(y(x),x) == 2*(e^y(x)+x^3*e^(2*x))
sage: de
x*e^y(x)*D[0](y)(x) == 2*x^3*e^(2*x) + 2*e^y(x)

Thanks for reporting, actually a similar bug was reported as trac ticket 15025.

edit flag offensive delete link more


That makes sense. A lot of this Sage/Python stuff is not particularly intuitive to those of us with a limited CS background. Thanks for responding quickly to what probably seems like a stupid question!

r.jay.hutchinson gravatar imager.jay.hutchinson ( 2015-01-30 16:24:45 +0200 )edit

It was not a stupid question, you really hit an issue of Sage. It is even possible that function('y', x) will be deprecated at some point (so you will have to use the second workaround).

tmonteil gravatar imagetmonteil ( 2015-01-30 17:49:40 +0200 )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

1 follower


Asked: 2015-01-30 08:07:49 +0200

Seen: 300 times

Last updated: Jan 30 '15