Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- import numpy as np
- def F(x):
- return x**3 + 4*x**2 + 2*x + 1
- def Par(a, b, eps, x, y):
- #метод парабол
- x1 = float(a)
- x2 = (a + b) / 2
- x3 = float(b)
- while not (x3 - x1) < eps:
- if x3 != x2 != x1 and x2 != x1:
- c1 = (F(x2) - F(x1)) / (x2 - x1)
- c2 = ((F(x3) - F(x1)) / (x3 - x1) - (F(x2) - F(x1)) / (x2 - x1)) / (x3 - x2)
- x_temp = (x1 + x2 - c1 / c2) / 2
- x_list = [x1, x2, x3, x_temp]
- x_list.sort()
- for i in range(2):
- if F(x_list[i]) >= F(x_list[i+1]) and F(x_list[i+1]) <= F(x_list[i+2]):
- x1 = x_list[i]
- x2 = x_list[i+1]
- x3 = x_list[i+2]
- print("Метод парабол: \nx* = ", (x1 + x3) / 2, "\ny_min = ", F((x1 + x3) / 2), '\n')
- def main():
- a = -1
- b = 1
- eps = 0.01
- x = []
- y = []
- for i in np.arange(a, b, eps/10):
- x.append(i)
- y = [F(i) for i in x]
- plt.plot(x, y)
- plt.grid()
- plt.suptitle("Метод парабол")
- plt.show()
- Par(a, b, eps, x, y)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement