Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | None | 0 0
  1. from numpy import arange, cos, sin
  2. import matplotlib.pyplot as plt
  3.  
  4. # A() oblicza przybliżenie pochodnej ze wzoru z podpkt a)
  5. def A(h = 0.005):
  6.     def derivatives(f, x, h):
  7.         df = (f(x + h) - f(x)) / h
  8.         print("A) Dhf(x) = ", df)
  9.         return df;
  10.     x = 0.3
  11.     dif = abs(derivatives(cos, x, h) + sin(x))
  12.     print(" |Dhf(x) - f'(x)| = ", dif)
  13.     return dif;
  14.  
  15. # B() oblicza przybliżenie pochodnej ze wzoru z podpkt b)
  16. def B(h = 0.005):
  17.     def derivatives(f, x, h):
  18.         df = (f(x + h) - f(x - h)) / 2 * h
  19.         print("B) Dhf(x) = ", df)
  20.         return df;
  21.     x = 0.3
  22.     dif = abs(derivatives(cos, x, h) + sin(x))
  23.     print(" |Dhf(x) - f'(x)| = ", dif)
  24.     return dif;
  25.  
  26. # Rysuję wykres
  27. h = arange(-2.0, 2.0, 0.1)
  28. plt.plot(h, A(h), 'o-', h, B(h), '^-')
  29. plt.xlabel('h')
  30. plt.legend(('Błąd przybliżenia a)', 'Błąd przybliżenia b)'), loc = 0)
  31. plt.grid(True)
  32. plt.savefig('plot.png', format = 'png')
  33. plt.show()
  34. input("\nPress return to exit.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement