Advertisement
vovkakorben

Untitled

Nov 12th, 2021
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.08 KB | None | 0 0
  1. import math
  2.  
  3. def f(x):
  4.     return math.sqrt(0.2*x)*math.cos(0.02*x)
  5.  
  6. def left_rect(fx: list, dx: float):
  7.     s = 0.0
  8.     for i in range(0, len(fx)-1):
  9.         s += fx[i]
  10.     return s*dx
  11.  
  12. def right_rect(xi: list, dx: float):
  13.     return 0
  14.     s = 0.0
  15.     for i in range(1, len(fx)-1):
  16.         s += fx[i]
  17.     return s*dx
  18.  
  19. def trapezoid(xi: list, dx: float):
  20.     return 0
  21.     s = 0.0
  22.     for i in range(0, len(fx)-2):
  23.         s += (fx[i]+fx[i+1])/2
  24.     return s*dx
  25.  
  26. def simpson(xi: list, dx: float):
  27.     return 0
  28.     s = 0.0
  29.     for x in xi:
  30.         s += dx/6 * ( f(x) + 4*f(x+dx/2) + f(x+dx) )
  31.     return s*dx
  32.  
  33. iterations = [4,20,100,400]
  34. xmin = 1
  35. xmax = 3
  36.  
  37. print('\n\tleft\t\tright\t\ttrapezoid\tsimpson')
  38. fx = []
  39. for i in range(len(iterations)):
  40.     fx.clear
  41.     n = iterations[i]
  42.     dx = (xmax - xmin)/n
  43.     x = xmin
  44.     for i in range(n+1):
  45.         fx.append(f(x))
  46.         x += dx
  47.    
  48.     #print(left_rect(fx,dx))
  49.     #print(n,' --- ',xi)
  50.     print(f'n={n}\t{left_rect(fx,dx):.10f}\t{right_rect(fx,dx):.10f}\t{trapezoid(fx,dx):.10f}\t{simpson(fx,dx):.10f}')
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement