<jupyter_start><jupyter_text>False position method![equation](http://1.bp.blogspot.com/-OGqfrUfX01c/VHlQ5C6oZWI/AAAAAAAAACU/zZy2erBUrTw/s1600/Met%2BFalsa%2BPos%2B2.jpg)<jupyter_code>import math
def false_position(eq, low, up, error=0.00001):
  
  if eq(low) * eq(up) > 0:
    raise Exception('False position not possible')  
  
  prev_Xr, Xr = 0, 0
  Xl, Xu = low, up
  Ea = 100
  
  i = 1
  while( Ea > error ):
    prev_Xr = Xr
    
    FnL, FnU = eq(Xl), eq(Xu)
    Xr = Xu - ( ( FnU * ( Xl - Xu ) ) / (FnL - FnU) )
    f = eq(Xr)
    
    Ea = math.fabs(( ( Xr - prev_Xr ) / Xr ) * 100)
    
    if f * FnL > 0: 
      Xl = Xr
    else: 
      Xu = Xr
    
    print("%i | %f | %f | %f | %f | %f | %f" % (i, Xl, Xu, Xr, FnL, f, Ea))
    
    i += 1
def fn(x):
 return (2 * x) * math.cos(2 * x) - (x + 1) ** 2
method = false_position(fn, -3, -2)<jupyter_output>1 | -3.000000 | -2.141933 | -2.141933 | -9.761022 | 0.476053 | 100.000000
2 | -3.000000 | -2.181836 | -2.181836 | -9.761022 | 0.094300 | 1.828851
3 | -3.000000 | -2.189664 | -2.189664 | -9.761022 | 0.016463 | 0.357522
4 | -3.000000 | -2.191029 | -2.191029 | -9.761022 | 0.002801 | 0.062273
5 | -3.000000 | -2.191261 | -2.191261 | -9.761022 | 0.000474 | 0.010590
6 | -3.000000 | -2.191300 | -2.191300 | -9.761022 | 0.000080 | 0.001793
7 | -3.000000 | -2.191307 | -2.191307 | -9.761022 | 0.000014 | 0.000303
8 | -3.000000 | -2.191308 | -2.191308 | -9.761022 | 0.000002 | 0.000051
9 | -3.000000 | -2.191308 | -2.191308 | -9.761022 | 0.000000 | 0.000009
{'fraction_non_alphanumeric': 0.19762062128222074, 'fraction_numerical': 0.2822207534699273, 'mean_word_length': 2.180672268907563, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '564867', 'license': 'MIT', 'n_tokens_mistral': 1013, 'n_tokens_neox': 764, 'n_words': 155, 'path': 'solving_equations/false_position/false_position.ipynb', 'repo_name': 'miltonolaf/snippets.ipynb', 'size': '1513'}