Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import cos
- def tested_function(x):
- return cos(x) - x
- def main():
- print('[x0, x1]')
- x0 = float(input('x0 = '))
- x1 = float(input('x1 = '))
- pivot = 0
- iteration = int(input('Number of iteration ?'))
- for i in range(iteration):
- x2 = x1 - tested_function(x1) *\
- (x1 - x0) / (tested_function(x1) - tested_function(x0))
- r = tested_function(x2)
- print('{}'.format(r))
- if r == 0:
- print('done')
- return
- if tested_function(x2) * tested_function(x1) < pivot:
- x0 = x2
- else:
- x1 = x2
- print('[{}, {}]'.format(x0, x1))
- print(x1)
- main()
Advertisement
Add Comment
Please, Sign In to add comment