101prairiedogs

Graphing a Function and its Derivative

Oct 30th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.48 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. from scipy.misc import derivative as D
  3.  
  4. f = lambda x: x**2 # <--- INSERT FUNCTION AFTER THE COLON HERE
  5.  
  6. der = lambda x: D(f, x)
  7.  
  8. def main():
  9.     r = range(-30, 30) # This is the X-Range that the graph will be shown on
  10.  
  11.     x = [i for i in r]
  12.     f1 = [f(i) for i in r]
  13.     f2 = [der(i) for i in r]
  14.    
  15.     plt.plot(x, f1, label='Original')
  16.     plt.plot(x, f2, label='Derivative')
  17.     plt.grid()
  18.     plt.legend()
  19.     plt.show()
  20.  
  21. main()
Add Comment
Please, Sign In to add comment