Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from matplotlib import pyplot as plt
- from random import random
- import numpy as np
- def L(w):
- return 4 * w ** 4 + 3 * w ** 3 - 6 * w ** 2 - 3
- def pochodnia(w):
- return 16 * w ** 3 + 9 * w ** 2 - 12 * w
- w = random()
- old_w = 0
- error = 1
- precision = 5e-14
- iteration = 0
- max_iterations = 10000
- learning_rate = 0.1
- while error > precision and iteration <= max_iterations:
- old_w = w
- w = old_w - learning_rate * pochodnia(w)
- error = abs(old_w - w)
- iteration += 1
- print("Minimum L(w):", L(w))
- x = np.arange(0, 1, step=0.001)
- plt.plot(x, L(x), color='violet', label='L(w)', linewidth=1.5)
- plt.plot(x, pochodnia(x), color='blue', label='pochodnia(w)', linewidth=0.33)
- plt.title('Pochodnia i L')
- plt.grid()
- plt.legend()
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment