Search code examples
sage

Why won't sage math evaluate this expression?


var('u_5 u beta')
y = u - u_5 + beta*(exp(u)-u-4)
beta = 1.0
u_5 = -4.0
p = y.subs(u==u_5)
p

results in: 0.0183156388887342*beta - u_5 - 4.00000000000000

How can I get a numerical value? I did try numerical_approx, n() etc.

I expect it to multiply 0.0183... by 1 (beta) and subtract the -4 for u_5


Solution

  • I'm not completely clear on what you're trying to do, but how about this:

    var('u_5 u beta')
    y = u - u_5 + beta*(exp(u)-u-4)
    p = y.subs(u==u_5)
    p.subs(beta==1., u_5=-4.)
    

    should return 0.0183156388887342. More succinctly, you can combine the last two lines:

    p = y.subs(u==u_5).subs(beta==1., u_5=-4.)
    

    (You have to substitute for u first and then plug in u_5, so I think these have to be done in stages.)

    One of the issues with your approach: when you evaluate beta = 1.0, this redefines beta, but it doesn't substitute this new definition into any existing expressions containing it. The subs method does this.