Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- from math import pi
- from math import cos
- from math import sqrt
- import matplotlib.pyplot as plt
- a = 0
- b = 1.5
- h = 0.15
- countStep = int((b - a) / h + 1)
- eps = 10 ** (-6)
- arrX = np.arange(a, b + h, h)
- #ТАБУЛИРОВАНИЕ
- def tab(x):
- aN = x
- sum = aN
- i = 0
- while (abs(aN) > eps):
- aN *= - (((pi ** 2 ) * (4*i + 1) * (x**4)) / (4 * (2*i + 1) * (2*i + 2) * (4*i + 5)))
- i += 1
- sum += aN
- return(sum)
- f = [None] * int((b - a) / h + 1)
- for i in range(len(arrX)):
- arrX[i] = round(arrX[i], 5)
- f[i] = tab(arrX[i])
- #ФУНКЦИЯ
- def func(x):
- return cos(pi * x ** 2 / 2)
- #ЛЕВЫЕ
- print('Квадратурная формула левых прямоугольников')
- def Pr(x, n):
- sum = 0
- hN = (x - a) / n
- i = 0
- for j in range(n + 1):
- sum += func(i) * hN
- i += hN
- return sum
- for i in range(countStep):
- n = 2
- while True:
- In = Pr(arrX[i], n)
- I2n = Pr(arrX[i], 2*n)
- n *= 2
- if (abs(I2n - In) <= eps or n > 1024):
- break
- n = int(n / 2)
- #if (abs(I2n - In) > eps):
- print('{} {:<5} {:>10} {:<20} {:>5} {:<20} {:>15} {:<25} {:>15} {}'.format('x =', arrX[i], 'C(x) =', f[i], 'I =', Pr(arrX[i],n), 'Разность =', abs(f[i] - Pr(arrX[i], n)), 'n =', n))
- print()
- #ЦЕНТРАЛЬНЫХ
- print('Квадратурная формула центральных прямоугольников')
- def Centr(x, n):
- sum = 0
- hN = (x - a) / n
- i = hN
- for j in range(n):
- sum += func((2 * i - hN) / 2) * hN
- i += hN
- return sum
- for i in range(countStep):
- n = 2
- while True:
- In = Centr(arrX[i], n)
- I2n = Centr(arrX[i], 2*n)
- n *= 2
- if (abs(I2n - In) <= eps or n > 1024):
- break
- n = int(n / 2)
- print('{} {:<5} {:>10} {:<20} {:>5} {:<20} {:>15} {:<25} {:>15} {}'.format('x =', arrX[i], 'C(x) =', f[i], 'I =', Centr(arrX[i],n), 'Разность =', abs(f[i] - Centr(arrX[i], n)), 'n =', n))
- print()
- #ТРАПЕЦИЙ
- print('Квадратурная формула трапеций')
- def Trap(x,n):
- sum = 0
- hN = (x - a) / n
- i = hN
- for j in range(n):
- sum += ((func(i) + func(i - hN)) * (hN / 2))
- i += hN
- return sum
- for i in range(countStep):
- n = 2
- while True:
- In = Trap(arrX[i], n)
- I2n = Trap(arrX[i], 2*n)
- n *= 2
- if (abs(I2n - In) <= eps or n > 1024):
- break
- n = int(n / 2)
- print('{} {:<5} {:>10} {:<20} {:>5} {:<20} {:>15} {:<25} {:>15} {}'.format('x =', arrX[i], 'C(x) =', f[i], 'I =', Trap(arrX[i],n), 'Разность =', abs(f[i] - Trap(arrX[i], n)), 'n =', n))
- print()
- #СИМПСОНА
- print('Квадратурная формула Симпсона')
- def Simps(x,n):
- sum = 0
- hN = (x - a) / n
- i = hN
- for j in range(n):
- sum += (func(i) + 4 * func((2 * i - hN) / 2) + func(i - hN)) * hN/6
- i += hN
- return sum
- for i in range(countStep):
- n = 2
- while True:
- In = Simps(arrX[i], n)
- I2n = Simps(arrX[i], 2*n)
- n *= 2
- if (abs(I2n - In) <= eps or n > 1024):
- break
- n = int(n / 2)
- print('{} {:<5} {:>10} {:<20} {:>5} {:<20} {:>15} {:<25} {:>15} {}'.format('x =', arrX[i], 'C(x) =', f[i], 'I =', Simps(arrX[i],n), 'Разность =', abs(f[i] - Simps(arrX[i], n)), 'n =', n))
- print()
- #ГАУССА
- print('Квадратурная формула Гаусса')
- def Gaus(x,n):
- sum = 0
- hN = (x - a) / n
- i = 0
- for j in range(n):
- sum += (func((i) + ((hN / 2) * (1 - 1 / sqrt(3)))) + func((i) + ((hN / 2) * (1 + 1 / sqrt(3))))) * hN / 2
- i += hN
- return sum
- for i in range(countStep):
- n = 2
- while True:
- In = Gaus(arrX[i], n)
- I2n = Gaus(arrX[i], 2*n)
- n *= 2
- if (abs(I2n - In) <= eps or n > 1024):
- break
- n = int(n / 2)
- print('{} {:<5} {:>10} {:<20} {:>5} {:<20} {:>15} {:<25} {:>15} {}'.format('x =', arrX[i], 'C(x) =', f[i], 'I =', Gaus(arrX[i],n), 'Разность =', abs(f[i] - Gaus(arrX[i], n)), 'n =', n))
- print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement