atol = 1e-5 output = True for i in range(4): #-------------------------------------------------------------------------- # Print divider for readability divider() # State Subproblem letter print("Problem {}:".format(index[i])) print("fixed_point()") # Start time tstart = time.time() x1, its1 = fixed_point(gfunc[i], x0, maxit, rtol, atol, output) # Stop time ttot = time.time() - tstart # Record solution print(" final solution = ", x1, ", residual = ", np.abs(gfunc[i](x1)), ", time = ", ttot, ", its = ", its1) #-------------------------------------------------------------------------- print("aitken_fp()") # Start time tstart = time.time() x2, its2 = aitken_fp(gfunc[i], x0, maxit, rtol, atol, output) # Stop time
import numpy as np from fixed_point import * def f(x): return np.exp(-x) x0 = 0.0 xroot, _ = fixed_point(f, x0, verbose=True) print(xroot)
# interp_representation_error interp_representation_error = lambda Gfun, x_final: abs( Gfun(x_final) - x_final) # run trials for name in order: Gfun = fixed_point_functions[name]['method'] x0 = fixed_point_functions[name]['initial guess'] if SHOW_OUTPUT: logger.info( "\n\n\tRunning fixed point iterations WITHOUT aitken acceleration on problem %s.\n\n\tFunction %s.\n" % (name, name)) x_final = fixed_point(Gfun, x0, maxit, rtol, atol, output=SHOW_OUTPUT) if SHOW_OUTPUT: # display interp_representation_error error = interp_representation_error(Gfun, x_final) logger.info( "\n\n[Method 2]: \nFinal fixed point target error WITHOUT using acceleration acceleration.\n \ \n\tFunction name: %s, \n\n\t\t |Gfun(x) - x| = %s.\n" % (name, error)) if SHOW_OUTPUT: logger.info( "\n\n\tRunning fixed point iterations WITH aitken acceleration on problem %s.\n\n\tFunction %s.\n" % (name, name)) x_final = aitken_fp(Gfun, x0, maxit, rtol, atol, output=SHOW_OUTPUT)
rtol = 10**(-10) # interp_representation_error interp_representation_error = lambda Gfun, x_final: abs( Gfun(x_final) - x_final) # run trials for Gfun_name, Gfun in fixed_point_functions.items(): Gfun = fixed_point_functions[Gfun_name] x = initial_guesses[Gfun_name] # find x* using aiken acceleration and method 1 # method 1 - in 'fixed_point.py' x_target = fixed_point(Gfun, x, maxit, rtol, atol, output=SHOW_OUTPUT) # display interp_representation_error error1 = interp_representation_error(Gfun, x) logger.info( "\n\n[Method 1]: \nFinal fixed point target error WITHOUT aitken acceleration \nusing newtons method on a newton interpolating polynomial in place of Ffun.\n \ \n\tFunction name: %s, \n\n\t\t |Gfun(x) - x| = %s.\n" % (Gfun_name, error1)) # aiken acceleration x_target = aitken_fp(Gfun, x, maxit, rtol, atol, output=SHOW_OUTPUT) # display interp_representation_error error2 = interp_representation_error(Gfun, x) logger.info( "\n\n[Method 2]: \nFinal fixed point target error using acceleration acceleration.\n \