Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from numpy import arange, cos, sin
- import matplotlib.pyplot as plt
- # A() oblicza przybliżenie pochodnej ze wzoru z podpkt a)
- def A(h = 0.005):
- def derivatives(f, x, h):
- df = (f(x + h) - f(x)) / h
- print("A) Dhf(x) = ", df)
- return df;
- x = 0.3
- dif = abs(derivatives(cos, x, h) + sin(x))
- print(" |Dhf(x) - f'(x)| = ", dif)
- return dif;
- # B() oblicza przybliżenie pochodnej ze wzoru z podpkt b)
- def B(h = 0.005):
- def derivatives(f, x, h):
- df = (f(x + h) - f(x - h)) / 2 * h
- print("B) Dhf(x) = ", df)
- return df;
- x = 0.3
- dif = abs(derivatives(cos, x, h) + sin(x))
- print(" |Dhf(x) - f'(x)| = ", dif)
- return dif;
- # Rysuję wykres
- h = arange(-2.0, 2.0, 0.1)
- plt.plot(h, A(h), 'o-', h, B(h), '^-')
- plt.xlabel('h')
- plt.legend(('Błąd przybliżenia a)', 'Błąd przybliżenia b)'), loc = 0)
- plt.grid(True)
- plt.savefig('plot.png', format = 'png')
- plt.show()
- input("\nPress return to exit.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement