# # Written by: # -- # John L. Weatherwax 2009-04-21 # # email: wax@alum.mit.edu # # Please send comments and especially bug reports to the # above email address. # #----- kth_derivative_of_f = function(x,k){ stopifnot( k>=0 ) if( k==0 ){ return( x^(1/3) ) }else if ( k==1 ){ return( x^(-2/3)/3 ) }else { numer = 1 term = 2 while( term <= 3*k-4 ){ numer = numer * term term = term + 3 } denom = 3^k return( ( (-1)^(k-1) * numer / denom ) * x^( -(3*k-1)/3 ) ) } } # Print the Taylor series terms: # taylor_terms = c() for( k in 0:5 ){ tt = (-1)^k * kth_derivative_of_f( 8, k )/factorial(k) # note that (x-x_0)^k = (7-8)^k = (-1)^k taylor_terms = c( taylor_terms, tt ) print( sprintf( "k= %5d, derivative= %10.6f", k, tt ) ) } print( "taylor_terms= " ) print( taylor_terms ) print( "cumsum(taylor_terms)= " ) print( cumsum(taylor_terms) )