Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hello, @RyanG! There are a couple of ways to do what you want.

  1. You could use the N() function with the right hand side (rhs) of your solutions:

    var('n')
    f(x,n) = -x^-1 + (1.097e7)*((1/(n-1)^2) - (1/n^2))
    
    for i in range (2,10):
        L = f(x,i).solve(x)
        print(N(L[0].rhs()))
    

    Let me explain the last line. Sage's solve() command returns a list of solutions. In this particular case, there only one solution, so we access that with L[0] (of course, if there was a second solution, it would be accessed with L[1], etc.). Now that solution has the form x == <some_number>, so we access that <some_number> with the rhs() command (of course, lhs() would access the variable x). Finally, we use the N() function in order to have a numerical approximation.

    I must point out that this method will work only when there is a unique solution. If there were many solutions---like with a polynomial of degree two or three---, this code should be modified in order to deal with that. (If you need that more general code, feel free to ask, and I can send it to you.)

    You can improve the output as follows:

    var('n')
    f(x,n) = -x^-1 + (1.097e7)*((1/(n-1)^2) - (1/n^2))
    
    for i in range (2,10):
        L = f(x,i).solve(x)
        print(L[0].lhs(), '=', N(L[0].rhs()))
    
  2. Sage has a find_root() function that works numerically, instead of symbolically as solve() does. You have to give it two arguments: the limits of the interval where you assume the root will be. You can "guesstimate" such interval by plotting your functions or simply use a crazy interval like $[0,100]$.

    In this particular case, you roots seem to be in $[0,1]$, but I will use the interval $[0,10]$ just to be sure:

    var('n')
    f(x,n) = -x^-1 + (1.097e7)*((1/(n-1)^2) - (1/n^2))
    
    for i in range (2,10):
        L = f(x,i).find_root(0,10)
        print('x =', L)
    

    Of course, this method has the disadvantage that only finds one root per call. If there were more than one solution for any of your functions, only one would be found. But, again, in this particular case, your functions have only one root.

I hope this helps!