Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- plt.close()
- eps=10**(-7)
- def myFunct(x):
- return(np.sin(x)+x**2/50+x/4)
- def myFunctDerivative(x):
- return(np.cos(x)+x/25+1/4)
- def plotDerivative(x):
- dx=1
- m=myFunctDerivative(x)
- dy=m
- y=myFunct(x)
- # plt.plot([x-dx, x, x+dx],[y-dy, y, y+dy])
- plt.plot(x,y,'md')
- plt.plot([x-dx, x],[y-dy, y])
- x=np.arange(-10,10,.1)
- y=myFunct(x)
- plt.plot(x,y)
- plt.grid(True)
- x0=1.5
- y0=myFunct(x0)
- plotDerivative(x0)
- c=.3
- eps=10**(-4)
- grad=myFunctDerivative(x0)
- while np.abs(grad)>eps:
- x0=x0-c*grad
- plotDerivative(x0)
- plt.plot([x0],[0],'.r')
- grad=myFunctDerivative(x0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement