Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- def f(x):
- return 3*np.cos(x)**2 # Define the main function f(x)
- def centered(x,dx): #Central difference approximate (second order)
- return (f(x-h)-2*f(x)+f(x+h))/h**2
- def fdiff(x):
- return 6*np.sin(x)**2- 6*np.cos(x)**2
- N=100 #number of slices
- x_initial=0
- x_final=2*np.pi
- h=(x_final-x_initial)/float(N)
- x=np.linspace(x_initial,x_final,h)
- print(h)
- plt.plot(x, f(x),"black")
- plt.plot(x, centered (x,h),"blue")
- plt.plot(x ,fdiff(x),"red")
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement