Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Lab2 variant 17
- from cmath import exp
- import matplotlib.pyplot as plt
- import numpy as np
- def F(x):
- """Возращает первообразную"""
- #result = x - np.log(np.exp(x) + 1)
- result = -(np.exp(x)/(np.exp(2 * x) + 2 * np.exp(x) + 1))
- return abs(result)
- def f(x) -> float:
- return np.exp(-x)/(1+np.exp(-x))
- fig, ax = plt.subplots()
- x = np.linspace(0,np.pi,10)
- y = np.exp(-x)/(1+np.exp(-x))
- eps = 0.000001
- a = 0
- b = np.pi
- ax = ax.plot(x, y, "b-")
- ax = plt.gca()
- ax.spines['left'].set_position("center")
- ax.spines['bottom'].set_position('center')
- ax.spines['top'].set_visible(False)
- ax.spines['right'].set_visible(False)
- ax.set_xlabel("Y", fontsize=15, color='blue', labelpad=120) # +
- ax.set_ylabel("X", fontsize=15, color='orange', labelpad=140, rotation=0)
- plt.show()
- """# кол-во разбиений
- n = 100
- # шаг
- h = (b - a) / n
- sum = 0
- for i in range(1, n + 1):
- xi = a + i * h
- sum += f(xi)
- sum = h * sum
- print(f'Интеграл = {sum}')"""
- # Найдем шаг разбиения
- h1 = F(a)
- h2 = F(b)
- if h1 >= h2:
- max = h1
- else:
- max = h2
- #print('max = ', max)
- n = 1
- #print(max * ((b-a)**2)/(2*n))
- while (max * ((b-a)**2)/(2*n) >= eps):
- n += 1
- print(f'Количество разбиений при точности {eps}; n = {n}')
- h = (b - a) / n
- sum = 0
- for i in range(1, n + 1):
- xi = a + i * h
- sum += f(xi)
- sum = h * sum
- print(f'Интеграл = {sum:.5f}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement