VikkaLorel

inverse linear interpolation method for continuous functions

Sep 28th, 2018
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | None | 0 0
  1. from math import cos
  2.  
  3.  
  4. def tested_function(x):
  5.     return cos(x) - x
  6.  
  7.  
  8. def main():
  9.     print('[x0, x1]')
  10.     x0 = float(input('x0 = '))
  11.     x1 = float(input('x1 = '))
  12.     pivot = 0
  13.     iteration = int(input('Number of iteration ?'))
  14.     for i in range(iteration):
  15.         x2 = x1 - tested_function(x1) *\
  16.                 (x1 - x0) / (tested_function(x1) - tested_function(x0))
  17.         r = tested_function(x2)
  18.         print('{}'.format(r))
  19.         if r == 0:
  20.             print('done')
  21.             return
  22.         if tested_function(x2) * tested_function(x1) < pivot:
  23.             x0 = x2
  24.         else:
  25.             x1 = x2
  26.         print('[{}, {}]'.format(x0, x1))
  27.         print(x1)
  28.  
  29.  
  30. main()
Advertisement
Add Comment
Please, Sign In to add comment