Advertisement
Guest User

QU

a guest
Feb 23rd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.53 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. def f(x):
  5.     return 3*np.cos(x)**2 # Define the main function f(x)
  6.  
  7.  
  8. def centered(x,dx): #Central difference approximate (second order)
  9.     return (f(x-h)-2*f(x)+f(x+h))/h**2
  10.  
  11. def fdiff(x):
  12.     return 6*np.sin(x)**2- 6*np.cos(x)**2
  13.  
  14. N=100 #number of slices
  15. x_initial=0
  16. x_final=2*np.pi
  17. h=(x_final-x_initial)/float(N)
  18. x=np.linspace(x_initial,x_final,h)
  19.  
  20. print(h)
  21.  
  22. plt.plot(x, f(x),"black")
  23. plt.plot(x, centered (x,h),"blue")
  24. plt.plot(x ,fdiff(x),"red")
  25. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement