<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/env python

# Jonathan Senning &lt;jonathan.senning@gordon.edu&gt;
# Gordon College
# January 1999
# Converted to Python May 2008
# Animation added January 2015
#
# This python program does essentially the same thing as the sequence
# of MatLab commands given on page 25 of "Numerical Mathematics and
# Computing", 4th edition, by Cheney and Kincaid, Brooks/Cole, 1999.

from __future__ import print_function
#from pylab import *
import numpy as np
import matplotlib.pyplot as plt
from math import factorial, pi
import time

plt.ion()

x = np.pi * np.arange( -400, 401 ) / 100.0
y = []
y.append( np.zeros( len( x ) ) )
plt.plot( x, np.sin( x ), 'b-' )
plt.axis( [x[0], x[-1], -3, 3] )
plt.xlabel( '$x$' )
plt.ylabel( '$y$' )
plt.title( 'Partial sums of the Taylor series for $\sin(x)$' )
legend_list = [ '$y = \sin(x)$' ]
plt.legend( tuple( legend_list ), loc='upper center' )
plt.draw()

print( "starting animation..." )

for k in range( 0, 12 ):
    time.sleep( 5 )
    y.append( y[-1] + ((-1)**k) * (x**(2*k+1)) / factorial( 2*k+1 ) )
    plt.ioff()
    plt.cla()
    plt.plot( x, np.sin( x ), 'b-', x, y[k+1], 'r-' )
    plt.axis( [x[0], x[-1], -3, 3] )
    plt.xlabel( '$x$' )
    plt.ylabel( '$y$' )
    plt.title( 'Partial sums of the Taylor series for $\sin(x)$' )
    legend_list = [ '$y = \sin(x)$', '$y = T_{%d}(x)$' % (2*k+1) ]
    plt.legend( tuple( legend_list ), loc='upper center' )
    plt.draw()
    plt.ion()
    print( "k = %d" % k )

raw_input( "Press Enter to quit... " )

# End of File
</pre></body></html>