source('utils.R') # # For verification we duplicate the numbers found in the table on EPage 113: # # Generate the iterates x_n given in the text: # iter_fn = function(x){ (0.1 + tan(x))/1.5 } x0 = 0.0 n = 30 xn = rep(NA, n+1) xn[1] = x0 # =x_0 for( k in 2:(n+1) ){ xn[k] = iter_fn(xn[k-1]) } a_res = aitkens_delta2(xn) T = cbind( seq(0, n), xn, a_res$xhatn, a_res$rn ) colnames(T) = c('n', 'x_n', 'x_hat_n', 'r_n') head(T) tail(T) # Exercise 3.4-2: # # Generate the iterates given in the text: # root_fn = function(x){ 0.5 - x + 0.2 * sin(x) } iter_fn = function(x){ 0.5 + 0.2 * sin(x) } x0 = 0.5 n = 10 xn = rep(NA, n+1) xn[1] = x0 for( k in 2:(n+1) ){ xn[k] = iter_fn(xn[k-1]) } a_res = aitkens_delta2(xn) T = cbind( seq(0, n), xn, a_res$xhatn, a_res$rn ) colnames(T) = c('n', 'x_n', 'x_hat_n', 'r_n') print(T) g_prime_xi = 1/a_res$rn[n] print(g_prime_xi) # Exercise 3.4-3: # x0 = 0.5 res = steffensen_iteration(x0, iter_fn) print(sprintf('Steffensens on g(x) = 0.5 + 0.2 sin(x) gives: x= %10f n=%3d', res[1], res[2])) # Exercise 3.4-4: # # Generate the iterates given in the text: # iter_fn = function(x){ sqrt(2+x) } x0 = 0.0 n = 5 xn = rep(NA, n+1) xn[1] = x0 for( k in 2:(n+1) ){ xn[k] = iter_fn(xn[k-1]) } a_res = aitkens_delta2(xn) T = cbind( seq(0, n), xn, a_res$xhatn, a_res$rn ) colnames(T) = c('n', 'x_n', 'x_hat_n', 'r_n') print(T)