lec10_derivatives2

lec10_derivatives2 - from pylab import * #Central...

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

View Full Document Right Arrow Icon
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)
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/05/2010 for the course PHYS phy503 taught by Professor Gladden during the Spring '09 term at Ole Miss.

Page1 / 2

lec10_derivatives2 - from pylab import * #Central...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online