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.Mon, 02 Sep 2019 12:14:09 -0500getting a float result from solvehttp://ask.sagemath.org/question/47720/getting-a-float-result-from-solve/I am trying to solve a system of 2eqs, and sage keeps giving me integer results.
![image description](http://)
When I try to print a.n() I get an error cannot evaluate symbolic expression numerically.
Sorry, I tried to attach an image of it and I messed it up. Here is the code:
a,b=var('a b')
solve([7.0==a*sqrt(16)/(1+b/16.0),22==a*sqrt(4)/(1+b/4)],a,b)
print a.n()
The result the solve command gives is the ratios of integers.
(I typed an asterisk between the a and the sqrt, but it doesn't show up...)Mon, 02 Sep 2019 10:24:12 -0500http://ask.sagemath.org/question/47720/getting-a-float-result-from-solve/Comment by tmonteil for <p>I am trying to solve a system of 2eqs, and sage keeps giving me integer results.
<img alt="image description" src="http://">
When I try to print a.n() I get an error cannot evaluate symbolic expression numerically.</p>
<p>Sorry, I tried to attach an image of it and I messed it up. Here is the code:</p>
<pre><code>a,b=var('a b')
solve([7.0==a*sqrt(16)/(1+b/16.0),22==a*sqrt(4)/(1+b/4)],a,b)
print a.n()
</code></pre>
<p>The result the solve command gives is the ratios of integers.</p>
<p>(I typed an asterisk between the a and the sqrt, but it doesn't show up...)</p>
http://ask.sagemath.org/question/47720/getting-a-float-result-from-solve/?comment=47728#post-id-47728I added the complements you put in an answer into the question.Mon, 02 Sep 2019 12:06:49 -0500http://ask.sagemath.org/question/47720/getting-a-float-result-from-solve/?comment=47728#post-id-47728Comment by rburing for <p>I am trying to solve a system of 2eqs, and sage keeps giving me integer results.
<img alt="image description" src="http://">
When I try to print a.n() I get an error cannot evaluate symbolic expression numerically.</p>
<p>Sorry, I tried to attach an image of it and I messed it up. Here is the code:</p>
<pre><code>a,b=var('a b')
solve([7.0==a*sqrt(16)/(1+b/16.0),22==a*sqrt(4)/(1+b/4)],a,b)
print a.n()
</code></pre>
<p>The result the solve command gives is the ratios of integers.</p>
<p>(I typed an asterisk between the a and the sqrt, but it doesn't show up...)</p>
http://ask.sagemath.org/question/47720/getting-a-float-result-from-solve/?comment=47721#post-id-47721Post your code please.Mon, 02 Sep 2019 10:26:15 -0500http://ask.sagemath.org/question/47720/getting-a-float-result-from-solve/?comment=47721#post-id-47721Answer by tmonteil for <p>I am trying to solve a system of 2eqs, and sage keeps giving me integer results.
<img alt="image description" src="http://">
When I try to print a.n() I get an error cannot evaluate symbolic expression numerically.</p>
<p>Sorry, I tried to attach an image of it and I messed it up. Here is the code:</p>
<pre><code>a,b=var('a b')
solve([7.0==a*sqrt(16)/(1+b/16.0),22==a*sqrt(4)/(1+b/4)],a,b)
print a.n()
</code></pre>
<p>The result the solve command gives is the ratios of integers.</p>
<p>(I typed an asterisk between the a and the sqrt, but it doesn't show up...)</p>
http://ask.sagemath.org/question/47720/getting-a-float-result-from-solve/?answer=47727#post-id-47727When you run the `solve` command, the function returns a list of expressions, that are equalities. It does not touch the Python variables `a` and `b` which continue to point to the symbols `a` and `b`. Those are symbols, not numbers, so it is normal that they do not have any numerical value.
How to extract the values provided by the `solve` function ?
The simplest way, is first to return the solutions as dictionaries, not as symbolic equalities, by using the `solution_dict=True` option:
sage: s = solve([7.0==a*sqrt(16)/(1+b/16.0),22==a*sqrt(4)/(1+b/4)],a,b, solution_dict=True)
sage: s
[{b: -592/169, a: 231/169}]
Here, you see that there is only one solution:
sage: s[0]
{b: -592/169, a: 231/169}
You can extract the value for `a` as follows:
sage: s[0][a]
231/169
and take its numerical value:
sage: s[0][a].n()
1.36686390532544Mon, 02 Sep 2019 12:05:57 -0500http://ask.sagemath.org/question/47720/getting-a-float-result-from-solve/?answer=47727#post-id-47727Answer by Emmanuel Charpentier for <p>I am trying to solve a system of 2eqs, and sage keeps giving me integer results.
<img alt="image description" src="http://">
When I try to print a.n() I get an error cannot evaluate symbolic expression numerically.</p>
<p>Sorry, I tried to attach an image of it and I messed it up. Here is the code:</p>
<pre><code>a,b=var('a b')
solve([7.0==a*sqrt(16)/(1+b/16.0),22==a*sqrt(4)/(1+b/4)],a,b)
print a.n()
</code></pre>
<p>The result the solve command gives is the ratios of integers.</p>
<p>(I typed an asterisk between the a and the sqrt, but it doesn't show up...)</p>
http://ask.sagemath.org/question/47720/getting-a-float-result-from-solve/?answer=47732#post-id-477321) When you type code, mark it as such by selecting it and using the "1 0 1 0 1 0" button of the damn' editor...
2) the following one-liner does what you want:
[[u.lhs()==u.rhs().n() for u in S] for S in solve([7.0==a*sqrt(16)/(1+b/16.0),22==a*sqrt(4)/(1+b/4)],a,b)]
[[a == 1.36686390532544, b == -3.50295857988166]]
3) I seriously question the need to get floats (i. e. inexact values). I'd try to keep exact values as far as possible (including in the equations):
sage: solve([7==a*sqrt(16)/(1+b/16),22==a*sqrt(4)/(1+b/4)],a,b)
[[a == (231/169), b == (-592/169)]]
**EDIT:** I didn't see *tmonteil*'s excellent answer, which was more or less simultaneous. It differs fro mine, but is better on certain aspects(use of solution dictionaries...).Mon, 02 Sep 2019 12:14:09 -0500http://ask.sagemath.org/question/47720/getting-a-float-result-from-solve/?answer=47732#post-id-47732Answer by Wayne for <p>I am trying to solve a system of 2eqs, and sage keeps giving me integer results.
<img alt="image description" src="http://">
When I try to print a.n() I get an error cannot evaluate symbolic expression numerically.</p>
<p>Sorry, I tried to attach an image of it and I messed it up. Here is the code:</p>
<pre><code>a,b=var('a b')
solve([7.0==a*sqrt(16)/(1+b/16.0),22==a*sqrt(4)/(1+b/4)],a,b)
print a.n()
</code></pre>
<p>The result the solve command gives is the ratios of integers.</p>
<p>(I typed an asterisk between the a and the sqrt, but it doesn't show up...)</p>
http://ask.sagemath.org/question/47720/getting-a-float-result-from-solve/?answer=47725#post-id-47725Sorry, I tried to attach an image of it and I messed it up. Here is the code:
a,b=var('a b')
solve([7.0==a*sqrt(16)/(1+b/16.0),22==a*sqrt(4)/(1+b/4)],a,b)
print a.n()
The result the solve command gives is the ratios of integers.
(I typed an asterisk between the a and the sqrt, but it doesn't show up...)
Mon, 02 Sep 2019 11:36:40 -0500http://ask.sagemath.org/question/47720/getting-a-float-result-from-solve/?answer=47725#post-id-47725