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

On Mon, 29 Aug 2022 at 10:48, Varun Kumar <varun83it...@gmail.com> wrote:

> 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.
>
> On Mon, Aug 29, 2022 at 12:18 AM G. M.-S. <lists....@gmail.com> wrote:
>
>>
>> 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
>>
>> On Sun, 28 Aug 2022 at 13:31, Varun Kumar <varun83it...@gmail.com> wrote:
>>
>>>  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
>>>
>>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sage-support/CANnG18_STOtHiDrwbgp8ZR4x3G0np_GG4YYUks-A%3DCKyS45Azw%40mail.gmail.com.

Reply via email to