{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

hw3_soln - sum =-1(term 1*x*n/float(fact(n term =1 return...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
#Solutions to HW03 by JRG from pylab import * def fact(n): ''' Recursively computes n! = n(n-1)(n-2)(n-3). ..(1) ''' nbang=n if n==0: return 1 nbang*=fact(n-1) return nbang def taylorSin(x,N): ''' Approximates sin(x) using a Taylor series expanion up to the Nth term. ''' sum=0.0 term=1 for n in range(1,N+1,2):
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: sum+=(-1)**(term+1)*x**n/float(fact(n)) term+=1 return sum #Main Program x=linspace(-np.pi,np.pi,100) plot(x,sin(x),'k-',label='Exact',lw=3) for ni in [3,5,7,9]: plot(x,taylorSin(x,ni),label='N=%i'%ni) ylim(1.2,-1.2) xlabel('X') ylabel('Sin(x)') legend()...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online