Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. plt.close()
  4.  
  5. eps=10**(-7)
  6.  
  7. def myFunct(x):
  8. return(np.sin(x)+x**2/50+x/4)
  9.  
  10. def myFunctDerivative(x):
  11. return(np.cos(x)+x/25+1/4)
  12.  
  13. def plotDerivative(x):
  14. dx=1
  15. m=myFunctDerivative(x)
  16. dy=m
  17. y=myFunct(x)
  18. # plt.plot([x-dx, x, x+dx],[y-dy, y, y+dy])
  19. plt.plot(x,y,'md')
  20. plt.plot([x-dx, x],[y-dy, y])
  21.  
  22. x=np.arange(-10,10,.1)
  23. y=myFunct(x)
  24. plt.plot(x,y)
  25. plt.grid(True)
  26.  
  27. x0=1.5
  28.  
  29. y0=myFunct(x0)
  30. plotDerivative(x0)
  31.  
  32. c=.3
  33. eps=10**(-4)
  34.  
  35. grad=myFunctDerivative(x0)
  36.  
  37. while np.abs(grad)>eps:
  38. x0=x0-c*grad
  39. plotDerivative(x0)
  40. plt.plot([x0],[0],'.r')
  41. grad=myFunctDerivative(x0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement