ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 02 Apr 2011 15:50:32 -0500unable to numerically solve an equation (basic question)http://ask.sagemath.org/question/8043/unable-to-numerically-solve-an-equation-basic-question/Hello,<br>
I make my first steps with sage and already run into a problem I was not able to solve. When I enter the following:
reset()
var('x')
eqn=x^2+3*x-9==0
sol=solve(eqn,x,solution_dict=true)
sol[x].n(30)
I get the following error message:
The error message is: ('EOF in multi-line statement', (12, 0))
TypeError Traceback (most recent call last)
/home/.../sage-4.6.2/local/lib/python2.6/site-packages/sage/all_cmdline.pyc in <module>()
/home/.../sage-4.6.2/local/lib/python2.6/site-packages/sage/misc/preparser.pyc in load(filename, globals, attach) <br>
1594 execfile(fpath, globals)<br>
1595 elif fpath.endswith('.sage'):<br>
-> 1596 exec(preparse_file(open(fpath).read()) + "\n", globals)<br>
1597 elif fpath.endswith('.spyx') or fpath.endswith('.pyx'):<br>
1598 import interpreter<br>
/home/.../sage-4.6.2/local/lib/python2.6/site-packages/sage/all_cmdline.pyc in <module>()
/home/.../sage-4.6.2/local/lib/python2.6/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression.__index__ (sage/symbolic/expression.cpp:16771)()
/home/.../sage-4.6.2/local/lib/python2.6/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression._integer_ (sage/symbolic/expression.cpp:4254)()
TypeError: unable to convert x (=x) to an integer
I added the "..." manually for this post.
Obviously I make a severe problem in getting the numerical solution of this simple equation. Maybe you can help me in getting a numerical solution for an equation.
Btw. I get the same error message when I try the example given in the sage tutorial on page 14 ([[s[p].n(30),.....)
Thanks in advance!
Sat, 02 Apr 2011 02:42:50 -0500http://ask.sagemath.org/question/8043/unable-to-numerically-solve-an-equation-basic-question/Comment by kcrisman for <p>Hello,<br/>
I make my first steps with sage and already run into a problem I was not able to solve. When I enter the following:</p>
<pre><code>reset()
var('x')
eqn=x^2+3*x-9==0
sol=solve(eqn,x,solution_dict=true)
sol[x].n(30)
</code></pre>
<p>I get the following error message:</p>
<pre><code>The error message is: ('EOF in multi-line statement', (12, 0))
TypeError Traceback (most recent call last)
/home/.../sage-4.6.2/local/lib/python2.6/site-packages/sage/all_cmdline.pyc in <module>()
/home/.../sage-4.6.2/local/lib/python2.6/site-packages/sage/misc/preparser.pyc in load(filename, globals, attach) <br>
1594 execfile(fpath, globals)<br>
1595 elif fpath.endswith('.sage'):<br>
-> 1596 exec(preparse_file(open(fpath).read()) + "\n", globals)<br>
1597 elif fpath.endswith('.spyx') or fpath.endswith('.pyx'):<br>
1598 import interpreter<br>
/home/.../sage-4.6.2/local/lib/python2.6/site-packages/sage/all_cmdline.pyc in <module>()
/home/.../sage-4.6.2/local/lib/python2.6/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression.__index__ (sage/symbolic/expression.cpp:16771)()
/home/.../sage-4.6.2/local/lib/python2.6/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression._integer_ (sage/symbolic/expression.cpp:4254)()
TypeError: unable to convert x (=x) to an integer
</code></pre>
<p>I added the "..." manually for this post.</p>
<p>Obviously I make a severe problem in getting the numerical solution of this simple equation. Maybe you can help me in getting a numerical solution for an equation.</p>
<p>Btw. I get the same error message when I try the example given in the sage tutorial on page 14 ([[s[p].n(30),.....)</p>
<p>Thanks in advance!</p>
http://ask.sagemath.org/question/8043/unable-to-numerically-solve-an-equation-basic-question/?comment=21922#post-id-21922Just so you know, it's possible to make things look like code by indenting them four space - no <hr> or <br> needed - or by highlighting a region and clicking the button that looks like 101010.Sat, 02 Apr 2011 15:49:42 -0500http://ask.sagemath.org/question/8043/unable-to-numerically-solve-an-equation-basic-question/?comment=21922#post-id-21922Answer by DSM for <p>Hello,<br/>
I make my first steps with sage and already run into a problem I was not able to solve. When I enter the following:</p>
<pre><code>reset()
var('x')
eqn=x^2+3*x-9==0
sol=solve(eqn,x,solution_dict=true)
sol[x].n(30)
</code></pre>
<p>I get the following error message:</p>
<pre><code>The error message is: ('EOF in multi-line statement', (12, 0))
TypeError Traceback (most recent call last)
/home/.../sage-4.6.2/local/lib/python2.6/site-packages/sage/all_cmdline.pyc in <module>()
/home/.../sage-4.6.2/local/lib/python2.6/site-packages/sage/misc/preparser.pyc in load(filename, globals, attach) <br>
1594 execfile(fpath, globals)<br>
1595 elif fpath.endswith('.sage'):<br>
-> 1596 exec(preparse_file(open(fpath).read()) + "\n", globals)<br>
1597 elif fpath.endswith('.spyx') or fpath.endswith('.pyx'):<br>
1598 import interpreter<br>
/home/.../sage-4.6.2/local/lib/python2.6/site-packages/sage/all_cmdline.pyc in <module>()
/home/.../sage-4.6.2/local/lib/python2.6/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression.__index__ (sage/symbolic/expression.cpp:16771)()
/home/.../sage-4.6.2/local/lib/python2.6/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression._integer_ (sage/symbolic/expression.cpp:4254)()
TypeError: unable to convert x (=x) to an integer
</code></pre>
<p>I added the "..." manually for this post.</p>
<p>Obviously I make a severe problem in getting the numerical solution of this simple equation. Maybe you can help me in getting a numerical solution for an equation.</p>
<p>Btw. I get the same error message when I try the example given in the sage tutorial on page 14 ([[s[p].n(30),.....)</p>
<p>Thanks in advance!</p>
http://ask.sagemath.org/question/8043/unable-to-numerically-solve-an-equation-basic-question/?answer=12247#post-id-12247It helps to go step-by-step. In this case, the error message "unable to convert x (=x) to an integer" is telling you that something is a symbolic variable (x) which you're trying to treat as something else:
sage: reset()
sage: var('x')
x
sage: eqn=x^2+3*x-9==0
sage: eqn
x^2 + 3*x - 9 == 0
Looks good, now we can solve:
sage: sol=solve(eqn,x,solution_dict=true)
sage: sol
[{x: -3/2*sqrt(5) - 3/2}, {x: 3/2*sqrt(5) - 3/2}]
sage: type(sol)
<type 'list'>
and you see that sol isn't a *dictionary*, it's a *list* of dictionaries. So sol[x] can't work, because it's trying to take the "x-th" element of the list, when really you want one of the entries (0 or 1, as Python/Sage starts counting from 0 instead of 1):
sage: sol[0]
{x: -3/2*sqrt(5) - 3/2}
sage: sol[0][x]
-3/2*sqrt(5) - 3/2
sage: sol[0][x].n(100)
-4.8541019662496845446137605031
sage: sol[1][x].n(100)
1.8541019662496845446137605031
Does that make sense?
Sat, 02 Apr 2011 04:25:48 -0500http://ask.sagemath.org/question/8043/unable-to-numerically-solve-an-equation-basic-question/?answer=12247#post-id-12247Comment by donC for <p>It helps to go step-by-step. In this case, the error message "unable to convert x (=x) to an integer" is telling you that something is a symbolic variable (x) which you're trying to treat as something else:</p>
<pre><code>sage: reset()
sage: var('x')
x
sage: eqn=x^2+3*x-9==0
sage: eqn
x^2 + 3*x - 9 == 0
</code></pre>
<p>Looks good, now we can solve:</p>
<pre><code>sage: sol=solve(eqn,x,solution_dict=true)
sage: sol
[{x: -3/2*sqrt(5) - 3/2}, {x: 3/2*sqrt(5) - 3/2}]
sage: type(sol)
<type 'list'>
</code></pre>
<p>and you see that sol isn't a <em>dictionary</em>, it's a <em>list</em> of dictionaries. So sol[x] can't work, because it's trying to take the "x-th" element of the list, when really you want one of the entries (0 or 1, as Python/Sage starts counting from 0 instead of 1):</p>
<pre><code>sage: sol[0]
{x: -3/2*sqrt(5) - 3/2}
sage: sol[0][x]
-3/2*sqrt(5) - 3/2
sage: sol[0][x].n(100)
-4.8541019662496845446137605031
sage: sol[1][x].n(100)
1.8541019662496845446137605031
</code></pre>
<p>Does that make sense?</p>
http://ask.sagemath.org/question/8043/unable-to-numerically-solve-an-equation-basic-question/?comment=21928#post-id-21928Thank you vey much! After reading your response I really felt quite embarrassed.Sat, 02 Apr 2011 06:23:54 -0500http://ask.sagemath.org/question/8043/unable-to-numerically-solve-an-equation-basic-question/?comment=21928#post-id-21928Comment by kcrisman for <p>It helps to go step-by-step. In this case, the error message "unable to convert x (=x) to an integer" is telling you that something is a symbolic variable (x) which you're trying to treat as something else:</p>
<pre><code>sage: reset()
sage: var('x')
x
sage: eqn=x^2+3*x-9==0
sage: eqn
x^2 + 3*x - 9 == 0
</code></pre>
<p>Looks good, now we can solve:</p>
<pre><code>sage: sol=solve(eqn,x,solution_dict=true)
sage: sol
[{x: -3/2*sqrt(5) - 3/2}, {x: 3/2*sqrt(5) - 3/2}]
sage: type(sol)
<type 'list'>
</code></pre>
<p>and you see that sol isn't a <em>dictionary</em>, it's a <em>list</em> of dictionaries. So sol[x] can't work, because it's trying to take the "x-th" element of the list, when really you want one of the entries (0 or 1, as Python/Sage starts counting from 0 instead of 1):</p>
<pre><code>sage: sol[0]
{x: -3/2*sqrt(5) - 3/2}
sage: sol[0][x]
-3/2*sqrt(5) - 3/2
sage: sol[0][x].n(100)
-4.8541019662496845446137605031
sage: sol[1][x].n(100)
1.8541019662496845446137605031
</code></pre>
<p>Does that make sense?</p>
http://ask.sagemath.org/question/8043/unable-to-numerically-solve-an-equation-basic-question/?comment=21921#post-id-21921No need; it takes a while to master all the various data types Python uses, if you're not familiar with them.Sat, 02 Apr 2011 15:50:32 -0500http://ask.sagemath.org/question/8043/unable-to-numerically-solve-an-equation-basic-question/?comment=21921#post-id-21921