# # 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. # #----- partial_sum = function(N=100){ # N = how many terms to sum from this series ti = 2 # the term index (start at the second term) S = 1 # the sum of all terms term_sign = -1 number_of_terms_summed = 0 number_of_terms_to_sum_before_changing_sign = 1 #all_terms = c(S) while( ti <= N ){ term = ( 1/ti ) * term_sign #all_terms = c( all_terms, 1/term ) # for debugging lets make sure that our terms look correct S = S + term ti = ti+1 number_of_terms_summed = number_of_terms_summed + 1 if( number_of_terms_summed >= number_of_terms_to_sum_before_changing_sign ){ term_sign = -1 * term_sign number_of_terms_to_sum_before_changing_sign = number_of_terms_to_sum_before_changing_sign + 1 number_of_terms_summed = 0 } } return(S) } # Lets plot the sequence of partial sums as some value: # #postscript("../WriteUp/Graphics/exercise_4_1_part_k_sequence_of_partial_sums.eps", onefile=FALSE, horizontal=FALSE) ns = 10^(1:7) ss = c() for( n in ns ){ S = partial_sum(n) ss = c( ss, S ) } plot( log(ns,10), ss, type='b', pch=20, cex=1.5, xlab='p: Number of terms=10^p', ylab='Partial Sum of 10^p terms' ) grid() #dev.off()