Advertisement
F4ntomas

VMiP_2.1

Apr 10th, 2022
1,052
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.47 KB | None | 0 0
  1. # Lab2 variant 17
  2. from cmath import exp
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5.  
  6. def F(x):
  7.     """Возращает первообразную"""
  8.     #result = x - np.log(np.exp(x) + 1)
  9.     result = -(np.exp(x)/(np.exp(2 * x) + 2 * np.exp(x) + 1))
  10.     return abs(result)
  11.  
  12. def f(x) -> float:
  13.     return np.exp(-x)/(1+np.exp(-x))
  14.  
  15.  
  16.  
  17. fig, ax = plt.subplots()
  18. x = np.linspace(0,np.pi,10)
  19. y = np.exp(-x)/(1+np.exp(-x))
  20. eps = 0.000001
  21. a = 0
  22. b = np.pi
  23. ax = ax.plot(x, y, "b-")
  24. ax = plt.gca()
  25. ax.spines['left'].set_position("center")
  26. ax.spines['bottom'].set_position('center')
  27. ax.spines['top'].set_visible(False)
  28. ax.spines['right'].set_visible(False)
  29. ax.set_xlabel("Y", fontsize=15, color='blue', labelpad=120)    # +
  30. ax.set_ylabel("X", fontsize=15, color='orange', labelpad=140, rotation=0)
  31. plt.show()
  32. """# кол-во разбиений
  33. n = 100
  34. # шаг
  35. h = (b - a) / n
  36. sum = 0
  37. for i in range(1, n + 1):
  38.    xi = a + i * h
  39.    sum += f(xi)
  40. sum = h * sum
  41. print(f'Интеграл = {sum}')"""
  42. # Найдем шаг разбиения
  43. h1 = F(a)
  44. h2 = F(b)
  45. if h1 >= h2:
  46.     max = h1
  47. else:
  48.     max = h2
  49. #print('max = ', max)
  50.  
  51. n = 1
  52. #print(max * ((b-a)**2)/(2*n))
  53. while (max * ((b-a)**2)/(2*n) >= eps):
  54.     n += 1
  55. print(f'Количество разбиений при точности {eps}; n = {n}')
  56. h = (b - a) / n
  57. sum = 0
  58. for i in range(1, n + 1):
  59.     xi = a + i * h
  60.     sum += f(xi)
  61. sum = h * sum
  62. print(f'Интеграл = {sum:.5f}')
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement