# How do I evaluate symbolic expressions numerically in notebook() This post is a wiki. Anyone with karma >750 is welcome to improve it.

I have solved an equation using this statement

s1 = solve(eq,ss)

The result is

ss = rhs

By setting all other variables in the right-hand side of the equation, I can retrieve a value using, among other things,

print rhs

or

N(rhs),

but when I try

N(ss)

I get the error message "cannot evaluate symbolic expression numerically". How can I evaluate the left-hand side as if it were the right-hand side?

edit retag close merge delete

Sort by » oldest newest most voted This post is a wiki. Anyone with karma >750 is welcome to improve it.

I am not sure why you are trying to print N(ss). ss is just a variable.

Do you want to print s1.rhs().n(), which is the solution.

more

I have got the message: " cannot evaluate symbolic expression numerically"

This simple example does no problem to sage, var ('x','y', 'v') f(x) = exp(-x^2) x = 10 y = f(v).subs(v=x).numerical_approx(digits=5)

but some more complicate results in the above error message

more This post is a wiki. Anyone with karma >750 is welcome to improve it.

I wasn't being clear, so here's some code. Assume that I've already defined an equation eq with a number of terms, including ss. Then I solve the equation for ss, with this result:

s1 = solve(eq,ss)
print s1

Evaluating the above, I see this:

ss == (c*n*st^2 + c*ra*rtr*st - ra*rdt - sqrt(2*c^2*n*ra*rtr*st^3 - 2*c*ra^2*rdt*rtr*st + ra^2*rdt^2 + (c^2*ra^2*rtr^2 + 2*c*n*ra*rdis - 2*c*n*ra*rdr)*st^2))/(c*n*st)

Now I want to calculate the value of ss, given that all the other variables in the equation have been given values, which they have. What I've done until now is copy the entire right-hand side of the equation into a print statement, as follows:

print (c*n*st^2 + c*ra*rtr*st - ra*rdt + sqrt(2*c^2*n*ra*rtr*st^3 - 2*c*ra^2*rdt*rtr*st + ra^2*rdt^2 + (c^2*ra^2*rtr^2 + 2*c*n*ra*rdis - 2*c*n*ra*rdr)*st^2))/(c*n*st)

Evaluating the above, I get a number representing the numeric value of the rhs of the equation, which is also the value of ss.

What I would like to do is have a function that would allow me to do this and get the same result:

print some_function(eq,ss)

because what I really want is the numeric value of ss, assuming that all the other variables in the equation have already been assigned values. And if I should have to modify the equation, I don't want to have to cut and paste a different string of symbols into the print statement. I want to just re-evaluate the lhs of eq.

more