Guest User

Untitled

a guest
Apr 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. import numpy as np
  2.  
  3. # NORMAL YONTEM
  4. # f'(x) = [f(x+h) - f(x)] / h
  5.  
  6. func = lambda x: np.sin(x)
  7.  
  8. def derivate(func, x, h=1e-12):
  9. return (func(x+h) - func(x))/(h)
  10.  
  11. results = []
  12.  
  13. for h_ in range(30):
  14. results.append(derivate(func, 1.2, h= np.power(10.,-h_)))
  15.  
  16. print results
  17. # [-0.1235426821476362, 0.3151909944996667, 0.3576915586159579,
  18. # 0.36189167457956195, 0.36231115191909247, 0.3623530942853392,
  19. # 0.36235728850808613, 0.36235770828341174, 0.3623577549127788,
  20. # 0.36235781042393, 0.36235792144623247, 0.36236569300740484,
  21. # 0.3624878175401136, 0.3630429290524262, 0.36637359812630166,
  22. # 0.44408920985006256, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
  23.  
  24. # DEGISTIRILMIS YONTEM
  25. results = []
  26.  
  27. # [ 2 * cos(x + h/2) * sin(h/2) ] / 2
  28. for h_ in range(30):
  29. h= np.power(10.,-h_)
  30. results.append( (2 * np.cos(1.2 + h/2) * np.sin(h/2) ) / h )
  31.  
  32. print results
  33. # [-0.12354268214763613, 0.31519099449966614, 0.3576915586159585, 0.36189167457956894,
  34. # 0.3623111519184844, 0.36235309427520446, 0.36235728845707016, 0.3623577078747188,
  35. # 0.3623577498164782, 0.36235775401065407, 0.3623577544300717, 0.3623577544720134,
  36. # 0.36235775447620755, 0.36235775447662705, 0.3623577544766688, 0.36235775447667323,
  37. # 0.3623577544766736, 0.36235775447667357, 0.3623577544766736,0.3623577544766736,
  38. # 0.3623577544766736, 0.3623577544766736, 0.3623577544766736, 0.3623577544766736,
  39. # 0.3623577544766736, 0.3623577544766736, 0.3623577544766736, 0.3623577544766736,
  40. # 0.3623577544766736, 0.3623577544766736]
Add Comment
Please, Sign In to add comment