Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hello, @ortollj! Let's answer your questions:

  1. How to supress warning messages? The problem is your definition of the function f (line 31 of your code). Instead of writing

    f = x^3 - 2*x^2 + x - 1
    

    you should write

    f(x) = x^3 - 2*x^2 + x - 1
    

    That's it! Notwithstanding the fact that this seems to be an unimportant addition to the code, it is actually a necessary, for Sage needs to know the independent variable(s) of a function in order to make some operations, like the derivative, etc.

  2. Why do the values of the convergence curves accumulate in rRg[3]? When you put the slider to the left, your delta (the error of the algorithm, called deltal in your code) becomes very small, and Newton's Method obviously requires more iterations in order to achieve that level of precision.

    In line 14 of your code, you append one point of the iteration to the list xL:

    xL.append((n,round(x_0,rd)))
    

    Then, in line 23, you can see that your function N_f calls itself (making N_f a recursive function) while the required precision is not achieved (among other conditions). In that line, you pass that new xL (with one extra point) to N_f (i.e., the list with all its new points is preserved from call to call). Then, the function appends another point, and then another, and then another, one per each iteration, until the precision is achieved (or some of the other stop conditions that you have established). Once the subroutine has ended, it returns some values:

    return x_0, n+1, delta, xL
    

    As you can see, xL is the fourth of them, thus corresponding to rRg[3] in the call

    rRg = N_f(x_0=x_0g,f=f,delta=deltag)
    

    Summarizing, when the interactive subroutine _ executes

    if rRg != [] :
        rRg = []
    

    it indeed empties the list rRg. But then it executes the line

    rRg = N_f(x_0=x_0g,f=f,delta=deltag)
    

    which iterates Newton's Method several times, adding one point to xL on every iteration. And the smaller the delta of the method, the more iterations it has to do, so the more points are appended. This xL is finally stored in rRg[3].

I hope this helps!

Hello, @ortollj! Let's answer your questions:

  1. How to supress warning messages? The problem is your definition of the function f (line 31 of your code). Instead of writing

    f = x^3 - 2*x^2 + x - 1
    

    you should write

    f(x) = x^3 - 2*x^2 + x - 1
    

    That's it! Notwithstanding the fact that this seems to be an unimportant addition to the code, it is actually a necessary, for Sage needs to know the independent variable(s) of a function in order to make some operations, like the derivative, etc.

  2. Why do the values of the convergence curves accumulate in rRg[3]? When you put I think you just discovered a bug. I don't know if this is Sage bug or a Jupyter bug, so I am posting a related question. It seems that the list xL is being kept in memory from execution to execution of the interact. For example, if you uncomment your last line of code and execute the interact, you will notice that every time you move the slider, the list rRg[3], which comes from xL in your first subroutine, gets bigger and bigger. It is storing the convergent sequences for every deltal you use (i.e., one sequence for every position of the slider to the left, your delta (the error of the algorithm, called deltal in your code) becomes very small, and Newton's Method obviously requires more iterations in order to achieve that level of precision.

    In you use).

    So, what do to do? I was able to solve this problem by changing your line 14 of your code, you append one point of the iteration to the list number 85 from

    rRg=N_f(x_0=x_0g,f=f,delta=deltag)
    

    to the almost similar

    rRg=N_f(x_0=x_0g,f=f,delta=deltag,xL=[])
    

    That way, you force xL:

    xL.append((n,round(x_0,rd)))
    

    Then, in line 23, you can see that your function N_f calls itself (making N_f a recursive function) while the required precision is not achieved (among other conditions). In that line, you pass that new xL (with one extra point) to N_f (i.e., the list with all its new points is preserved from call to call). Then, the function appends another point, and then another, and then another, one per each iteration, until the precision is achieved (or some of the other stop conditions that you have established). Once the subroutine has ended, it returns some values:

    return x_0, n+1, delta, xL
    

    As you can see, xL is the fourth of them, thus corresponding to rRg[3] in the call

    rRg = N_f(x_0=x_0g,f=f,delta=deltag)
    

    Summarizing, when the interactive subroutine _ executes

    if rRg != [] :
        rRg = []
    

    it indeed empties the list rRg. But then it executes the line

    rRg = N_f(x_0=x_0g,f=f,delta=deltag)
    

    which iterates Newton's Method several times, adding one point to xL to become empty on every iteration. And the smaller the delta of the method, the more iterations it has to do, movement of the slider, so the more points are appended. This xL is finally stored in rRg[3].information from previous executions (the previous convergent sequence for the previous slider position) is forgotten, and you get only the information for the current position of the slider.

I hope this helps!

Hello, @ortollj! Let's answer your questions:

  1. How to supress warning messages? The problem is your definition of the function f (line 31 of your code). Instead of writing

    f = x^3 - 2*x^2 + x - 1
    

    you should write

    f(x) = x^3 - 2*x^2 + x - 1
    

    That's it! Notwithstanding the fact that this seems to be an unimportant addition to the code, it is actually a necessary, for Sage needs to know the independent variable(s) of a function in order to make some operations, like the derivative, etc.

  2. Why do the values of the convergence curves accumulate in rRg[3]? I think you just discovered a bug. I don't know if this is Sage bug or a Jupyter bug, so I am posting a related question. question. It seems that the list xL is being kept in memory from execution to execution of the interact. For example, if you uncomment your last line of code and execute the interact, you will notice that every time you move the slider, the list rRg[3], which comes from xL in your first subroutine, gets bigger and bigger. It is storing the convergent sequences for every deltal you use (i.e., one sequence for every position of the slider you use).

    So, what do to do? I was able to solve this problem by changing your line number 85 from

    rRg=N_f(x_0=x_0g,f=f,delta=deltag)
    

    to the almost similar

    rRg=N_f(x_0=x_0g,f=f,delta=deltag,xL=[])
    

    That way, you force xL to become empty on every movement of the slider, so the information from previous executions (the previous convergent sequence for the previous slider position) is forgotten, and you get only the information for the current position of the slider.

I hope this helps!