# numerical evaluation with variables

Hello,

a few days ago I started working with sage. So very soon I run into into questions. Here is one of them:

I defined an equation:

sage: x, y = var('x, y')

sage: eq = y == (sqrt(5) + 3) * x^3 + e^pi * x^2 + sqrt(2) * x + 1/27

And I want to get from that something like:

y == 5.236 * x^3 + 23.141 * x^2 + 1.414 * x + 0.037

the numerical_approx() seems to be designed for another purpose. How do I get my desired result?

edit retag close merge delete

Sort by ยป oldest newest most voted

One way is to use _convert to coerce the symbolic coefficients to the Real Field:

sage: var("x y")
(x, y)
sage: eq = y == (sqrt(5) + 3) * x^3 + e^pi * x^2 + sqrt(2) * x + 1/27
sage: eq._convert(RR)
y == 5.23606797749979*x^3 + 23.1406926327793*x^2 + 1.41421356237310*x + 0.0370370370370370


There's a ticket -- trac #12577 -- which proposes to expose this (by which I mean get rid of the initial _). Unfortunately the convert method without an underscore already exists, and doesn't seem to be really useful, but we could use something more explicit like change_coefficient_ring. Actually, it could even take a function argument as well as a ring, and could be used for quick transformations without needing to implement an expression walker..

more

Thank you, that is exactly what I needed.

( 2012-09-30 04:12:13 -0500 )edit