-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbisection_method.py
53 lines (47 loc) · 1.21 KB
/
bisection_method.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#Definition of Polynomial Function
index=-1;accum=[]
degree=0; coeff=[]; guess = [];
def Poly():
u=0; sum2=0; F=[];global degree
degree = int(input("Enter the degree: "))
for i in range (degree+1):
c=float(input("Enter your coefficients "))
coeff.append(c)
for j in range (-20, 20):
sum1=0
for k in range(degree+1):
sum1=sum1+coeff[k]*j**(degree-k)
print("f(%.0f)f=%.2f"%(j,sum1))
F.append(sum1)
if(u>0):
sign=F[u-1]*F[u]
if(sign<=0):
guess.append(j)
u=u+1
print("The Guess:" ,guess)
def func(je):
fx=0
#print(degree,coeff)
for k in range(degree+1):
fx=fx+coeff[k]*je**(degree-k)
return fx
def bisect(a,b):
global index
global accum
if index>=10:
return
avg=(a+b)/2
accum.append(avg)
index=index+1
print("index is", index)
print("cccc",accum)
if index>0:
if abs(accum[index]-accum[index-1])<0.0001:
print("cccccc",accum[index])
else:
if (func(a)*func(avg)<=0):
bisect(a,avg)
else:
bisect(avg,b)
Poly()
bisect(-1,0)