PNiewiarowska

AI_Niewiarowska_lab6_2

Dec 10th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. from matplotlib import pyplot as plt
  2. from random import random
  3. import numpy as np
  4.  
  5. def L(w):
  6.     return 4 * w ** 4 + 3 * w ** 3 - 6 * w ** 2 - 3
  7.  
  8. def pochodnia(w):
  9.     return 16 * w ** 3 + 9 * w ** 2 - 12 * w
  10.  
  11. w = random()
  12. old_w = 0
  13. error = 1
  14. precision = 5e-14
  15. iteration = 0
  16. max_iterations = 10000
  17. learning_rate = 0.1
  18.  
  19. while error > precision and iteration <= max_iterations:
  20.     old_w = w
  21.     w = old_w - learning_rate * pochodnia(w)
  22.     error = abs(old_w - w)
  23.     iteration += 1
  24.  
  25. print("Minimum L(w):", L(w))
  26.  
  27. x = np.arange(0, 1, step=0.001)
  28.  
  29. plt.plot(x, L(x), color='violet', label='L(w)', linewidth=1.5)
  30. plt.plot(x, pochodnia(x), color='blue', label='pochodnia(w)', linewidth=0.33)
  31.  
  32. plt.title('Pochodnia i L')
  33. plt.grid()
  34. plt.legend()
  35. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment