Ask Your Question
1

getting a float result from solve

asked 5 years ago

Wayne gravatar image

updated 5 years ago

tmonteil gravatar image

I am trying to solve a system of 2eqs, and sage keeps giving me integer results. image description 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...)

Preview: (hide)

Comments

1

Post your code please.

rburing gravatar imagerburing ( 5 years ago )
1

I added the complements you put in an answer into the question.

tmonteil gravatar imagetmonteil ( 5 years ago )

3 Answers

Sort by » oldest newest most voted
1

answered 5 years ago

tmonteil gravatar image

updated 5 years ago

When 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.36686390532544
Preview: (hide)
link
0

answered 5 years ago

Wayne gravatar image

updated 5 years ago

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==asqrt(16)/(1+b/16.0),22==asqrt(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...)

Preview: (hide)
link
0

answered 5 years ago

Emmanuel Charpentier gravatar image

updated 5 years ago

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

Preview: (hide)
link

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

Stats

Asked: 5 years ago

Seen: 2,370 times

Last updated: Sep 03 '19