def fixed_point(expr, arguments, max_err=1e-5, max_iter=50): if len(arguments) != 1: raise ValueError("Error! Invalid number of arguments") xi = arguments[0] f = expr_to_lambda(expr) symbol = get_symbol(expr) output = Output() _init_output(output, "Fixed-Point", f, lambda x: x - f(x)) cur_xi = numpy.empty(0, dtype=numpy.float64) cur_err_i = numpy.empty(0, dtype=numpy.float64) cur_xi = numpy.append(cur_xi, xi) cur_err_i = numpy.append(cur_err_i, float('NaN')) begin = timeit.default_timer() for i in range(0, max_iter): root = xi - f(xi) err = abs((root - xi)) xi = root cur_xi = numpy.append(cur_xi, root) cur_err_i = numpy.append(cur_err_i, err) if err <= max_err: break end = timeit.default_timer() try: x_next = xi - f(xi) output.error_bound = abs(x_next - xi) except (ZeroDivisionError, OverflowError): output.error_bound = 0 output.execution_time = abs(end - begin) output.roots = numpy.append(output.roots, root) output.errors = numpy.append(output.errors, err) output.dataframes.append( create_dataframe(cur_xi, output.function, cur_err_i, symbol)) return output
def regula_falsi(expr, arguments, max_err=1e-5, max_iter=50): if len(arguments) != 2: raise ValueError("Error! Invalid number of arguments") xl, xu = min(arguments[0], arguments[1]), max(arguments[0], arguments[1]) f = expr_to_lambda(expr) if f(xl) * f(xu) > 0: raise ValueError("Error! There are no roots in the range [%d, %d]" % (xl, xu)) prev_xr = 0 symbol = get_symbol(expr) output = Output() _init_output(output, "Regula-Falsi", f, expr_to_lambda(diff(expr))) cur_xi = numpy.empty(0, dtype=numpy.float64) cur_err_i = numpy.empty(0, dtype=numpy.float64) cur_xi = numpy.append(cur_xi, xl) cur_err_i = numpy.append(cur_err_i, float('NaN')) begin = timeit.default_timer() for _ in range(0, max_iter): yl = f(xl) yu = f(xu) xr = (xl * yu - xu * yl) / (yu - yl) yr = f(xr) err = abs(xr - prev_xr) if yr * yu < 0: xl = xr elif yr * yl < 0: xu = xr else: err = 0 prev_xr = xr cur_xi = numpy.append(cur_xi, xr) cur_err_i = numpy.append(cur_err_i, err) if err <= max_err: break end = timeit.default_timer() try: yl = f(xl) yu = f(xu) x_next = (xl * yu - xu * yl) / (yu - yl) output.error_bound = abs(x_next - prev_xr) except (ZeroDivisionError, OverflowError): output.error_bound = 0 output.execution_time = abs(end - begin) output.roots = numpy.append(output.roots, xr) output.errors = numpy.append(output.errors, err) output.dataframes.append( create_dataframe(cur_xi, output.function, cur_err_i, symbol)) return output
def newton_mod2(expr, arguments, max_err=1e-5, max_iter=50): if len(arguments) != 1: raise ValueError("Error! Invalid number of arguments") xi = arguments[0] f = expr_to_lambda(expr) expr_diff = diff(expr) f_diff = expr_to_lambda(expr_diff) f_diff2 = expr_to_lambda(diff(expr_diff)) symbol = get_symbol(expr) output = Output() _init_output(output, "Newton-Raphson Mod#2", f, f_diff) cur_xi = numpy.empty(0, dtype=numpy.float64) cur_err_i = numpy.empty(0, dtype=numpy.float64) cur_xi = numpy.append(cur_xi, xi) cur_err_i = numpy.append(cur_err_i, float('NaN')) begin = timeit.default_timer() for _ in range(0, max_iter): fxi = f(xi) f_diff_xi = f_diff(xi) f_diff_xi2 = f_diff2(xi) root = xi - f_diff_xi * fxi / (f_diff_xi**2 - fxi * f_diff_xi2) err = abs((root - xi)) xi = root cur_xi = numpy.append(cur_xi, root) cur_err_i = numpy.append(cur_err_i, err) if err <= max_err: break end = timeit.default_timer() try: fxi = f(xi) f_diff_xi = f_diff(xi) f_diff_xi2 = f_diff2(xi) x_next = xi - f_diff_xi * fxi / (f_diff_xi**2 - fxi * f_diff_xi2) output.error_bound = abs(x_next - xi) except (ZeroDivisionError, OverflowError): output.error_bound = 0 output.execution_time = abs(end - begin) output.roots = numpy.append(output.roots, root) output.errors = numpy.append(output.errors, err) output.dataframes.append( create_dataframe(cur_xi, output.function, cur_err_i, symbol)) return output
def bisection(expr, arguments, max_err=1e-5, max_iter=50): if len(arguments) != 2: raise ValueError("Error! Invalid number of arguments") xl, xu = min(arguments[0], arguments[1]), max(arguments[0], arguments[1]) f = expr_to_lambda(expr) if f(xl) * f(xu) > 0: raise ValueError("Error! There are no roots in the range [%d, %d]" % (xl, xu)) prev_xr = 0 symbol = get_symbol(expr) output = Output() _init_output(output, "Bisection", f, lambda x: x / 2) output.error_bound = ceil(abs(log2(abs(xu - xl)) - log2(max_err))) cur_xi = numpy.empty(0, dtype=numpy.float64) cur_err_i = numpy.empty(0, dtype=numpy.float64) cur_xi = numpy.append(cur_xi, xl) cur_err_i = numpy.append(cur_err_i, float('NaN')) begin = timeit.default_timer() for _ in range(0, max_iter): yl = f(xl) yu = f(xu) xr = (xl + xu) / 2 yr = f(xr) err = abs(xr - prev_xr) if yr * yu < 0: xl = xr elif yr * yl < 0: xu = xr else: err = 0 prev_xr = xr cur_xi = numpy.append(cur_xi, xr) cur_err_i = numpy.append(cur_err_i, err) if err <= max_err: break end = timeit.default_timer() output.execution_time = abs(end - begin) output.roots = numpy.append(output.roots, xr) output.errors = numpy.append(output.errors, err) output.dataframes.append( create_dataframe(cur_xi, output.function, cur_err_i, symbol)) return output