source('utils.R') # 3.3-2 (a): # xs = seq( 1, 1.5, length.out=100 ) # our interval I fs = xs^3 - xs - 1 true_roots = polyroot( c( -1, -1, 0, 1 ) ) truth = Re(true_roots[3]) par(mfrow=c(1,2)) plot( xs, fs, type='l', xlab='x', ylab='f(x)') abline(h=0, col='black') grid() #gs = xs^3 - 1 gs = ( xs + 1 )^(1/3) plot( xs, gs, type='l', xlab='x', ylab='g(x)') abline(h=0, col='black') grid() par(mfrow=c(1,1)) g_prime_max = 1 / ( 3*(2^(2/3)) ) root_fn = function(x){ x^3 - x - 1 } iter_fn = function(x){ (x+1)^(1/3) } res = fixed_point_iteration(1.25, root_fn, iter_fn) print(sprintf('Part (a): Truth: %f; FP iteration: %f', truth, res[1])) # 3.3-2 (b): # xs = seq( 4.45, 4.55, length.out=100 ) # our interval I fs = xs - tan(xs) par(mfrow=c(1,2)) plot( xs, fs, type='l', xlab='x', ylab='f(x)') abline(h=0, col='black') grid() gs = atan(xs) + pi plot( xs, gs, type='l', xlab='x', ylab='g(x)') abline(h=0, col='black') grid() par(mfrow=c(1,1)) g_prime_max = 1 / ( 1 + min(xs)^2 ) root_fn = function(x){ x - tan(x) } iter_fn = function(x){ atan(x) + pi } res = fixed_point_iteration(4.5, root_fn, iter_fn) print(sprintf('Part (b); FP iteration: %f', res[1])) # 3.3-2 (c): # xs = seq( 0.5, 2, length.out=100 ) # our interval I fs = exp(-xs) - cos(xs) par(mfrow=c(1,2)) plot( xs, fs, type='l', xlab='x', ylab='f(x)') abline(h=0, col='black') grid() gs = acos(exp(-xs)) plot( xs, gs, type='l', xlab='x', ylab='g(x)') abline(h=0, col='black') grid() par(mfrow=c(1,1)) g_prime_max = exp(-min(xs)) / sqrt( 1 - exp(-2*min(xs))) root_fn = function(x){ exp(-x) - cos(x) } iter_fn = function(x){ acos(exp(-x)) } res = fixed_point_iteration(4.5, root_fn, iter_fn) print(sprintf('Part (c); FP iteration: %f', res[1])) # 3.3-4: # a = 0.5 xs = seq( -5, +5, length.out=100 ) gs = 2*xs - 2*a*xs^2 plot(xs, gs, type='l', xlab='x', ylab='g(x)') grid()