Error while finding a solution of the non linear equation by bisection method using loop in SageCellServer

26 views
Skip to first unread message

Varun Kumar

unread,
Aug 28, 2022, 7:31:51 AM8/28/22
to sage-support
 def Bisection(f, a, b, errorBound):
    xm = (a+b)/2
    while( f(x)!=0 and (b-a) >= errorBound):
        if(f(a)*f(xm)<0):
            b = xm
            else:
                a = xm
                xm = (a+b)/2
                return xm
def f(x):
return x^3-9*x+1
bisection(f,2,3,0.001)
print("root=", xm,"f(x)=",f(x))
After evaluating we get an error like: 
 File "/tmp/ipykernel_997521/4223205321.py", line 6 else: ^ SyntaxError: invalid syntax

What can I do and what is the tool that I can use?
Help me
Your's 
Varun

Colombel Bruno

unread,
Aug 28, 2022, 8:38:57 AM8/28/22
to sage-s...@googlegroups.com
You need to change some indentations.

def Bisection(f, a, b, errorBound):
    xm = (a+b)/2
    while( f(x)!=0 and (b-a) >= errorBound):
        if(f(a)*f(xm)<0):
            b = xm
        else:
            a = xm
        xm = (a+b)/2
    return xm.n()

def f(x):
    return x^3-9*x+1

Bisection(f,2,3,0.001)
2.94287109375000



--
You received this message because you are subscribed to the Google Groups "sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-support...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sage-support/0ccec33b-da7c-4f9f-8525-8ebf2903911fn%40googlegroups.com.

G. M.-S.

unread,
Aug 28, 2022, 2:48:35 PM8/28/22
to sage-s...@googlegroups.com

Hi Varun.

There are several problems (besides the confusion between "Bisection" and "bisection").

1) As you know, in Python blocks are delimited by indentation.  So it should be

sage: def bisection(f, a, b, errorBound):

....:     xm = (a+b)/2

....:     while f(x) != 0 and (b-a) >= errorBound:

....:         if f(a)*f(xm) < 0:

....:             b = xm

....:         else:

....:             a = xm

....:         xm = (a+b)/2

....:     return xm


2) The second problem is you suppose b>a which is not always true:

sage: bisection(f,3,2,.001).n()

2.50000000000000


which is totally wrong.


3) There is a third problem, as you can see with

sage: f(x) = x^2-9

sage: x = 3

sage: bisection(f,-2,10,1)

4

which is totally wrong.

This is because you use x instead of xm inside bisection.


4) Also, the while loop is not guaranteed to terminate, so a finite loop is better.


5) Finally, as you are looking for a root, it is perhaps best to bound both |f(xm)| and |b–a|.


6) Do not forget that xm is a local variable, so it is not defined outside bisection.


So you could do something similar to the following:


sage: def mybisection(f, aa, bb, maxvalueerror, maxrooterror, maxiter):

....:     if abs(f(aa)) < maxrooterror:

....:         print("Solution found after 0 iterations.")

....:         return aa

....:     if abs(f(bb)) < maxrooterror:

....:         print("Solution found after 0 iterations.")

....:         return bb

....:     if aa < bb:

....:         a,b = aa,bb

....:     else:

....:         a,b = bb,aa

....:     if f(a)*f(b) > 0:

....:         print("No guaranteed solution, stopping.")

....:         return None

....:     for i in range(maxiter):

....:         xm = (a+b)/2

....:         if abs(f(xm)) < maxvalueerror and (b-a)/2 < maxrooterror:

....:             print("Solution found after "+str(i+1)+" iterations.")

....:             return xm

....:         if f(a)*f(xm) < 0:

....:             b = xm

....:         else:

....:             a = xm

....:     print("No solution found after "+str(maxiter)+" iterations")

....:     return None

....: 

sage: def f(x):

....:     return x^3-9*x+1

....: 

sage: x0 = mybisection(f,2,3,10^-12,10^-12,1000)

Solution found after 40 iterations.

sage: x0.n(),f(x0).n()

(2.94282005779587, 5.45047701991629e-13)

sage: 


(Notice the name, to avoid any clash in case bisection already exists.)

HTH,

Guillermo

Varun Kumar

unread,
Aug 29, 2022, 4:48:03 AM8/29/22
to sage-s...@googlegroups.com
In my problem the Bisection means a bisection method in numerical analysis.
In this method, b > a always because we find the root of the equation in the interval (a, b) so for the formation of the interval it is necessary b > a.



--
You received this message because you are subscribed to the Google Groups "sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-support...@googlegroups.com.

G. M.-S.

unread,
Aug 29, 2022, 7:07:59 AM8/29/22
to sage-s...@googlegroups.com

Implicit assumptions are a great source of disasters (in programming and elsewhere).
There are many other things that can go wrong in this "bisection" procedure, I was just pointing at some of them.
Anyway, I am sorry if you took it badly.  I was only trying to help.

Best,

Guillermo
Reply all
Reply to author
Forward
0 new messages