Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.27 KB | None | 0 0
  1. import math
  2. import pandas
  3. from pandas import DataFrame
  4. # pandas.set_option('display.height', 5000)
  5. pandas.set_option('display.max_rows', 500)
  6. pandas.set_option('display.max_columns', 500)
  7. pandas.set_option('display.width', 180)
  8.  
  9. print("Choose function to integrate:")
  10. print(DataFrame.from_records([
  11.     ["cos(x)"],
  12.     ["x ^ 4"],
  13.     ["x ^ 3"],
  14.     ["x ^ 2"],
  15.     ["x"],
  16.     ["sin(x)"]
  17. ]))
  18.  
  19. CHOICE = int(input())
  20.  
  21. print("You choose to integrate", end=' ')
  22. if CHOICE == 0:
  23.     print("cos(x)")
  24.     f = math.cos
  25.     f1 = math.sin
  26. elif CHOICE == 1:
  27.     print("x ^ 4")
  28.     f = lambda x: x ** 4
  29.     f1 = lambda x: x ** 5 / 5
  30. elif CHOICE == 2:
  31.     print("x ^ 3")
  32.     f = lambda x: x ** 3
  33.     f1 = lambda x: x ** 4 / 4
  34. elif CHOICE == 3:
  35.     print("x ^ 2")
  36.     f = lambda x: x ** 2
  37.     f1 = lambda x: x ** 3 / 3
  38. elif CHOICE == 4:
  39.     print("x")
  40.     f = lambda x: x
  41.     f1 = lambda x: x ** 2 / 2
  42. elif CHOICE == 5:
  43.     print("sin(x)")
  44.     f = lambda x: math.sin(x)
  45.     f1 = lambda x: -math.cos(x)
  46.  
  47. print ("Enter A and B - boudaries of the segment of integration.")
  48. a, b = (lambda x: (float(x[0]), float(x[1])))(input().split())
  49.  
  50. print("Enter m - number of division segments.")
  51. m = int(input())
  52.  
  53. table_xk = [a + (b - a) * i / m  for i in range(0, m + 1) ]
  54.  
  55. # print("Generated table of x_k with values of f.")
  56. # print(DataFrame.from_records([
  57. #   ["x_k"] + table_xk,
  58. #   ["f(x_k)"] + [f(xi) for xi in table_xk],
  59. # ]))
  60.  
  61. h = (b - a) / m
  62.  
  63. def rectangular_formula(f, h, table_xk):
  64.     val = zip(table_xk, table_xk[1:])
  65.     return h * sum([ f((c + d) / 2) for c, d in val ])
  66.  
  67. def trapez_formula(f, h, table_xk):
  68.     f_val = [f(xk) for xk in table_xk]
  69.     f_val = zip(f_val, f_val[1:])
  70.     return h * sum([ (c + d) / 2 for c, d in f_val ])
  71.  
  72. def simps_formula(f, h, table_xk):
  73.     x_val = zip(table_xk, table_xk[1:])
  74.     x_val = [ [xk, (xk + xk1) / 2, xk1] for xk, xk1 in x_val ]
  75.     return h / 6 * sum([ f(c) + f(d) * 4 + f(e) for c,d,e in x_val ])
  76.  
  77. f_int = f1(b) - f1(a)
  78.  
  79. rec = rectangular_formula(f, h, table_xk)
  80. trap = trapez_formula(f, h, table_xk)
  81. simz = simps_formula(f, h, table_xk)
  82. print('Real integration value is', f_int)
  83. print('Rectangular formula gives value', rec, 'and error', abs(rec - f_int))
  84. print('Trapez formula gives value', trap, 'and error', abs(trap - f_int))
  85. print('Simps formula gives value', simz, 'and error', abs(simz - f_int))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement