In this article, we will be going to study the bisection method in numerical analysis and design using Python.
Algorithm
Read a,b,E
i=0
if f(a)*f(b)>0 the begin write 'starting values unsuitable'
endif
while |f(m)|<0 do
begin
m=(a+b)/2
i=i+1
if f(a)*f(b)>0 then a=m else b=m
endif
write 'Solution converges to a root'
write 'Number of iterations=',I
write m,f(m)
stop
Flowchart
Example
Q] Find the real root of equation x*x-x-1=0 by bisection method up to 4 decimal places? a and b are given as 1 and 2.
Solution-
import math
def f(x):
return x*x - x -1
def bisection(a,b):
if (f(a) * f(b) >= 0.0):
print("Invalid Interval\n")
return
m=(a+b)/2
while (abs(f(m))>= 0.01):
m= (a+b)/2
if(f(m)==0.0):
break
if (f(m)*f(a) < 0.0):
b = m
else:
a = m
print("The value of root is : ","%.4f"%m)
a =1.0
b = 2.0
bisection(a, b)
Happy Coding!
Follow us on Instagram @programmersdoor
Join us on Telegram @programmersdoor
Please write comments if you find any bug in the above code, or want some updates.
Follow Programmers Door for more.
#bisectionmethod #rootfindingtechinques #NAD #numerical #analysis #design #maths #computer #science #question #python #programming
Comments