Skip to content

Commit e3f1de7

Browse files
authored
Create bisection_method.py
1 parent de430a9 commit e3f1de7

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

bisection_method.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author: sudipto3331
4+
"""
5+
6+
#Import libraries as necessary
7+
import math
8+
import numpy as np
9+
#import xlwt
10+
from xlwt import Workbook
11+
12+
#Take necessary input
13+
#For bisection, two input is required to bracket the root
14+
xl=np.float(input ('Enter 1st initial value: ')) #1st input
15+
xu=float(input ('Enter 2nd initial value: ')) #2nd input
16+
17+
#computing function values corresponding to initial values
18+
19+
fxl=(667.38/xl)*(1-math.exp(-0.146843*xl))-40
20+
fxu=(667.38/xu)*(1-math.exp(-0.146843*xu))-40
21+
22+
#checking initial input values
23+
if fxl*fxu>0:
24+
print('Wrong initial input')
25+
#if the initial input is correct
26+
elif fxl*fxu<0:
27+
#taking input
28+
err=float(input('Enter desired percentage relative error: '))
29+
ite=int(input('Enter number of iterations: '))
30+
#initialization
31+
x_l=np.zeros([ite])
32+
x_u=np.zeros([ite])
33+
x_c=np.zeros([ite])
34+
35+
f_xl=np.zeros([ite])
36+
f_xu=np.zeros([ite])
37+
f_xc=np.zeros([ite])
38+
39+
rel_err=np.zeros([ite])
40+
itern=np.zeros([ite])
41+
#storing initial computed values into array
42+
x_l[0]=xl
43+
x_u[0]=xu
44+
45+
f_xl[0]=fxl
46+
f_xu[0]=fxu
47+
#begin iteration
48+
for i in range(ite):
49+
#storing the values of iteration
50+
itern[i]=i+1
51+
#Bisection Formula
52+
x_c[i]=(x_l[i]+x_u[i])/2
53+
54+
f_xl[i]=(667.38/x_l[i])*(1-math.exp(-0.146843*x_l[i]))-40
55+
f_xu[i]=(667.38/x_u[i])*(1-math.exp(-0.146843*x_u[i]))-40
56+
f_xc[i]=(667.38/x_c[i])*(1-math.exp(-0.146843*x_c[i]))-40
57+
#calculating error
58+
if i>0:
59+
rel_err[i]=((x_c[i]-x_c[i-1])/x_c[i])*100
60+
#terminate if error criteria meets
61+
if all ([i>0, abs(rel_err[i])<err]):
62+
break
63+
elif f_xc[i]==0:
64+
break
65+
66+
if i==ite-1:
67+
break
68+
#replacement of the new estimate
69+
if all ([f_xc[i]>0, f_xl[i]>0]):
70+
x_l[i+1]=x_c[i]
71+
x_u[i+1]=x_u[i]
72+
elif all ([f_xc[i]>0, f_xu[i]>0]):
73+
x_u[i+1]=x_c[i]
74+
x_l[i+1]=x_l[i]
75+
elif all ([f_xc[i]<0, f_xl[i]<0]):
76+
x_l[i+1]=x_c[i]
77+
x_u[i+1]=x_u[i]
78+
elif all ([f_xc[i]<0, f_xu[i]<0]):
79+
x_u[i+1]=x_c[i]
80+
x_l[i+1]=x_l[i]
81+
82+
#Writing the results on an excel sheet
83+
#Workbook is created
84+
wb = Workbook()
85+
86+
# add_sheet is used to create sheet.
87+
sheet1 = wb.add_sheet('Sheet 1')
88+
num_of_iter=i
89+
90+
#writing on excel
91+
#sheet1.write(0,2,'The')
92+
sheet1.write(0,3,'Bisection')
93+
sheet1.write(0,4,'Method')
94+
#sheet1.write(0,5,x_c[i])
95+
96+
sheet1.write(1,0,'Number of iteration')
97+
sheet1.write(1,1,'x_l')
98+
sheet1.write(1,2,'x_u')
99+
sheet1.write(1,3,'f(x_l)')
100+
sheet1.write(1,4,'f(x_u)')
101+
sheet1.write(1,5,'x_c')
102+
sheet1.write(1,6,'f(x_c)')
103+
sheet1.write(1,7,'Relative error')
104+
105+
#writing values on excel
106+
for n in range(num_of_iter+1):
107+
108+
sheet1.write(n+2,0,itern[n])
109+
sheet1.write(n+2,1,x_l[n])
110+
sheet1.write(n+2,2,x_u[n])
111+
sheet1.write(n+2,3,f_xl[n])
112+
sheet1.write(n+2,4,f_xu[n])
113+
sheet1.write(n+2,5,x_c[n])
114+
sheet1.write(n+2,6,f_xc[n])
115+
sheet1.write(n+2,7,rel_err[n])
116+
117+
sheet1.write(n+4,2,'The')
118+
sheet1.write(n+4,3,'root')
119+
sheet1.write(n+4,4,'is')
120+
sheet1.write(n+4,5,x_c[i])
121+
122+
#save the excel file
123+
wb.save('LAB1.xls')

0 commit comments

Comments
 (0)