{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lec10_derivatives2

# lec10_derivatives2 - from pylab import#Central Difference...

This preview shows pages 1–2. Sign up to view the full content.

from pylab import * #Central Difference Approximation def cda(f,x,h): return (f(x+h)-f(x-h))/(2*h) ##Forward difference approximation def fda(f,x,h): ''' Computes the derivative of f wrt x with interval of h using the forward difference approximation (fda). ''' return ( f(x+h) - f(x) )/h #Backward difference approximation def bda(f,x,h): ''' Computes the derivative of f wrt x with interval of h using the backward difference approximation (fda). ''' return ( f(x) - f(x-h) )/h #Second order derivative def cda2(f,x,h): return ( f(x-h) - 2*f(x) + f(x+h) )/(h**2) ##Evaluating a derivative of an analytic function over a range ##CDA, etc just become vectorized functions #x=linspace(-np.pi,np.pi,100) #def f(x): return 2*sin(2*x) #dfdx=cda(f,x,0.1) #df2dx2=cda2(f,x,0.1) #dfdx_true=4.*cos(2*x) # #plot(x,f(x),'b-',lw=2,label='Function') #plot(x,dfdx_true,'r-',lw=2,label='1st Derivative (True)') #plot(x,dfdx,'k-',label='1st Derivative (CDA)') #plot(x,df2dx2,'g-',lw=2,label='2nd Derivative (CDA)') #legend(loc=2) #title('Derivatives of: $f(x)=2\sin(2x)$',fontsize=20) #grid() #ylim(-10,10) #Evaluating the derivative over a mesh of points

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}